2020-06-11 20:54:01 +07:00
|
|
|
/*
|
|
|
|
* 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.junit.Before;
|
|
|
|
import org.junit.Test;
|
2020-07-10 20:16:19 +07:00
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
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;
|
|
|
|
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;
|
|
|
|
|
2020-07-10 20:16:19 +07:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Set;
|
|
|
|
|
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-10 20:16:19 +07:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
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 RegisteredClient registeredClient;
|
|
|
|
private OAuth2AuthorizationService authorizationService;
|
2020-06-11 20:54:01 +07:00
|
|
|
private OAuth2ClientCredentialsAuthenticationProvider authenticationProvider;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
2020-07-10 20:16:19 +07:00
|
|
|
this.registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
this.authorizationService = mock(OAuth2AuthorizationService.class);
|
|
|
|
this.authenticationProvider = new OAuth2ClientCredentialsAuthenticationProvider(this.authorizationService);
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void constructorWhenAuthorizationServiceNullThenThrowIllegalArgumentException() {
|
|
|
|
assertThatThrownBy(() -> new OAuth2ClientCredentialsAuthenticationProvider(null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.hasMessage("authorizationService 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() {
|
|
|
|
TestingAuthenticationToken clientPrincipal = new TestingAuthenticationToken(
|
|
|
|
this.registeredClient.getClientId(), this.registeredClient.getClientSecret());
|
|
|
|
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() {
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
|
|
|
this.registeredClient.getClientId(), this.registeredClient.getClientSecret());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-07-10 20:16:19 +07:00
|
|
|
public void authenticateWhenInvalidScopeThenThrowOAuth2AuthenticationException() {
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(this.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() {
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(this.registeredClient);
|
|
|
|
Set<String> requestedScope = Collections.singleton("openid");
|
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication =
|
|
|
|
new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal, requestedScope);
|
|
|
|
|
|
|
|
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() {
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(this.registeredClient);
|
|
|
|
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken(clientPrincipal);
|
|
|
|
|
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
|
|
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());
|
|
|
|
assertThat(authorization.getAccessToken()).isNotNull();
|
|
|
|
assertThat(authorization.getAccessToken().getScopes()).isEqualTo(clientPrincipal.getRegisteredClient().getScopes());
|
|
|
|
assertThat(accessTokenAuthentication.getPrincipal()).isEqualTo(clientPrincipal);
|
|
|
|
assertThat(accessTokenAuthentication.getAccessToken()).isEqualTo(authorization.getAccessToken());
|
2020-06-11 20:54:01 +07:00
|
|
|
}
|
|
|
|
}
|