-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Wrong JWT token sub attribute after second authentication #384
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
Comments
@jacksoncastro The current behaviour is correct. When the client performs the Take a look at the You might want to review the Client Credentials grant for further details. |
After upgrading to version Detail any new request to So in that case who is with the correct behavior? |
@jgrandja we just stumbled across the same issue. Due to not being able to get refresh tokens using
but we can't get a refresh token in the first place, so we're kind of stuck here. Any idea how to get around this? Thanks |
If the access token expires and there is no refresh token available, then the client needs to go through the |
@nickmelis take a look at what the OIDC spec says regarding I recently set up a SPA to use |
Describe the bug
I have an environment composed of a SPA, a resource server and an authorization server using Spring's new library.
SPA authorizes using the Authorization Code + PKCE flow.
Lib versions:
spring-boot-starter-parent: 2.5.2
spring-security-oauth2-authorization-server: 0.1.0
I'm having a somewhat complex problem to explain, but come on.
During the first authorization of the SPA application, which in turn is redirected to the login screen of the authorization server, I get an access_token with the JWT's "sub" attribute with the desired value (any user identifier), as a response. for example "bob".
When the token expires, a new authorization request is started, which in turn ends up returning a new access_token with the wrong "sub" attribute. Its value turns out to be the customer id and not the user identifier.
I'm going to divide this troubleshooting into 2 steps: 1st and 2nd authentication.
1st authentication (not yet logged into the Authorization Server)
During the first authentication that occurs on the endpoint
/oauth2/authorize
, the Principal is validated, which was passed on by the login form and created an authentication of theUsernamePasswordAuthenticationToken
type, which in turn implements theorg.springframework.security.core.Authentication
interface.This object is saved within the user's session in the
SPRING_SECURITY_CONTEXT
attribute.After that, the
code
is created and saved inauthorizationService.save()
. For theAuthentication#principalName
field, the authentication username (username entered in the login screen) is used.In the request back to
/oauth2/token
it is seen if there is a session and the Principal is searched via theSPRING_SECURITY_CONTEXT
attribute. The correctPrincipal
is loaded. The JWT "sub" is issued with the correct value which is the username.Following the flow, the org.springframework.security.oauth2.server.authorization.web.DelegatingAuthenticationConverter class registers 3 converters:
And ends up using the
ClientCredentialsAuthenticationConverter
converter to create the authorization on top of the request. Here authorization is wrong with application name (spa-client).The first token ends up being issued correctly because its match is made by code, as shown in the following line:
So the wrong authorization is saved in the session.
2nd authentication
At the beginning of authentication it is seen that already exists session. Here the wrong authentication is retrieved and passed to the JWT "sub".
The framework uses this wrong value until it returns the token.
Comments:
Expected behavior
In all authentications, even if the user is logged into the authorization server, the JWT's "sub" attribute must come from the username.
Examples of requests:
Examples of returns:
Correct:
Wrong:
The text was updated successfully, but these errors were encountered: