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-01-18 21:31:06 +07:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
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;
|
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;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationAttributeNames;
|
2020-04-24 03:45:34 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
2020-06-04 03:08:52 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.TokenType;
|
2020-06-23 02:35:01 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
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 {
|
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) -> {};
|
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;
|
|
|
|
}
|
|
|
|
|
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(
|
2020-06-04 03:08:52 +07:00
|
|
|
authorizationCodeAuthentication.getCode(), TokenType.AUTHORIZATION_CODE);
|
|
|
|
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(
|
|
|
|
OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST);
|
|
|
|
|
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-01-18 21:31:06 +07:00
|
|
|
// @formatter:off
|
|
|
|
JwtEncodingContext context = JwtEncodingContextUtils.accessTokenContext(registeredClient, authorization)
|
|
|
|
.principal(authorization.getAttribute(OAuth2AuthorizationAttributeNames.PRINCIPAL))
|
|
|
|
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
|
.authorizationGrant(authorizationCodeAuthentication)
|
|
|
|
.build();
|
|
|
|
// @formatter:on
|
|
|
|
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();
|
|
|
|
Jwt jwt = this.jwtEncoder.encode(headers, claims);
|
|
|
|
|
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
|
|
jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getClaim(OAuth2ParameterNames.SCOPE));
|
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
|
|
|
|
2020-12-04 22:02:07 +07:00
|
|
|
OidcIdToken idToken = null;
|
|
|
|
if (authorizationRequest.getScopes().contains(OidcScopes.OPENID)) {
|
2021-01-18 21:31:06 +07:00
|
|
|
// @formatter:off
|
|
|
|
context = JwtEncodingContextUtils.idTokenContext(registeredClient, authorization)
|
|
|
|
.principal(authorization.getAttribute(OAuth2AuthorizationAttributeNames.PRINCIPAL))
|
|
|
|
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
|
.authorizationGrant(authorizationCodeAuthentication)
|
|
|
|
.build();
|
|
|
|
// @formatter:on
|
|
|
|
this.jwtCustomizer.customize(context);
|
|
|
|
|
|
|
|
headers = context.getHeaders().build();
|
|
|
|
claims = context.getClaims().build();
|
|
|
|
Jwt jwtIdToken = this.jwtEncoder.encode(headers, claims);
|
|
|
|
|
2020-12-04 22:02:07 +07:00
|
|
|
idToken = new OidcIdToken(jwtIdToken.getTokenValue(), jwtIdToken.getIssuedAt(),
|
|
|
|
jwtIdToken.getExpiresAt(), jwtIdToken.getClaims());
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization.from(authorization)
|
|
|
|
.accessToken(accessToken)
|
|
|
|
.attribute(OAuth2AuthorizationAttributeNames.ACCESS_TOKEN_ATTRIBUTES, jwt);
|
|
|
|
if (refreshToken != null) {
|
|
|
|
authorizationBuilder.refreshToken(refreshToken);
|
|
|
|
}
|
|
|
|
if (idToken != null) {
|
|
|
|
authorizationBuilder.token(idToken);
|
|
|
|
}
|
|
|
|
authorization = authorizationBuilder.build();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|