|
| 1 | +package com.example.authorizationserver.oauth.endpoint.token; |
| 2 | + |
| 3 | +import com.example.authorizationserver.config.AuthorizationServerConfigurationProperties; |
| 4 | +import com.example.authorizationserver.oauth.client.model.AccessTokenFormat; |
| 5 | +import com.example.authorizationserver.oauth.client.model.RegisteredClient; |
| 6 | +import com.example.authorizationserver.oauth.common.ClientCredentials; |
| 7 | +import com.example.authorizationserver.oauth.common.GrantType; |
| 8 | +import com.example.authorizationserver.oauth.common.TokenType; |
| 9 | +import com.example.authorizationserver.oauth.endpoint.token.resource.TokenRequest; |
| 10 | +import com.example.authorizationserver.oauth.endpoint.token.resource.TokenResponse; |
| 11 | +import com.example.authorizationserver.scim.model.ScimUserEntity; |
| 12 | +import com.example.authorizationserver.scim.service.ScimService; |
| 13 | +import com.example.authorizationserver.security.client.RegisteredClientAuthenticationService; |
| 14 | +import com.example.authorizationserver.token.jwt.JsonWebTokenService; |
| 15 | +import com.example.authorizationserver.token.store.TokenService; |
| 16 | +import com.example.authorizationserver.token.store.model.JsonWebToken; |
| 17 | +import com.nimbusds.jose.JOSEException; |
| 18 | +import com.nimbusds.jwt.JWTClaimsSet; |
| 19 | +import org.apache.commons.lang3.StringUtils; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.springframework.http.ResponseEntity; |
| 23 | +import org.springframework.security.core.AuthenticationException; |
| 24 | +import org.springframework.stereotype.Service; |
| 25 | + |
| 26 | +import java.text.ParseException; |
| 27 | +import java.time.Duration; |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | +import static com.example.authorizationserver.oauth.endpoint.token.resource.TokenResponse.BEARER_TOKEN_TYPE; |
| 31 | + |
| 32 | +@Service |
| 33 | +public class TokenExchangeEndpointService { |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(TokenExchangeEndpointService.class); |
| 35 | + |
| 36 | + private final TokenService tokenService; |
| 37 | + private final ScimService scimService; |
| 38 | + private final AuthorizationServerConfigurationProperties authorizationServerProperties; |
| 39 | + private final RegisteredClientAuthenticationService registeredClientAuthenticationService; |
| 40 | + private final JsonWebTokenService jsonWebTokenService; |
| 41 | + |
| 42 | + public TokenExchangeEndpointService( |
| 43 | + TokenService tokenService, |
| 44 | + ScimService scimService, AuthorizationServerConfigurationProperties authorizationServerProperties, |
| 45 | + RegisteredClientAuthenticationService registeredClientAuthenticationService, JsonWebTokenService jsonWebTokenService) { |
| 46 | + this.tokenService = tokenService; |
| 47 | + this.scimService = scimService; |
| 48 | + this.authorizationServerProperties = authorizationServerProperties; |
| 49 | + this.registeredClientAuthenticationService = registeredClientAuthenticationService; |
| 50 | + this.jsonWebTokenService = jsonWebTokenService; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * ------------------------- Exchanging a Token |
| 55 | + * |
| 56 | + * <p>The client makes a token exchange request to the token endpoint with an extension grant type using the HTTP POST method. |
| 57 | + * The following parameters are included in the HTTP request entity-body using the application/x-www-form-urlencoded format per |
| 58 | + * Appendix B with a character encoding of UTF-8 in the HTTP request entity-body: |
| 59 | + * |
| 60 | + * <p>grant_type REQUIRED. Value MUST be set to "urn:ietf:params:oauth:grant-type:token-exchange". |
| 61 | + * refresh_token REQUIRED. The refresh token issued to the client. scope OPTIONAL. The scope of the access request as |
| 62 | + * described by Section 3.3. The requested scope MUST NOT include any scope not originally granted |
| 63 | + * by the resource owner, and if omitted is treated as equal to the scope originally granted by |
| 64 | + * the resource owner. |
| 65 | + */ |
| 66 | + public ResponseEntity<TokenResponse> getTokenResponseForTokenExchange( |
| 67 | + String authorizationHeader, TokenRequest tokenRequest) { |
| 68 | + |
| 69 | + LOG.debug("Exchange token for given token with [{}]", tokenRequest); |
| 70 | + |
| 71 | + ClientCredentials clientCredentials = |
| 72 | + TokenEndpointHelper.retrieveClientCredentials(authorizationHeader, tokenRequest); |
| 73 | + |
| 74 | + if (clientCredentials == null) { |
| 75 | + return TokenEndpointHelper.reportInvalidClientError(); |
| 76 | + } |
| 77 | + |
| 78 | + Duration accessTokenLifetime = authorizationServerProperties.getAccessToken().getLifetime(); |
| 79 | + Duration refreshTokenLifetime = authorizationServerProperties.getRefreshToken().getLifetime(); |
| 80 | + |
| 81 | + RegisteredClient registeredClient; |
| 82 | + |
| 83 | + try { |
| 84 | + registeredClient = |
| 85 | + registeredClientAuthenticationService.authenticate( |
| 86 | + clientCredentials.getClientId(), clientCredentials.getClientSecret()); |
| 87 | + |
| 88 | + } catch (AuthenticationException ex) { |
| 89 | + return TokenEndpointHelper.reportInvalidClientError(); |
| 90 | + } |
| 91 | + |
| 92 | + if (registeredClient.getGrantTypes().contains(GrantType.TOKEN_EXCHANGE)) { |
| 93 | + TokenType tokenType = TokenType.ACCESS_TOKEN; |
| 94 | + if (tokenRequest.getRequested_token_type() != null) { |
| 95 | + try { |
| 96 | + tokenType = TokenType.getTokenTypeForIdentifier(tokenRequest.getRequested_token_type()); |
| 97 | + } catch (IllegalArgumentException ex) { |
| 98 | + LOG.warn("Token exchange is not valid for requested token type [{}]", tokenRequest.getRequested_token_type()); |
| 99 | + return TokenEndpointHelper.reportInvalidRequestError(); |
| 100 | + } |
| 101 | + } |
| 102 | + if (!TokenType.ACCESS_TOKEN.equals(tokenType)) { |
| 103 | + LOG.warn("Token exchange is not valid for requested token type [{}]", tokenRequest.getRequested_token_type()); |
| 104 | + return TokenEndpointHelper.reportInvalidRequestError(); |
| 105 | + } |
| 106 | + JsonWebToken jsonWebToken = tokenService.findJsonWebToken(tokenRequest.getSubject_token()); |
| 107 | + if (jsonWebToken != null && jsonWebToken.isAccessToken()) { |
| 108 | + Set<String> scopes = new HashSet<>(); |
| 109 | + if (StringUtils.isNotBlank(tokenRequest.getScope())) { |
| 110 | + scopes = new HashSet<>(Arrays.asList(tokenRequest.getScope().split(" "))); |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + JWTClaimsSet jwtClaimsSet = |
| 115 | + jsonWebTokenService.parseAndValidateToken(jsonWebToken.getValue()); |
| 116 | + String subject = jwtClaimsSet.getSubject(); |
| 117 | + if (TokenService.ANONYMOUS_TOKEN.equals(subject)) { |
| 118 | + |
| 119 | + LOG.info( |
| 120 | + "Creating anonymous token response for token exchange with client [{}]", |
| 121 | + clientCredentials.getClientId()); |
| 122 | + |
| 123 | + return ResponseEntity.ok( |
| 124 | + new TokenResponse( |
| 125 | + AccessTokenFormat.JWT.equals(registeredClient.getAccessTokenFormat()) |
| 126 | + ? tokenService |
| 127 | + .createAnonymousJwtAccessToken( |
| 128 | + clientCredentials.getClientId(), scopes, accessTokenLifetime) |
| 129 | + .getValue() |
| 130 | + : tokenService |
| 131 | + .createAnonymousOpaqueAccessToken( |
| 132 | + clientCredentials.getClientId(), scopes, accessTokenLifetime) |
| 133 | + .getValue(), |
| 134 | + tokenService |
| 135 | + .createAnonymousRefreshToken( |
| 136 | + clientCredentials.getClientId(), scopes, refreshTokenLifetime) |
| 137 | + .getValue(), |
| 138 | + accessTokenLifetime.toSeconds(), |
| 139 | + null, |
| 140 | + BEARER_TOKEN_TYPE, TokenType.ACCESS_TOKEN.getIdentifier(), tokenRequest.getScope())); |
| 141 | + } else { |
| 142 | + Optional<ScimUserEntity> authenticatedUser = |
| 143 | + scimService.findUserByIdentifier(UUID.fromString(subject)); |
| 144 | + if (authenticatedUser.isPresent()) { |
| 145 | + |
| 146 | + LOG.info( |
| 147 | + "Creating personalized token response for token exchange with client [{}]", |
| 148 | + clientCredentials.getClientId()); |
| 149 | + |
| 150 | + return ResponseEntity.ok( |
| 151 | + new TokenResponse( |
| 152 | + AccessTokenFormat.JWT.equals(registeredClient.getAccessTokenFormat()) |
| 153 | + ? tokenService |
| 154 | + .createPersonalizedJwtAccessToken( |
| 155 | + authenticatedUser.get(), |
| 156 | + clientCredentials.getClientId(), |
| 157 | + null, |
| 158 | + scopes, |
| 159 | + accessTokenLifetime) |
| 160 | + .getValue() |
| 161 | + : tokenService |
| 162 | + .createPersonalizedOpaqueAccessToken( |
| 163 | + authenticatedUser.get(), |
| 164 | + clientCredentials.getClientId(), |
| 165 | + scopes, |
| 166 | + accessTokenLifetime) |
| 167 | + .getValue(), |
| 168 | + tokenService |
| 169 | + .createPersonalizedRefreshToken( |
| 170 | + clientCredentials.getClientId(), |
| 171 | + authenticatedUser.get(), |
| 172 | + scopes, |
| 173 | + refreshTokenLifetime) |
| 174 | + .getValue(), |
| 175 | + accessTokenLifetime.toSeconds(), |
| 176 | + null, |
| 177 | + BEARER_TOKEN_TYPE, TokenType.ACCESS_TOKEN.getIdentifier(), tokenRequest.getScope())); |
| 178 | + } |
| 179 | + } |
| 180 | + tokenService.remove(jsonWebToken); |
| 181 | + } catch (ParseException | JOSEException e) { |
| 182 | + return TokenEndpointHelper.reportInvalidRequestError(); |
| 183 | + } |
| 184 | + } |
| 185 | + return TokenEndpointHelper.reportInvalidClientError(); |
| 186 | + } else { |
| 187 | + return TokenEndpointHelper.reportUnauthorizedClientError(); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments