Add better support for JWT bearer token authentication in openidconnectbearer mode to enable external API calls#9174
Open
tylerjmchugh wants to merge 5 commits intogeonetwork:mainfrom
Open
Conversation
674b305 to
d0f0f86
Compare
| username = ((OAuth2User)principal).getAttribute(StandardClaimNames.PREFERRED_USERNAME); | ||
| } | ||
| else if (authentication instanceof JwtAuthenticationToken) { | ||
| username = authentication.getName(); |
Contributor
There was a problem hiding this comment.
Looks like you're trying to get the username but this will actually return the subject of the principal, based on Spring java docs In our case, it will be the uuid from Keycloak
Sth like following might work but I have not tested it
authentication.getToken().getClaim(StandardClaimNames.PREFERRED_USERNAME)
Contributor
Author
There was a problem hiding this comment.
It should not be the subject as I set the name myself when I create the authentication in GeonetworkJwtAuthenticationProvider using the 3 arg constructor. The value comes from the extractUsername method which extracts the username from a list of options starting with the value configured in geonetwork.
d0f0f86 to
b3971e2
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Currently, when users authenticate via JWT bearer tokens with the
openidconnectbearerauth type, the application cannot access the original token to pass to external APIs. The previous approach relied onOAuth2AuthenticationTokenwithOAuth2AuthorizedClientManager, which requires an authorized client in the application. This fails for bearer token users since they are external (never logged into the application directly) and have no authorized client.Additionally, bearer token authentication is stateless with no server session. The JWT token is lost after parsing into an
OAuth2Userprincipal, making it unavailable for subsequent external API calls.This PR aims to fix this by:
JwtAuthenticationToken, which stores the decoded Jwt containing the original token accessible viagetToken().getTokenValue()This enables the
openidconnectbearerauth type to make authenticated external API calls with the user's token.Checklist
mainbranch, backports managed with labelREADME.mdfilespom.xmldependency management. Update build documentation with intended library use and library tutorials or documentation