2020-06-11 20:54:01 +07:00
|
|
|
/*
|
2021-01-18 21:31:06 +07:00
|
|
|
* Copyright 2020-2021 the original author or authors.
|
2020-06-11 20:54:01 +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.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
2020-11-03 06:47:55 +07:00
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
2020-06-11 20:54:01 +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;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
|
|
|
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-07-10 20:16:19 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
2020-07-17 00:59:55 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationAttributeNames;
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
|
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;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.springframework.util.Assert;
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
2020-11-04 21:15:13 +07:00
|
|
|
import static org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthenticationProviderUtils.getAuthenticatedClientElseThrowInvalidClient;
|
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
/**
|
|
|
|
* An {@link AuthenticationProvider} implementation for the OAuth 2.0 Client Credentials Grant.
|
|
|
|
*
|
|
|
|
* @author Alexey Nesterov
|
2021-01-18 21:31:06 +07:00
|
|
|
* @author Joe Grandja
|
2020-06-11 20:54:01 +07:00
|
|
|
* @since 0.0.1
|
|
|
|
* @see OAuth2ClientCredentialsAuthenticationToken
|
2020-07-10 20:16:19 +07:00
|
|
|
* @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-11 20:54:01 +07:00
|
|
|
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.4">Section 4.4 Client Credentials Grant</a>
|
|
|
|
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.4.2">Section 4.4.2 Access Token Request</a>
|
|
|
|
*/
|
|
|
|
public class OAuth2ClientCredentialsAuthenticationProvider implements AuthenticationProvider {
|
2020-07-10 20:16:19 +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-11 20:54:01 +07:00
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
/**
|
|
|
|
* Constructs an {@code OAuth2ClientCredentialsAuthenticationProvider} using the provided parameters.
|
|
|
|
*
|
|
|
|
* @param authorizationService the authorization service
|
2020-07-17 00:59:55 +07:00
|
|
|
* @param jwtEncoder the jwt encoder
|
2020-07-10 20:16:19 +07:00
|
|
|
*/
|
2020-07-17 00:59:55 +07:00
|
|
|
public OAuth2ClientCredentialsAuthenticationProvider(OAuth2AuthorizationService authorizationService,
|
|
|
|
JwtEncoder jwtEncoder) {
|
2020-07-10 20:16:19 +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-07-10 20:16:19 +07:00
|
|
|
this.authorizationService = authorizationService;
|
2020-07-17 00:59:55 +07:00
|
|
|
this.jwtEncoder = jwtEncoder;
|
2020-07-10 20:16:19 +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-06-11 20:54:01 +07:00
|
|
|
@Override
|
|
|
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2ClientCredentialsAuthenticationToken clientCredentialsAuthentication =
|
2020-06-11 20:54:01 +07:00
|
|
|
(OAuth2ClientCredentialsAuthenticationToken) authentication;
|
|
|
|
|
2020-11-04 21:15:13 +07:00
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal =
|
|
|
|
getAuthenticatedClientElseThrowInvalidClient(clientCredentialsAuthentication);
|
2020-07-10 20:16:19 +07:00
|
|
|
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
|
2020-06-11 20:54:01 +07:00
|
|
|
|
2020-11-03 06:47:55 +07:00
|
|
|
if (!registeredClient.getAuthorizationGrantTypes().contains(AuthorizationGrantType.CLIENT_CREDENTIALS)) {
|
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT));
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
Set<String> scopes = registeredClient.getScopes(); // Default to configured scopes
|
|
|
|
if (!CollectionUtils.isEmpty(clientCredentialsAuthentication.getScopes())) {
|
|
|
|
Set<String> unauthorizedScopes = clientCredentialsAuthentication.getScopes().stream()
|
|
|
|
.filter(requestedScope -> !registeredClient.getScopes().contains(requestedScope))
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
if (!CollectionUtils.isEmpty(unauthorizedScopes)) {
|
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE));
|
|
|
|
}
|
|
|
|
scopes = new LinkedHashSet<>(clientCredentialsAuthentication.getScopes());
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
// @formatter:off
|
|
|
|
JwtEncodingContext context = JwtEncodingContextUtils.accessTokenContext(registeredClient, clientPrincipal.getName(), scopes)
|
|
|
|
.principal(clientPrincipal)
|
|
|
|
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
|
|
|
|
.authorizationGrant(clientCredentialsAuthentication)
|
|
|
|
.build();
|
|
|
|
// @formatter:on
|
|
|
|
this.jwtCustomizer.customize(context);
|
|
|
|
|
|
|
|
JoseHeader headers = context.getHeaders().build();
|
|
|
|
JwtClaimsSet claims = context.getClaims().build();
|
|
|
|
Jwt jwt = this.jwtEncoder.encode(headers, claims);
|
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
2021-01-18 21:31:06 +07:00
|
|
|
jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getClaim(OAuth2ParameterNames.SCOPE));
|
2020-07-10 20:16:19 +07:00
|
|
|
|
|
|
|
OAuth2Authorization authorization = OAuth2Authorization.withRegisteredClient(registeredClient)
|
|
|
|
.principalName(clientPrincipal.getName())
|
2021-02-06 01:20:17 +07:00
|
|
|
.token(accessToken)
|
2020-10-30 07:45:11 +07:00
|
|
|
.attribute(OAuth2AuthorizationAttributeNames.ACCESS_TOKEN_ATTRIBUTES, jwt)
|
2020-07-10 20:16:19 +07:00
|
|
|
.build();
|
|
|
|
this.authorizationService.save(authorization);
|
2020-06-11 20:54:01 +07:00
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
return new OAuth2AccessTokenAuthenticationToken(registeredClient, clientPrincipal, accessToken);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supports(Class<?> authentication) {
|
|
|
|
return OAuth2ClientCredentialsAuthenticationToken.class.isAssignableFrom(authentication);
|
|
|
|
}
|
|
|
|
}
|