-
Notifications
You must be signed in to change notification settings - Fork 70
Support jwt guarded identity via custom token claim #1277
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
Merged
Merged
Changes from 1 commit
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
WIP
- Loading branch information
commit 27c4be378a50962591416828d5839a3a0dc5d210
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
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 |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ public class JwtGuardHandler implements GuardHandler | |
| private final String issuer; | ||
| private final String audience; | ||
| private final Duration challenge; | ||
| private final String identity; | ||
| private final Map<String, JsonWebKey> keys; | ||
| private final Long2ObjectHashMap<JwtSession> sessionsById; | ||
| private final LongSupplier supplyAuthorizedId; | ||
|
|
@@ -72,14 +73,15 @@ public JwtGuardHandler( | |
| this.issuer = options.issuer; | ||
| this.audience = options.audience; | ||
| this.challenge = options.challenge.orElse(null); | ||
| this.identity = options.identity; | ||
|
|
||
| List<JwtKeyConfig> keysConfig = options.keys; | ||
| if ((keysConfig == null || keysConfig.isEmpty()) && options.keysURL.isPresent()) | ||
| { | ||
| JsonbConfig config = new JsonbConfig() | ||
| JsonbConfig keyConfig = new JsonbConfig() | ||
| .withAdapters(new JwtKeySetConfigAdapter()); | ||
| Jsonb jsonb = JsonbBuilder.newBuilder() | ||
| .withConfig(config) | ||
| .withConfig(keyConfig) | ||
|
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. Let's inline the |
||
| .build(); | ||
| Path keysPath = context.resolvePath(options.keysURL.get()); | ||
| String keysText = readKeys(keysPath); | ||
|
|
@@ -128,7 +130,7 @@ public long reauthorize( | |
| String credentials) | ||
| { | ||
| JwtSession session = null; | ||
| String subject = null; | ||
| String identity = null; | ||
| String reason = ""; | ||
|
|
||
| authorize: | ||
|
|
@@ -158,7 +160,7 @@ public long reauthorize( | |
|
|
||
| String payload = signature.getPayload(); | ||
| JwtClaims claims = JwtClaims.parse(payload); | ||
| subject = claims.getSubject(); | ||
| identity = this.identity != null ? claims.getStringClaimValue(this.identity) : claims.getSubject(); | ||
| NumericDate notBefore = claims.getNotBefore(); | ||
| NumericDate notAfter = claims.getExpirationTime(); | ||
| String issuer = claims.getIssuer(); | ||
|
|
@@ -185,7 +187,7 @@ public long reauthorize( | |
| .orElse(null); | ||
|
|
||
| JwtSessionStore sessionStore = supplySessionStore(contextId); | ||
| session = sessionStore.supplySession(subject, roles); | ||
| session = sessionStore.supplySession(identity, roles); | ||
|
|
||
| session.credentials = credentials; | ||
| session.roles = roles; | ||
|
|
@@ -204,7 +206,7 @@ public long reauthorize( | |
| } | ||
| if (session == null) | ||
| { | ||
| event.authorizationFailed(traceId, bindingId, subject, reason); | ||
| event.authorizationFailed(traceId, bindingId, identity, reason); | ||
| } | ||
| return session != null ? session.authorized : NOT_AUTHORIZED; | ||
| } | ||
|
|
@@ -231,7 +233,7 @@ public String identity( | |
| long sessionId) | ||
| { | ||
| JwtSession session = sessionsById.get(sessionId); | ||
| return session != null ? session.subject : null; | ||
| return session != null ? session.identity : null; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -298,51 +300,51 @@ private JwtSessionStore supplySessionStore( | |
| private final class JwtSessionStore | ||
| { | ||
| private final long contextId; | ||
| private final Map<String, JwtSession> sessionsBySubject; | ||
| private final Map<String, JwtSession> sessionsByIdentity; | ||
|
|
||
| private JwtSessionStore( | ||
| long contextId) | ||
| { | ||
| this.contextId = contextId; | ||
| this.sessionsBySubject = new IdentityHashMap<>(); | ||
| this.sessionsByIdentity = new IdentityHashMap<>(); | ||
| } | ||
|
|
||
| private JwtSession supplySession( | ||
| String subject, | ||
| String identity, | ||
| List<String> roles) | ||
| { | ||
| String subjectKey = subject != null ? subject.intern() : null; | ||
| JwtSession session = sessionsBySubject.get(subjectKey); | ||
| String identityKey = identity != null ? identity.intern() : null; | ||
| JwtSession session = sessionsByIdentity.get(identityKey); | ||
|
|
||
| if (subjectKey == null || session != null && roles != null && !supersetOf(session, roles)) | ||
| if (identityKey == null || session != null && roles != null && !supersetOf(session, roles)) | ||
| { | ||
| session = newSession(subjectKey); | ||
| session = newSession(identityKey); | ||
| } | ||
| else | ||
| { | ||
| session = sessionsBySubject.computeIfAbsent(subjectKey, this::newSharedSession); | ||
| session = sessionsByIdentity.computeIfAbsent(identityKey, this::newSharedSession); | ||
| } | ||
|
|
||
| return session; | ||
| } | ||
|
|
||
| private JwtSession newSharedSession( | ||
| String subject) | ||
| String identity) | ||
| { | ||
| return new JwtSession(supplyAuthorizedId.getAsLong(), subject, this::onUnshared); | ||
| return new JwtSession(supplyAuthorizedId.getAsLong(), identity, this::onUnshared); | ||
| } | ||
|
|
||
| private JwtSession newSession( | ||
| String subject) | ||
| String identity) | ||
| { | ||
| return new JwtSession(supplyAuthorizedId.getAsLong(), subject); | ||
| return new JwtSession(supplyAuthorizedId.getAsLong(), identity); | ||
| } | ||
|
|
||
| private void onUnshared( | ||
| JwtSession session) | ||
| { | ||
| sessionsBySubject.remove(session.subject); | ||
| if (sessionsBySubject.isEmpty()) | ||
| sessionsByIdentity.remove(session.identity); | ||
| if (sessionsByIdentity.isEmpty()) | ||
| { | ||
| sessionStoresByContextId.remove(contextId); | ||
| } | ||
|
|
@@ -352,7 +354,7 @@ private void onUnshared( | |
| private final class JwtSession | ||
| { | ||
| private final long authorized; | ||
| private final String subject; | ||
| private final String identity; | ||
| private final Consumer<JwtSession> unshare; | ||
|
|
||
| private String credentials; | ||
|
|
@@ -366,26 +368,26 @@ private final class JwtSession | |
|
|
||
| private JwtSession( | ||
| long authorized, | ||
| String subject) | ||
| String identity) | ||
| { | ||
| this(authorized, subject, null); | ||
| this(authorized, identity, null); | ||
| } | ||
|
|
||
| private JwtSession( | ||
| long authorized, | ||
| String subject, | ||
| String identity, | ||
| Consumer<JwtSession> unshare) | ||
| { | ||
| this.authorized = authorized; | ||
| this.subject = subject; | ||
| this.identity = identity; | ||
| this.unshare = unshare; | ||
| } | ||
|
|
||
| boolean challenge( | ||
| long now) | ||
| { | ||
| final boolean challenge = | ||
| subject != null && | ||
| identity != null && | ||
| challengeAt <= now && now < expiresAt && | ||
| challengedAt < challengeAt; | ||
|
|
||
|
|
||
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
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.
Can we remove this?