-
Notifications
You must be signed in to change notification settings - Fork 945
OIDC Login Module with JWT token support #6304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
grgrzybek
wants to merge
2
commits into
apache:main
Choose a base branch
from
grgrzybek:oidc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,7 @@ | |
| import org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory; | ||
| import org.apache.activemq.artemis.protocol.amqp.sasl.PlainSASLResult; | ||
| import org.apache.activemq.artemis.protocol.amqp.sasl.SASLResult; | ||
| import org.apache.activemq.artemis.protocol.amqp.sasl.TokenSASLResult; | ||
| import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; | ||
| import org.apache.activemq.artemis.spi.core.remoting.ReadyListener; | ||
| import org.apache.activemq.artemis.utils.ByteUtil; | ||
|
|
@@ -729,6 +730,9 @@ private boolean validateUser(Connection connection) throws Exception { | |
| if (saslResult instanceof PlainSASLResult plainSASLResult) { | ||
| password = plainSASLResult.getPassword(); | ||
| } | ||
| if (saslResult instanceof TokenSASLResult tokenSASLResult) { | ||
| password = tokenSASLResult.getToken(); | ||
| } | ||
| } | ||
|
|
||
| if (isIncomingConnection() && saslClientFactory == null && !isBrokerConnection()) { | ||
|
|
@@ -988,6 +992,9 @@ public String getPassword() { | |
| if (saslResult instanceof PlainSASLResult plainSASLResult) { | ||
| password = plainSASLResult.getPassword(); | ||
| } | ||
| if (saslResult instanceof TokenSASLResult tokenSASLResult) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also should just be an else-if |
||
| password = tokenSASLResult.getToken(); | ||
| } | ||
| } | ||
|
|
||
| return password; | ||
|
|
||
95 changes: 95 additions & 0 deletions
95
...rotocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/OAuthBearerSASL.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.activemq.artemis.protocol.amqp.sasl; | ||
|
|
||
| import org.apache.activemq.artemis.core.security.SecurityStore; | ||
| import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
|
|
||
| public class OAuthBearerSASL extends ServerSASLToken { | ||
|
|
||
| // https://datatracker.ietf.org/doc/html/rfc7628#section-3 | ||
| static final String MECHANISM_NAME = "OAUTHBEARER"; | ||
|
|
||
| public OAuthBearerSASL(SecurityStore securityStore, String securityDomain, RemotingConnection remotingConnection) { | ||
| super(securityStore, securityDomain, remotingConnection); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return MECHANISM_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] processSASL(byte[] bytes) { | ||
| // expecting `n,a=<user>,\x01host=<host>\x01port=<port>\x01auth=Bearer <token>\x01\x01` | ||
| // according to https://datatracker.ietf.org/doc/html/rfc7628#section-3.1 | ||
| // `n,a=<user>,` is from https://datatracker.ietf.org/doc/html/rfc5801 | ||
| // - gs2-cb-flag: "n" = client does not support channel binding, | ||
| // "p" = client supports and used channel binding | ||
| // "y" = client supports CB, thinks the server does not | ||
| // - gs2-authzid: "a=<saslname>" | ||
| if (bytes == null || bytes.length == 0) { | ||
| result = new TokenSASLResult(false, null, null); | ||
| } else { | ||
| if (bytes.length < 2 || !(bytes[bytes.length - 2] == '\001' && bytes[bytes.length - 1] == '\001')) { | ||
| result = new TokenSASLResult(false, null, null); | ||
| } else { | ||
| String data = new String(bytes, StandardCharsets.UTF_8); | ||
| String[] segments = data.split("\001"); | ||
| if (segments.length < 2) { | ||
| result = new TokenSASLResult(false, null, null); | ||
| } else { | ||
| String gs2Header = segments[0]; | ||
| String[] headersSegments = gs2Header.split(","); | ||
| String user = null; | ||
| for (String s : headersSegments) { | ||
| if (s.startsWith("a=")) { | ||
| user = s.substring(2); | ||
| } | ||
| } | ||
|
|
||
| String token = null; | ||
| for (int i = 1; i < segments.length; i++) { | ||
| if (segments[i].startsWith("auth=Bearer ")) { | ||
| token = segments[i].substring(12); | ||
| } | ||
| } | ||
|
|
||
| if (token == null) { | ||
| result = new TokenSASLResult(false, null, null); | ||
| } else { | ||
| boolean success = authenticate(user, token); | ||
| result = new TokenSASLResult(success, user, token); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: validate host and port from the SASL OAUTHBEARER initial response? | ||
|
|
||
| if (result.isSuccess()) { | ||
| return null; | ||
| } | ||
|
|
||
| // see https://datatracker.ietf.org/doc/html/rfc7628#section-3.2.2 | ||
| return Base64.getEncoder().encode("{\"status\":\"invalid_token\"}".getBytes()); | ||
| } | ||
|
|
||
| } |
48 changes: 48 additions & 0 deletions
48
...ain/java/org/apache/activemq/artemis/protocol/amqp/sasl/OAuthBearerServerSASLFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.activemq.artemis.protocol.amqp.sasl; | ||
|
|
||
| import org.apache.activemq.artemis.core.server.ActiveMQServer; | ||
| import org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor; | ||
| import org.apache.activemq.artemis.protocol.amqp.proton.AMQPRoutingHandler; | ||
| import org.apache.activemq.artemis.spi.core.protocol.ProtocolManager; | ||
| import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; | ||
| import org.apache.activemq.artemis.spi.core.remoting.Connection; | ||
|
|
||
| public class OAuthBearerServerSASLFactory implements ServerSASLFactory { | ||
|
|
||
| @Override | ||
| public String getMechanism() { | ||
| return OAuthBearerSASL.MECHANISM_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public ServerSASL create(ActiveMQServer server, ProtocolManager<AmqpInterceptor, AMQPRoutingHandler> manager, Connection connection, RemotingConnection remotingConnection) { | ||
| return new OAuthBearerSASL(server.getSecurityStore(), manager.getSecurityDomain(), connection.getProtocolConnection()); | ||
| } | ||
|
|
||
| @Override | ||
| public int getPrecedence() { | ||
| return 32; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isDefaultPermitted() { | ||
| return false; | ||
| } | ||
|
|
||
| } |
57 changes: 57 additions & 0 deletions
57
...rotocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/ServerSASLToken.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.activemq.artemis.protocol.amqp.sasl; | ||
|
|
||
| import org.apache.activemq.artemis.core.security.SecurityStore; | ||
| import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection; | ||
|
|
||
| public abstract class ServerSASLToken implements ServerSASL { | ||
|
|
||
| protected final SecurityStore securityStore; | ||
| protected final String securityDomain; | ||
| protected RemotingConnection remotingConnection; | ||
|
|
||
| protected SASLResult result = null; | ||
|
|
||
| public ServerSASLToken(SecurityStore securityStore, String securityDomain, RemotingConnection remotingConnection) { | ||
| this.securityStore = securityStore; | ||
| this.securityDomain = securityDomain; | ||
| this.remotingConnection = remotingConnection; | ||
| } | ||
|
|
||
| @Override | ||
| public SASLResult result() { | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public void done() { | ||
| } | ||
|
|
||
| protected boolean authenticate(String user, String token) { | ||
| if (securityStore != null && securityStore.isSecurityEnabled()) { | ||
| try { | ||
| securityStore.authenticate(user, token, remotingConnection, securityDomain); | ||
| return true; | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } |
56 changes: 56 additions & 0 deletions
56
...rotocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/TokenSASLResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.activemq.artemis.protocol.amqp.sasl; | ||
|
|
||
| import javax.security.auth.Subject; | ||
|
|
||
| /** | ||
| * A {@link SASLResult} containing a String representation of a token (like JWT) and optional username (usually | ||
| * not relevant). | ||
| */ | ||
| public class TokenSASLResult implements SASLResult { | ||
|
|
||
| private final boolean success; | ||
| private final String user; | ||
| private final String token; | ||
|
|
||
| public TokenSASLResult(boolean success, String user, String token) { | ||
| this.success = success; | ||
| this.user = user; | ||
| this.token = token; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSuccess() { | ||
| return success; | ||
| } | ||
|
|
||
| @Override | ||
| public String getUser() { | ||
| return user; | ||
| } | ||
|
|
||
| public String getToken() { | ||
| return token; | ||
| } | ||
|
|
||
| @Override | ||
| public Subject getSubject() { | ||
| return null; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an else-if since its only one or the other