2020-06-11 20:54:01 +07:00
|
|
|
/*
|
2021-01-15 17:57:15 +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.time.Instant;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.mockito.ArgumentCaptor;
|
2021-01-18 21:31:06 +07:00
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.core.Authentication;
|
2020-11-03 06:47:55 +07:00
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
2020-10-24 06:41:17 +07:00
|
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
2020-06-11 20:54:01 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
2021-02-11 02:39:25 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2TokenType;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
2020-07-17 00:59:55 +07:00
|
|
|
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.JoseHeaderNames;
|
2020-07-17 00:59:55 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
|
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
2020-06-11 20:54:01 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
2021-02-05 18:30:12 +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
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
2020-07-17 00:59:55 +07:00
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
2020-07-10 20:16:19 +07:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
2020-07-17 00:59:55 +07:00
|
|
|
import static org.mockito.Mockito.when;
|
2020-06-11 20:54:01 +07:00
|
|
|
|
|
|
|
/**
|
2020-07-10 20:16:19 +07:00
|
|
|
* Tests for {@link OAuth2ClientCredentialsAuthenticationProvider}.
|
|
|
|
*
|
2020-06-11 20:54:01 +07:00
|
|
|
* @author Alexey Nesterov
|
2020-07-10 20:16:19 +07:00
|
|
|
* @author Joe Grandja
|
2020-06-11 20:54:01 +07:00
|
|
|
*/
|
|
|
|
public class OAuth2ClientCredentialsAuthenticationProviderTests {
|
2020-07-10 20:16:19 +07:00
|
|
|
private OAuth2AuthorizationService authorizationService;
|
2020-07-17 00:59:55 +07:00
|
|
|
private JwtEncoder jwtEncoder;
|
2021-01-18 21:31:06 +07:00
|
|
|
private OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer;
|
2020-06-11 20:54:01 +07:00
|
|
|
private OAuth2ClientCredentialsAuthenticationProvider authenticationProvider;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
2020-07-10 20:16:19 +07:00
|
|
|
this.authorizationService = mock(OAuth2AuthorizationService.class);
|
2020-07-17 00:59:55 +07:00
|
|
|
this.jwtEncoder = mock(JwtEncoder.class);
|
|
|
|
this.authenticationProvider = new OAuth2ClientCredentialsAuthenticationProvider(
|
|
|
|
this.authorizationService, this.jwtEncoder);
|
2021-01-18 21:31:06 +07:00
|
|
|
this.jwtCustomizer = mock(OAuth2TokenCustomizer.class);
|
|
|
|
this.authenticationProvider.setJwtCustomizer(this.jwtCustomizer);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void constructorWhenAuthorizationServiceNullThenThrowIllegalArgumentException() {
|
2020-07-17 00:59:55 +07:00
|
|
|
assertThatThrownBy(() -> new OAuth2ClientCredentialsAuthenticationProvider(null, this.jwtEncoder))
|
2020-07-10 20:16:19 +07:00
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("authorizationService cannot be null");
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:59:55 +07:00
|
|
|
@Test
|
|
|
|
public void constructorWhenJwtEncoderNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> new OAuth2ClientCredentialsAuthenticationProvider(this.authorizationService, null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("jwtEncoder cannot be null");
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
@Test
|
|
|
|
public void setJwtCustomizerWhenNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.setJwtCustomizer(null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("jwtCustomizer cannot be null");
|
|
|
|
}
|
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void supportsWhenSupportedAuthenticationThenTrue() {
|
|
|
|
assertThat(this.authenticationProvider.supports(OAuth2ClientCredentialsAuthenticationToken.class)).isTrue();
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void supportsWhenUnsupportedAuthenticationThenFalse() {
|
|
|
|
assertThat(this.authenticationProvider.supports(OAuth2AuthorizationCodeAuthenticationToken.class)).isFalse();
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void authenticateWhenClientPrincipalNotOAuth2ClientAuthenticationTokenThenThrowOAuth2AuthenticationException() {
|
2020-11-03 06:47:55 +07:00
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
|
2020-07-10 20:16:19 +07:00
|
|
|
TestingAuthenticationToken clientPrincipal = new TestingAuthenticationToken(
|
2020-11-03 06:47:55 +07:00
|
|
|
registeredClient.getClientId(), registeredClient.getClientSecret());
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal);
|
2020-06-11 20:54:01 +07:00
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void authenticateWhenClientPrincipalNotAuthenticatedThenThrowOAuth2AuthenticationException() {
|
2020-11-03 06:47:55 +07:00
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
2020-10-24 06:41:17 +07:00
|
|
|
registeredClient.getClientId(), registeredClient.getClientSecret(), ClientAuthenticationMethod.BASIC, null);
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal);
|
2020-06-11 20:54:01 +07:00
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
2020-07-10 20:16:19 +07:00
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
2020-11-03 06:47:55 +07:00
|
|
|
@Test
|
|
|
|
public void authenticateWhenClientNotAuthorizedToRequestTokenThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2()
|
|
|
|
.authorizationGrantTypes(grantTypes -> grantTypes.remove(AuthorizationGrantType.CLIENT_CREDENTIALS))
|
|
|
|
.build();
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal);
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT);
|
|
|
|
}
|
|
|
|
|
2020-06-11 20:54:01 +07:00
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void authenticateWhenInvalidScopeThenThrowOAuth2AuthenticationException() {
|
2020-11-03 06:47:55 +07:00
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
2020-06-11 20:54:01 +07:00
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken(
|
2020-07-10 20:16:19 +07:00
|
|
|
clientPrincipal, Collections.singleton("invalid-scope"));
|
2020-06-11 20:54:01 +07:00
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
2020-07-10 20:16:19 +07:00
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_SCOPE);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void authenticateWhenScopeRequestedThenAccessTokenContainsScope() {
|
2020-11-03 06:47:55 +07:00
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
2020-12-04 22:02:07 +07:00
|
|
|
Set<String> requestedScope = Collections.singleton("scope1");
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication =
|
|
|
|
new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal, requestedScope);
|
|
|
|
|
2021-02-11 02:39:25 +07:00
|
|
|
when(this.jwtEncoder.encode(any(), any()))
|
|
|
|
.thenReturn(createJwt(Collections.singleton("mapped-scoped")));
|
2020-07-17 00:59:55 +07:00
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
assertThat(accessTokenAuthentication.getAccessToken().getScopes()).isEqualTo(requestedScope);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
@Test
|
|
|
|
public void authenticateWhenValidAuthenticationThenReturnAccessToken() {
|
2020-11-03 06:47:55 +07:00
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal);
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
when(this.jwtEncoder.encode(any(), any())).thenReturn(createJwt(registeredClient.getScopes()));
|
2020-07-17 00:59:55 +07:00
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
ArgumentCaptor<JwtEncodingContext> jwtEncodingContextCaptor = ArgumentCaptor.forClass(JwtEncodingContext.class);
|
|
|
|
verify(this.jwtCustomizer).customize(jwtEncodingContextCaptor.capture());
|
|
|
|
JwtEncodingContext jwtEncodingContext = jwtEncodingContextCaptor.getValue();
|
|
|
|
assertThat(jwtEncodingContext.getRegisteredClient()).isEqualTo(registeredClient);
|
|
|
|
assertThat(jwtEncodingContext.<Authentication>getPrincipal()).isEqualTo(clientPrincipal);
|
|
|
|
assertThat(jwtEncodingContext.getAuthorization()).isNull();
|
2021-02-09 23:44:21 +07:00
|
|
|
assertThat(jwtEncodingContext.getTokenType()).isEqualTo(OAuth2TokenType.ACCESS_TOKEN);
|
2021-01-18 21:31:06 +07:00
|
|
|
assertThat(jwtEncodingContext.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.CLIENT_CREDENTIALS);
|
2021-02-05 18:30:12 +07:00
|
|
|
assertThat(jwtEncodingContext.<OAuth2AuthorizationGrantAuthenticationToken>getAuthorizationGrant()).isEqualTo(authentication);
|
2021-01-18 21:31:06 +07:00
|
|
|
assertThat(jwtEncodingContext.getHeaders()).isNotNull();
|
|
|
|
assertThat(jwtEncodingContext.getClaims()).isNotNull();
|
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
|
verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
OAuth2Authorization authorization = authorizationCaptor.getValue();
|
|
|
|
|
|
|
|
assertThat(authorization.getRegisteredClientId()).isEqualTo(clientPrincipal.getRegisteredClient().getId());
|
|
|
|
assertThat(authorization.getPrincipalName()).isEqualTo(clientPrincipal.getName());
|
2021-02-09 02:57:15 +07:00
|
|
|
assertThat(authorization.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.CLIENT_CREDENTIALS);
|
2021-02-06 01:20:17 +07:00
|
|
|
assertThat(authorization.getAccessToken()).isNotNull();
|
2021-02-11 07:37:14 +07:00
|
|
|
assertThat(authorization.<Set<String>>getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME)).isNotNull();
|
|
|
|
assertThat(authorization.getAccessToken().getToken().getScopes())
|
|
|
|
.isEqualTo(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME));
|
2020-07-10 20:16:19 +07:00
|
|
|
assertThat(accessTokenAuthentication.getPrincipal()).isEqualTo(clientPrincipal);
|
2021-02-06 01:20:17 +07:00
|
|
|
assertThat(accessTokenAuthentication.getAccessToken()).isEqualTo(authorization.getAccessToken().getToken());
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
2020-07-17 00:59:55 +07:00
|
|
|
|
2021-01-18 21:31:06 +07:00
|
|
|
private static Jwt createJwt(Set<String> scope) {
|
2020-07-17 00:59:55 +07:00
|
|
|
Instant issuedAt = Instant.now();
|
|
|
|
Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS);
|
|
|
|
return Jwt.withTokenValue("token")
|
|
|
|
.header(JoseHeaderNames.ALG, SignatureAlgorithm.RS256.getName())
|
|
|
|
.issuedAt(issuedAt)
|
|
|
|
.expiresAt(expiresAt)
|
2021-01-18 21:31:06 +07:00
|
|
|
.claim(OAuth2ParameterNames.SCOPE, scope)
|
2020-07-17 00:59:55 +07:00
|
|
|
.build();
|
|
|
|
}
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|