211 lines
9.8 KiB
Java
211 lines
9.8 KiB
Java
/*
|
|
* Copyright 2020 the original author or authors.
|
|
*
|
|
* 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;
|
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
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;
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
|
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
|
|
import org.springframework.security.oauth2.jose.JoseHeader;
|
|
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
|
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationAttributeNames;
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
import org.springframework.security.oauth2.server.authorization.TokenType;
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.URI;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.time.Instant;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.Base64;
|
|
import java.util.Collections;
|
|
|
|
/**
|
|
* An {@link AuthenticationProvider} implementation for the OAuth 2.0 Authorization Code Grant.
|
|
*
|
|
* @author Joe Grandja
|
|
* @author Daniel Garnier-Moiroux
|
|
* @since 0.0.1
|
|
* @see OAuth2AuthorizationCodeAuthenticationToken
|
|
* @see OAuth2AccessTokenAuthenticationToken
|
|
* @see RegisteredClientRepository
|
|
* @see OAuth2AuthorizationService
|
|
* @see JwtEncoder
|
|
* @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>
|
|
*/
|
|
public class OAuth2AuthorizationCodeAuthenticationProvider implements AuthenticationProvider {
|
|
private final RegisteredClientRepository registeredClientRepository;
|
|
private final OAuth2AuthorizationService authorizationService;
|
|
private final JwtEncoder jwtEncoder;
|
|
|
|
/**
|
|
* Constructs an {@code OAuth2AuthorizationCodeAuthenticationProvider} using the provided parameters.
|
|
*
|
|
* @param registeredClientRepository the repository of registered clients
|
|
* @param authorizationService the authorization service
|
|
* @param jwtEncoder the jwt encoder
|
|
*/
|
|
public OAuth2AuthorizationCodeAuthenticationProvider(RegisteredClientRepository registeredClientRepository,
|
|
OAuth2AuthorizationService authorizationService, JwtEncoder jwtEncoder) {
|
|
Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null");
|
|
Assert.notNull(authorizationService, "authorizationService cannot be null");
|
|
Assert.notNull(jwtEncoder, "jwtEncoder cannot be null");
|
|
this.registeredClientRepository = registeredClientRepository;
|
|
this.authorizationService = authorizationService;
|
|
this.jwtEncoder = jwtEncoder;
|
|
}
|
|
|
|
@Override
|
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
|
OAuth2AuthorizationCodeAuthenticationToken authorizationCodeAuthentication =
|
|
(OAuth2AuthorizationCodeAuthenticationToken) authentication;
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = null;
|
|
RegisteredClient registeredClient;
|
|
if (OAuth2ClientAuthenticationToken.class.isAssignableFrom(authorizationCodeAuthentication.getPrincipal().getClass())) {
|
|
clientPrincipal = (OAuth2ClientAuthenticationToken) authorizationCodeAuthentication.getPrincipal();
|
|
if (!clientPrincipal.isAuthenticated()) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
|
|
}
|
|
registeredClient = clientPrincipal.getRegisteredClient();
|
|
} else if (StringUtils.hasText(authorizationCodeAuthentication.getClientId())) {
|
|
String clientId = authorizationCodeAuthentication.getClientId();
|
|
registeredClient = this.registeredClientRepository.findByClientId(clientId);
|
|
if (registeredClient == null) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
|
|
}
|
|
} else {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
|
|
}
|
|
|
|
OAuth2Authorization authorization = this.authorizationService.findByToken(
|
|
authorizationCodeAuthentication.getCode(), TokenType.AUTHORIZATION_CODE);
|
|
if (authorization == null) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
}
|
|
|
|
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(
|
|
OAuth2AuthorizationAttributeNames.AUTHORIZATION_REQUEST);
|
|
|
|
if (!registeredClient.getClientId().equals(authorizationRequest.getClientId())) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
}
|
|
|
|
if (StringUtils.hasText(authorizationRequest.getRedirectUri()) &&
|
|
!authorizationRequest.getRedirectUri().equals(authorizationCodeAuthentication.getRedirectUri())) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
}
|
|
|
|
// Validate PKCE parameters
|
|
String codeChallenge = (String) authorizationRequest
|
|
.getAdditionalParameters()
|
|
.get(PkceParameterNames.CODE_CHALLENGE);
|
|
if (StringUtils.hasText(codeChallenge)) {
|
|
String codeChallengeMethod = (String) authorizationRequest
|
|
.getAdditionalParameters()
|
|
.get(PkceParameterNames.CODE_CHALLENGE_METHOD);
|
|
|
|
String codeVerifier = (String) authorizationCodeAuthentication
|
|
.getAdditionalParameters()
|
|
.get(PkceParameterNames.CODE_VERIFIER);
|
|
|
|
if (!pkceCodeVerifierValid(codeVerifier, codeChallenge, codeChallengeMethod)) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
}
|
|
} else if (registeredClient.getClientSettings().requireProofKey()) {
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_GRANT));
|
|
}
|
|
|
|
JoseHeader joseHeader = JoseHeader.withAlgorithm(SignatureAlgorithm.RS256).build();
|
|
|
|
// TODO Allow configuration for issuer claim
|
|
URL issuer = null;
|
|
try {
|
|
issuer = URI.create("https://oauth2.provider.com").toURL();
|
|
} catch (MalformedURLException e) { }
|
|
|
|
Instant issuedAt = Instant.now();
|
|
Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS); // TODO Allow configuration for access token time-to-live
|
|
|
|
JwtClaimsSet jwtClaimsSet = JwtClaimsSet.withClaims()
|
|
.issuer(issuer)
|
|
.subject(authorization.getPrincipalName())
|
|
.audience(Collections.singletonList(registeredClient.getClientId()))
|
|
.issuedAt(issuedAt)
|
|
.expiresAt(expiresAt)
|
|
.notBefore(issuedAt)
|
|
.claim(OAuth2ParameterNames.SCOPE, authorizationRequest.getScopes())
|
|
.build();
|
|
|
|
Jwt jwt = this.jwtEncoder.encode(joseHeader, jwtClaimsSet);
|
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getClaim(OAuth2ParameterNames.SCOPE));
|
|
|
|
authorization = OAuth2Authorization.from(authorization)
|
|
.attribute(OAuth2AuthorizationAttributeNames.ACCESS_TOKEN_ATTRIBUTES, jwt)
|
|
.accessToken(accessToken)
|
|
.build();
|
|
this.authorizationService.save(authorization);
|
|
|
|
return clientPrincipal != null ?
|
|
new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken) :
|
|
new OAuth2AccessTokenAuthenticationToken(registeredClient, new OAuth2ClientAuthenticationToken(registeredClient), accessToken);
|
|
}
|
|
|
|
private static boolean pkceCodeVerifierValid(String codeVerifier, String codeChallenge, String codeChallengeMethod) {
|
|
if (!StringUtils.hasText(codeVerifier)) {
|
|
return false;
|
|
} else if (!StringUtils.hasText(codeChallengeMethod) || "plain".equals(codeChallengeMethod)) {
|
|
return codeVerifier.equals(codeChallenge);
|
|
} else if ("S256".equals(codeChallengeMethod)) {
|
|
try {
|
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
byte[] digest = md.digest(codeVerifier.getBytes(StandardCharsets.US_ASCII));
|
|
String encodedVerifier = Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
|
|
return encodedVerifier.equals(codeChallenge);
|
|
} catch (NoSuchAlgorithmException ex) {
|
|
// It is unlikely that SHA-256 is not available on the server. If it is not available,
|
|
// there will likely be bigger issues as well. We default to SERVER_ERROR.
|
|
}
|
|
}
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR));
|
|
}
|
|
|
|
@Override
|
|
public boolean supports(Class<?> authentication) {
|
|
return OAuth2AuthorizationCodeAuthenticationToken.class.isAssignableFrom(authentication);
|
|
}
|
|
}
|