|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.oauth2.server.authorization.authentication; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 23 | +import org.springframework.security.core.Authentication; |
| 24 | +import org.springframework.security.core.AuthenticationException; |
| 25 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 29 | +import org.springframework.security.oauth2.core.OAuth2RefreshToken; |
| 30 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 31 | +import org.springframework.security.oauth2.jwt.JwtEncoder; |
| 32 | +import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
| 33 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationAttributeNames; |
| 34 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 35 | +import org.springframework.security.oauth2.server.authorization.TokenType; |
| 36 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 37 | +import org.springframework.security.oauth2.server.authorization.config.TokenSettings; |
| 38 | +import org.springframework.security.oauth2.server.authorization.token.OAuth2Tokens; |
| 39 | +import org.springframework.util.Assert; |
| 40 | + |
| 41 | +/** |
| 42 | + * An {@link AuthenticationProvider} implementation for the OAuth 2.0 Refresh Token Grant. |
| 43 | + * |
| 44 | + * @author Alexey Nesterov |
| 45 | + * @since 0.0.3 |
| 46 | + * @see OAuth2RefreshTokenAuthenticationToken |
| 47 | + * @see OAuth2AccessTokenAuthenticationToken |
| 48 | + * @see OAuth2AuthorizationService |
| 49 | + * @see JwtEncoder |
| 50 | + * @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-1.5">Section 1.5 Refresh Token</a> |
| 51 | + * @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-6">Section 6 Refreshing an Access Token</a> |
| 52 | + */ |
| 53 | + |
| 54 | +public class OAuth2RefreshTokenAuthenticationProvider implements AuthenticationProvider { |
| 55 | + |
| 56 | + private final OAuth2AuthorizationService authorizationService; |
| 57 | + private final JwtEncoder jwtEncoder; |
| 58 | + |
| 59 | + public OAuth2RefreshTokenAuthenticationProvider(OAuth2AuthorizationService authorizationService, JwtEncoder jwtEncoder) { |
| 60 | + Assert.notNull(authorizationService, "authorizationService cannot be null"); |
| 61 | + Assert.notNull(jwtEncoder, "jwtEncoder cannot be null"); |
| 62 | + |
| 63 | + this.authorizationService = authorizationService; |
| 64 | + this.jwtEncoder = jwtEncoder; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 69 | + OAuth2RefreshTokenAuthenticationToken refreshTokenAuthentication = |
| 70 | + (OAuth2RefreshTokenAuthenticationToken) authentication; |
| 71 | + |
| 72 | + OAuth2ClientAuthenticationToken clientPrincipal = null; |
| 73 | + if (OAuth2ClientAuthenticationToken.class.isAssignableFrom(refreshTokenAuthentication.getPrincipal().getClass())) { |
| 74 | + clientPrincipal = (OAuth2ClientAuthenticationToken) refreshTokenAuthentication.getPrincipal(); |
| 75 | + } |
| 76 | + |
| 77 | + if (clientPrincipal == null || !clientPrincipal.isAuthenticated()) { |
| 78 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT)); |
| 79 | + } |
| 80 | + |
| 81 | + OAuth2Authorization authorization = this.authorizationService.findByToken(refreshTokenAuthentication.getRefreshToken(), TokenType.REFRESH_TOKEN); |
| 82 | + if (authorization == null) { |
| 83 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT)); |
| 84 | + } |
| 85 | + |
| 86 | + Instant refreshTokenExpiration = authorization.getTokens().getRefreshToken().getExpiresAt(); |
| 87 | + if (refreshTokenExpiration != null && refreshTokenExpiration.isBefore(Instant.now())) { |
| 88 | + // as per https://tools.ietf.org/html/rfc6749#section-5.2 |
| 89 | + // invalid_grant: The provided authorization grant (e.g., authorization |
| 90 | + // code, resource owner credentials) or refresh token is invalid, expired, revoked [...]. |
| 91 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT)); |
| 92 | + } |
| 93 | + |
| 94 | + RegisteredClient registeredClient = clientPrincipal.getRegisteredClient(); |
| 95 | + |
| 96 | + // https://tools.ietf.org/html/rfc6749#section-6 |
| 97 | + // The requested scope MUST NOT include any scope not originally granted by the resource owner, |
| 98 | + // and if omitted is treated as equal to the scope originally granted by the resource owner. |
| 99 | + Set<String> refreshTokenScopes = refreshTokenAuthentication.getScopes(); |
| 100 | + Set<String> authorizedScopes = authorization.getAttribute(OAuth2AuthorizationAttributeNames.AUTHORIZED_SCOPES); |
| 101 | + if (!authorizedScopes.containsAll(refreshTokenScopes)) { |
| 102 | + throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE)); |
| 103 | + } |
| 104 | + |
| 105 | + if (refreshTokenScopes.isEmpty()) { |
| 106 | + refreshTokenScopes = authorizedScopes; |
| 107 | + } |
| 108 | + |
| 109 | + Jwt jwt = OAuth2TokenIssuerUtil |
| 110 | + .issueJwtAccessToken(this.jwtEncoder, authorization.getPrincipalName(), registeredClient.getClientId(), refreshTokenScopes); |
| 111 | + OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, |
| 112 | + jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), refreshTokenScopes); |
| 113 | + |
| 114 | + TokenSettings tokenSettings = registeredClient.getTokenSettings(); |
| 115 | + OAuth2RefreshToken refreshToken; |
| 116 | + if (tokenSettings.reuseRefreshTokens()) { |
| 117 | + refreshToken = authorization.getTokens().getRefreshToken(); |
| 118 | + } else { |
| 119 | + refreshToken = OAuth2TokenIssuerUtil.issueRefreshToken(tokenSettings.refreshTokenTimeToLive()); |
| 120 | + } |
| 121 | + |
| 122 | + authorization = OAuth2Authorization.from(authorization) |
| 123 | + .attribute(OAuth2AuthorizationAttributeNames.ACCESS_TOKEN_ATTRIBUTES, accessToken) |
| 124 | + .tokens(OAuth2Tokens.builder().accessToken(accessToken).refreshToken(refreshToken).build()) |
| 125 | + .build(); |
| 126 | + |
| 127 | + this.authorizationService.save(authorization); |
| 128 | + |
| 129 | + return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken, refreshToken); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean supports(Class<?> authentication) { |
| 134 | + return OAuth2RefreshTokenAuthenticationToken.class.isAssignableFrom(authentication); |
| 135 | + } |
| 136 | +} |
0 commit comments