spring-projects / spring-authorization-server

Spring Authorization Server
https://spring.io/projects/spring-authorization-server
Apache License 2.0
4.84k stars 1.27k forks source link

Add getCode to OAuth2Authorization? #1727

Closed colin-riddell closed 2 weeks ago

colin-riddell commented 2 weeks ago

Expected Behavior

Ideally, it would be nice if OAuth2Authorization had a getCode() that returns the auth code token? Even more ideal: the tokens are stored in the OAuth2Authorization and keyd only against a tokenType value?

Current Behavior Currently, there's no way to get the code from the token, as far as I can see? It doesn't behave the same as the refresh and access token type, and it looks like it's keyd in the tokens map with an instance of OAuth2AuthorizationCode making it hard to obtain by directly accessing the map?

Context

I'm trying to cache tokens against their different lookup values.

Given the need to do the following in find by token I'm able to cache against access and refresh token keys, but unable to cache against code.

switch (tokenType.getValue()) {
                        case OAuth2ParameterNames.STATE ->
                                this.authorizationReadRepository.findByState(token);
                        case OAuth2ParameterNames.CODE ->
                                this.authorizationReadRepository.findByAuthorizationCode(token);
                        case OAuth2ParameterNames.ACCESS_TOKEN ->
                                this.authorizationReadRepository.findByAccessToken(token);
                        case OAuth2ParameterNames.REFRESH_TOKEN ->
                                this.authorizationReadRepository.findByRefreshToken(token);
                        case OidcParameterNames.ID_TOKEN ->
                                this.authorizationReadRepository.findByOidcToken(token);
                        case OAuth2ParameterNames.DEVICE_CODE ->
                                this.authorizationReadRepository.findByDeviceCode(token);
                        case OAuth2ParameterNames.USER_CODE ->
                                this.authorizationReadRepository.findByUserCode(token);
colin-riddell commented 2 weeks ago

I have realised that authorization.getToken(OAuth2AuthorizationCode.class) will get the code.