2020-04-24 03:45:34 +07:00
|
|
|
/*
|
2021-01-18 21:31:06 +07:00
|
|
|
* Copyright 2020-2021 the original author or authors.
|
2020-04-24 03:45:34 +07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.oauth2.server.authorization.authentication;
|
|
|
|
|
2021-02-09 09:14:33 +07:00
|
|
|
import java.security.Principal;
|
2021-01-18 21:31:06 +07:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2021-02-11 17:26:23 +07:00
|
|
|
import java.util.HashSet;
|
2021-01-18 21:31:06 +07:00
|
|
|
import java.util.Map;
|
2021-02-10 16:07:27 +07:00
|
|
|
import java.util.Set;
|
2021-01-18 21:31:06 +07:00
|
|
|
|
2021-02-10 16:07:27 +07:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2020-04-24 03:45:34 +07:00
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
2020-11-28 17:50:12 +07:00
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2Error;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
2021-02-09 23:44:21 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2TokenType;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
2020-12-04 22:02:07 +07:00
|
|
|
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
|
|
|
|
import org.springframework.security.oauth2.core.oidc.OidcScopes;
|
|
|
|
import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.JoseHeader;
|
2020-07-17 00:59:55 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
2020-07-17 00:59:55 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
2020-04-24 03:45:34 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
2020-06-23 02:35:01 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
2021-02-10 16:07:27 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
|
2021-02-06 01:20:17 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.token.JwtEncodingContext;
|
2020-10-23 01:03:24 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.token.OAuth2AuthorizationCode;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.util.Assert;
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
2020-11-04 21:15:13 +07:00
|
|
|
import static org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthenticationProviderUtils.getAuthenticatedClientElseThrowInvalidClient;
|
|
|
|
|
2020-04-24 03:45:34 +07:00
|
|
|
/**
|
2020-06-04 03:08:52 +07:00
|
|
|
* An {@link AuthenticationProvider} implementation for the OAuth 2.0 Authorization Code Grant.
|
|
|
|
*
|
2020-04-24 03:45:34 +07:00
|
|
|
* @author Joe Grandja
|
2020-10-01 02:20:38 +07:00
|
|
|
* @author Daniel Garnier-Moiroux
|
2020-06-04 03:08:52 +07:00
|
|
|
* @since 0.0.1
|
|
|
|
* @see OAuth2AuthorizationCodeAuthenticationToken
|
|
|
|
* @see OAuth2AccessTokenAuthenticationToken
|
|
|
|
* @see OAuth2AuthorizationService
|
2020-07-17 00:59:55 +07:00
|
|
|
* @see JwtEncoder
|
2021-01-18 21:31:06 +07:00
|
|
|
* @see OAuth2TokenCustomizer
|
|
|
|
* @see JwtEncodingContext
|
2020-06-04 03:08:52 +07:00
|
|
|
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1">Section 4.1 Authorization Code Grant</a>
|
|
|
|
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1.3">Section 4.1.3 Access Token Request</a>
|
2020-04-24 03:45:34 +07:00
|
|
|
*/
|
|
|
|
public class OAuth2AuthorizationCodeAuthenticationProvider implements AuthenticationProvider {
|
2021-02-10 16:07:27 +07:00
|
|
|
private static final OAuth2TokenType AUTHORIZATION_CODE_TOKEN_TYPE =
|
|
|
|
new OAuth2TokenType(OAuth2ParameterNames.CODE);
|
|
|
|
private static final OAuth2TokenType ID_TOKEN_TOKEN_TYPE =
|
|
|
|
new OAuth2TokenType(OidcParameterNames.ID_TOKEN);
|
2020-06-04 03:08:52 +07:00
|
|
|
private final OAuth2AuthorizationService authorizationService;
|
2020-07-17 00:59:55 +07:00
|
|
|
private final JwtEncoder jwtEncoder;
|
2021-01-18 21:31:06 +07:00
|
|
|
private OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer = (context) -> {};
|
2021-02-10 16:07:27 +07:00
|
|
|
private ProviderSettings providerSettings;
|
2020-06-04 03:08:52 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an {@code OAuth2AuthorizationCodeAuthenticationProvider} using the provided parameters.
|
|
|
|
*
|
|
|
|
* @param authorizationService the authorization service
|
2020-07-17 00:59:55 +07:00
|
|
|
* @param jwtEncoder the jwt encoder
|
2020-06-04 03:08:52 +07:00
|
|
|
*/
|
2020-12-11 23:53:31 +07:00
|
|
|
public OAuth2AuthorizationCodeAuthenticationProvider(OAuth2AuthorizationService authorizationService, JwtEncoder jwtEncoder) {
|
2020-06-04 03:08:52 +07:00
|
|
|
Assert.notNull(authorizationService, "authorizationService cannot be null");
|
2020-07-17 00:59:55 +07:00
|
|
|
Assert.notNull(jwtEncoder, "jwtEncoder cannot be null");
|
2020-06-04 03:08:52 +07:00
|
|
|
this.authorizationService = authorizationService;
|
2020-07-17 00:59:55 +07:00
|
|
|
this.jwtEncoder = jwtEncoder;
|
2020-06-04 03:08:52 +07:00
|
|
|
}
|
2020-04-24 03:45:34 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
public final void setJwtCustomizer(OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer) {
|
|
|
|
Assert.notNull(jwtCustomizer, "jwtCustomizer cannot be null");
|
|
|
|
this.jwtCustomizer = jwtCustomizer;
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:07:27 +07:00
|
|
|
@Autowired(required = false)
|
|
|
|
protected void setProviderSettings(ProviderSettings providerSettings) {
|
|
|
|
this.providerSettings = providerSettings;
|
|
|
|
}
|
|
|
|
|
2020-04-24 03:45:34 +07:00
|
|
|
@Override
|
|
|
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
2020-06-04 03:08:52 +07:00
|
|
|
OAuth2AuthorizationCodeAuthenticationToken authorizationCodeAuthentication =
|
|
|
|
(OAuth2AuthorizationCodeAuthenticationToken) authentication;
|
|
|
|
|
2020-11-04 21:15:13 +07:00
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal =
|
|
|
|
getAuthenticatedClientElseThrowInvalidClient(authorizationCodeAuthentication);
|
2020-10-02 16:02:52 +07:00
|
|
|
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
|
2020-06-04 03:08:52 +07:00
|
|
|
|
2020-07-02 19:40:48 +07:00
|
|
|
OAuth2Authorization authorization = this.authorizationService.findByToken(
|
2021-02-09 23:44:21 +07:00
|
|
|
authorizationCodeAuthentication.getCode(), AUTHORIZATION_CODE_TOKEN_TYPE);
|
2020-06-04 03:08:52 +07:00
|
|
|
if (authorization == null) {
|
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
|
|
}
|
2021-02-06 01:20:17 +07:00
|
|
|
OAuth2Authorization.Token<OAuth2AuthorizationCode> authorizationCode =
|
|
|
|
authorization.getToken(OAuth2AuthorizationCode.class);
|
2020-06-04 03:08:52 +07:00
|
|
|
|
|
|
|
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(
|
2021-02-09 15:25:21 +07:00
|
|
|
OAuth2AuthorizationRequest.class.getName());
|
2020-06-04 03:08:52 +07:00
|
|
|
|
2020-06-23 02:35:01 +07:00
|
|
|
if (!registeredClient.getClientId().equals(authorizationRequest.getClientId())) {
|
2021-02-06 01:20:17 +07:00
|
|
|
if (!authorizationCode.isInvalidated()) {
|
2020-10-14 14:06:23 +07:00
|
|
|
// Invalidate the authorization code given that a different client is attempting to use it
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, authorizationCode.getToken());
|
2020-10-14 14:06:23 +07:00
|
|
|
this.authorizationService.save(authorization);
|
|
|
|
}
|
2020-06-23 02:35:01 +07:00
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
|
|
}
|
|
|
|
|
2020-10-01 02:20:38 +07:00
|
|
|
if (StringUtils.hasText(authorizationRequest.getRedirectUri()) &&
|
|
|
|
!authorizationRequest.getRedirectUri().equals(authorizationCodeAuthentication.getRedirectUri())) {
|
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
|
|
}
|
2020-06-23 02:35:01 +07:00
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
if (authorizationCode.isInvalidated()) {
|
2020-10-23 01:03:24 +07:00
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:07:27 +07:00
|
|
|
String issuer = this.providerSettings != null ? this.providerSettings.issuer() : null;
|
|
|
|
Set<String> authorizedScopes = authorization.getAttribute(
|
|
|
|
OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME);
|
|
|
|
|
|
|
|
JoseHeader.Builder headersBuilder = JwtUtils.headers();
|
|
|
|
JwtClaimsSet.Builder claimsBuilder = JwtUtils.accessTokenClaims(
|
2021-02-11 17:26:23 +07:00
|
|
|
registeredClient, issuer, authorization.getPrincipalName(),
|
|
|
|
excludeOpenidIfNecessary(authorizedScopes));
|
2021-02-10 16:07:27 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
// @formatter:off
|
2021-02-10 16:07:27 +07:00
|
|
|
JwtEncodingContext context = JwtEncodingContext.with(headersBuilder, claimsBuilder)
|
|
|
|
.registeredClient(registeredClient)
|
2021-02-09 09:14:33 +07:00
|
|
|
.principal(authorization.getAttribute(Principal.class.getName()))
|
2021-02-10 16:07:27 +07:00
|
|
|
.authorization(authorization)
|
2021-02-11 08:26:01 +07:00
|
|
|
.authorizedScopes(authorizedScopes)
|
2021-02-10 16:07:27 +07:00
|
|
|
.tokenType(OAuth2TokenType.ACCESS_TOKEN)
|
2021-01-18 21:31:06 +07:00
|
|
|
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
|
.authorizationGrant(authorizationCodeAuthentication)
|
|
|
|
.build();
|
|
|
|
// @formatter:on
|
2021-02-10 16:07:27 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
this.jwtCustomizer.customize(context);
|
2020-07-17 00:59:55 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
JoseHeader headers = context.getHeaders().build();
|
|
|
|
JwtClaimsSet claims = context.getClaims().build();
|
2021-02-09 08:33:17 +07:00
|
|
|
Jwt jwtAccessToken = this.jwtEncoder.encode(headers, claims);
|
2021-01-18 21:31:06 +07:00
|
|
|
|
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
2021-02-09 08:33:17 +07:00
|
|
|
jwtAccessToken.getTokenValue(), jwtAccessToken.getIssuedAt(),
|
2021-02-11 17:26:23 +07:00
|
|
|
jwtAccessToken.getExpiresAt(), excludeOpenidIfNecessary(authorizedScopes));
|
2020-07-17 00:59:55 +07:00
|
|
|
|
2020-10-07 16:53:37 +07:00
|
|
|
OAuth2RefreshToken refreshToken = null;
|
2020-12-09 01:53:32 +07:00
|
|
|
if (registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.REFRESH_TOKEN)) {
|
2021-01-18 21:31:06 +07:00
|
|
|
refreshToken = OAuth2RefreshTokenAuthenticationProvider.generateRefreshToken(
|
|
|
|
registeredClient.getTokenSettings().refreshTokenTimeToLive());
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
2020-06-04 03:08:52 +07:00
|
|
|
|
2021-02-09 08:33:17 +07:00
|
|
|
Jwt jwtIdToken = null;
|
2020-12-04 22:02:07 +07:00
|
|
|
if (authorizationRequest.getScopes().contains(OidcScopes.OPENID)) {
|
2021-02-10 16:07:27 +07:00
|
|
|
String nonce = (String) authorizationRequest.getAdditionalParameters().get(OidcParameterNames.NONCE);
|
|
|
|
|
|
|
|
headersBuilder = JwtUtils.headers();
|
|
|
|
claimsBuilder = JwtUtils.idTokenClaims(
|
|
|
|
registeredClient, issuer, authorization.getPrincipalName(), nonce);
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
// @formatter:off
|
2021-02-10 16:07:27 +07:00
|
|
|
context = JwtEncodingContext.with(headersBuilder, claimsBuilder)
|
|
|
|
.registeredClient(registeredClient)
|
2021-02-09 09:14:33 +07:00
|
|
|
.principal(authorization.getAttribute(Principal.class.getName()))
|
2021-02-10 16:07:27 +07:00
|
|
|
.authorization(authorization)
|
2021-02-11 08:26:01 +07:00
|
|
|
.authorizedScopes(authorizedScopes)
|
2021-02-10 16:07:27 +07:00
|
|
|
.tokenType(ID_TOKEN_TOKEN_TYPE)
|
2021-01-18 21:31:06 +07:00
|
|
|
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
|
.authorizationGrant(authorizationCodeAuthentication)
|
|
|
|
.build();
|
|
|
|
// @formatter:on
|
2021-02-10 16:07:27 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
this.jwtCustomizer.customize(context);
|
|
|
|
|
|
|
|
headers = context.getHeaders().build();
|
|
|
|
claims = context.getClaims().build();
|
2021-02-09 08:33:17 +07:00
|
|
|
jwtIdToken = this.jwtEncoder.encode(headers, claims);
|
|
|
|
}
|
2021-01-18 21:31:06 +07:00
|
|
|
|
2021-02-09 08:33:17 +07:00
|
|
|
OidcIdToken idToken;
|
|
|
|
if (jwtIdToken != null) {
|
2020-12-04 22:02:07 +07:00
|
|
|
idToken = new OidcIdToken(jwtIdToken.getTokenValue(), jwtIdToken.getIssuedAt(),
|
|
|
|
jwtIdToken.getExpiresAt(), jwtIdToken.getClaims());
|
2021-02-09 08:33:17 +07:00
|
|
|
} else {
|
|
|
|
idToken = null;
|
2020-12-04 22:02:07 +07:00
|
|
|
}
|
|
|
|
|
2021-02-09 08:33:17 +07:00
|
|
|
// @formatter:off
|
2021-02-06 01:20:17 +07:00
|
|
|
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization.from(authorization)
|
2021-02-09 08:33:17 +07:00
|
|
|
.token(accessToken,
|
|
|
|
(metadata) ->
|
|
|
|
metadata.put(OAuth2Authorization.Token.CLAIMS_METADATA_NAME, jwtAccessToken.getClaims())
|
|
|
|
);
|
2021-02-06 01:20:17 +07:00
|
|
|
if (refreshToken != null) {
|
|
|
|
authorizationBuilder.refreshToken(refreshToken);
|
|
|
|
}
|
|
|
|
if (idToken != null) {
|
2021-02-09 08:33:17 +07:00
|
|
|
authorizationBuilder
|
|
|
|
.token(idToken,
|
|
|
|
(metadata) ->
|
|
|
|
metadata.put(OAuth2Authorization.Token.CLAIMS_METADATA_NAME, idToken.getClaims()));
|
2021-02-06 01:20:17 +07:00
|
|
|
}
|
|
|
|
authorization = authorizationBuilder.build();
|
2021-02-09 08:33:17 +07:00
|
|
|
// @formatter:on
|
2020-10-14 14:06:23 +07:00
|
|
|
|
|
|
|
// Invalidate the authorization code as it can only be used once
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, authorizationCode.getToken());
|
2020-10-14 14:06:23 +07:00
|
|
|
|
2020-06-04 03:08:52 +07:00
|
|
|
this.authorizationService.save(authorization);
|
|
|
|
|
2020-12-04 22:02:07 +07:00
|
|
|
Map<String, Object> additionalParameters = Collections.emptyMap();
|
|
|
|
if (idToken != null) {
|
|
|
|
additionalParameters = new HashMap<>();
|
|
|
|
additionalParameters.put(OidcParameterNames.ID_TOKEN, idToken.getTokenValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OAuth2AccessTokenAuthenticationToken(
|
|
|
|
registeredClient, clientPrincipal, accessToken, refreshToken, additionalParameters);
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:26:23 +07:00
|
|
|
private static Set<String> excludeOpenidIfNecessary(Set<String> scopes) {
|
|
|
|
if (!scopes.contains(OidcScopes.OPENID)) {
|
|
|
|
return scopes;
|
|
|
|
}
|
|
|
|
scopes = new HashSet<>(scopes);
|
|
|
|
scopes.remove(OidcScopes.OPENID);
|
|
|
|
return scopes;
|
|
|
|
}
|
|
|
|
|
2020-04-24 03:45:34 +07:00
|
|
|
@Override
|
|
|
|
public boolean supports(Class<?> authentication) {
|
|
|
|
return OAuth2AuthorizationCodeAuthenticationToken.class.isAssignableFrom(authentication);
|
|
|
|
}
|
2020-11-28 17:50:12 +07:00
|
|
|
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|