2020-10-07 16:53:37 +07:00
|
|
|
/*
|
2021-01-15 17:57:15 +07:00
|
|
|
* Copyright 2020-2021 the original author or authors.
|
2020-10-07 16:53:37 +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-02-09 09:14:33 +07:00
|
|
|
import java.security.Principal;
|
2021-01-18 21:31:06 +07:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2020-10-07 16:53:37 +07:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.mockito.ArgumentCaptor;
|
2021-01-18 21:31:06 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.core.Authentication;
|
2020-10-31 01:22:47 +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-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
2020-11-10 01:38:33 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken2;
|
2021-01-18 21:31:06 +07:00
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
2020-10-07 16:53:37 +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-10-07 16:53:37 +07:00
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
|
import org.springframework.security.oauth2.jwt.JwtEncoder;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
|
2021-02-09 23:44:21 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2TokenType;
|
2020-10-07 16:53:37 +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-10-07 16:53:37 +07:00
|
|
|
|
|
|
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
|
|
|
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
2020-10-31 01:22:47 +07:00
|
|
|
import static org.mockito.ArgumentMatchers.eq;
|
2020-10-07 16:53:37 +07:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
/**
|
2020-10-31 01:22:47 +07:00
|
|
|
* Tests for {@link OAuth2RefreshTokenAuthenticationProvider}.
|
|
|
|
*
|
2020-10-07 16:53:37 +07:00
|
|
|
* @author Alexey Nesterov
|
2021-01-18 21:31:06 +07:00
|
|
|
* @author Joe Grandja
|
2020-10-07 16:53:37 +07:00
|
|
|
* @since 0.0.3
|
|
|
|
*/
|
|
|
|
public class OAuth2RefreshTokenAuthenticationProviderTests {
|
|
|
|
private OAuth2AuthorizationService authorizationService;
|
|
|
|
private JwtEncoder jwtEncoder;
|
2021-01-18 21:31:06 +07:00
|
|
|
private OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer;
|
2020-10-31 01:22:47 +07:00
|
|
|
private OAuth2RefreshTokenAuthenticationProvider authenticationProvider;
|
2020-10-07 16:53:37 +07:00
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
this.authorizationService = mock(OAuth2AuthorizationService.class);
|
|
|
|
this.jwtEncoder = mock(JwtEncoder.class);
|
2021-01-18 21:31:06 +07:00
|
|
|
when(this.jwtEncoder.encode(any(), any())).thenReturn(createJwt(Collections.singleton("scope1")));
|
2020-10-31 01:22:47 +07:00
|
|
|
this.authenticationProvider = new OAuth2RefreshTokenAuthenticationProvider(
|
|
|
|
this.authorizationService, this.jwtEncoder);
|
2021-01-18 21:31:06 +07:00
|
|
|
this.jwtCustomizer = mock(OAuth2TokenCustomizer.class);
|
|
|
|
this.authenticationProvider.setJwtCustomizer(this.jwtCustomizer);
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void constructorWhenAuthorizationServiceNullThenThrowIllegalArgumentException() {
|
2020-10-07 16:53:37 +07:00
|
|
|
assertThatThrownBy(() -> new OAuth2RefreshTokenAuthenticationProvider(null, this.jwtEncoder))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.extracting(Throwable::getMessage)
|
|
|
|
.isEqualTo("authorizationService cannot be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void constructorWhenJwtEncoderNullThenThrowIllegalArgumentException() {
|
2020-10-07 16:53:37 +07:00
|
|
|
assertThatThrownBy(() -> new OAuth2RefreshTokenAuthenticationProvider(this.authorizationService, null))
|
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
|
.extracting(Throwable::getMessage)
|
|
|
|
.isEqualTo("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-10-07 16:53:37 +07:00
|
|
|
@Test
|
|
|
|
public void supportsWhenSupportedAuthenticationThenTrue() {
|
2020-10-31 01:22:47 +07:00
|
|
|
assertThat(this.authenticationProvider.supports(OAuth2RefreshTokenAuthenticationToken.class)).isTrue();
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void supportsWhenUnsupportedAuthenticationThenFalse() {
|
2020-10-31 01:22:47 +07:00
|
|
|
assertThat(this.authenticationProvider.supports(OAuth2ClientCredentialsAuthenticationToken.class)).isFalse();
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void authenticateWhenValidRefreshTokenThenReturnAccessToken() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
2020-10-31 01:22:47 +07:00
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
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);
|
2021-02-09 09:14:33 +07:00
|
|
|
assertThat(jwtEncodingContext.<Authentication>getPrincipal()).isEqualTo(authorization.getAttribute(Principal.class.getName()));
|
2021-01-18 21:31:06 +07:00
|
|
|
assertThat(jwtEncodingContext.getAuthorization()).isEqualTo(authorization);
|
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.REFRESH_TOKEN);
|
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-10-07 16:53:37 +07:00
|
|
|
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
|
verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
OAuth2Authorization updatedAuthorization = authorizationCaptor.getValue();
|
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
assertThat(accessTokenAuthentication.getRegisteredClient().getId()).isEqualTo(updatedAuthorization.getRegisteredClientId());
|
|
|
|
assertThat(accessTokenAuthentication.getPrincipal()).isEqualTo(clientPrincipal);
|
2021-02-06 01:20:17 +07:00
|
|
|
assertThat(accessTokenAuthentication.getAccessToken()).isEqualTo(updatedAuthorization.getAccessToken().getToken());
|
|
|
|
assertThat(updatedAuthorization.getAccessToken()).isNotEqualTo(authorization.getAccessToken());
|
|
|
|
assertThat(accessTokenAuthentication.getRefreshToken()).isEqualTo(updatedAuthorization.getRefreshToken().getToken());
|
2020-10-31 01:22:47 +07:00
|
|
|
// By default, refresh token is reused
|
2021-02-06 01:20:17 +07:00
|
|
|
assertThat(updatedAuthorization.getRefreshToken()).isEqualTo(authorization.getRefreshToken());
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void authenticateWhenReuseRefreshTokensFalseThenReturnNewRefreshToken() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
|
.tokenSettings(tokenSettings -> tokenSettings.reuseRefreshTokens(false))
|
2020-10-07 16:53:37 +07:00
|
|
|
.build();
|
2020-10-31 01:22:47 +07:00
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
|
|
|
ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class);
|
|
|
|
verify(this.authorizationService).save(authorizationCaptor.capture());
|
|
|
|
OAuth2Authorization updatedAuthorization = authorizationCaptor.getValue();
|
|
|
|
|
2021-02-06 01:20:17 +07:00
|
|
|
assertThat(accessTokenAuthentication.getRefreshToken()).isEqualTo(updatedAuthorization.getRefreshToken().getToken());
|
|
|
|
assertThat(updatedAuthorization.getRefreshToken()).isNotEqualTo(authorization.getRefreshToken());
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void authenticateWhenRequestedScopesAuthorizedThenAccessTokenIncludesScopes() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
2021-02-09 15:25:21 +07:00
|
|
|
Set<String> authorizedScopes = authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME);
|
2020-10-31 01:22:47 +07:00
|
|
|
Set<String> requestedScopes = new HashSet<>(authorizedScopes);
|
|
|
|
requestedScopes.remove("email");
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal, requestedScopes);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
|
|
|
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication =
|
2020-10-31 01:22:47 +07:00
|
|
|
(OAuth2AccessTokenAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
assertThat(accessTokenAuthentication.getAccessToken().getScopes()).isEqualTo(requestedScopes);
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void authenticateWhenRequestedScopesNotAuthorizedThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
2021-02-09 15:25:21 +07:00
|
|
|
Set<String> authorizedScopes = authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME);
|
2020-10-31 01:22:47 +07:00
|
|
|
Set<String> requestedScopes = new HashSet<>(authorizedScopes);
|
|
|
|
requestedScopes.add("unauthorized");
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal, requestedScopes);
|
2020-10-31 01:22:47 +07:00
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
2020-10-07 16:53:37 +07:00
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
2020-10-31 01:22:47 +07:00
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
2020-10-07 16:53:37 +07:00
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_SCOPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void authenticateWhenInvalidRefreshTokenThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
|
|
|
"invalid", clientPrincipal);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
2020-10-07 16:53:37 +07:00
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
|
|
|
|
}
|
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
@Test
|
|
|
|
public void authenticateWhenClientPrincipalNotOAuth2ClientAuthenticationTokenThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
TestingAuthenticationToken clientPrincipal = new TestingAuthenticationToken(
|
|
|
|
registeredClient.getClientId(), registeredClient.getClientSecret());
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
|
|
|
"refresh-token", clientPrincipal);
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:53:37 +07:00
|
|
|
@Test
|
|
|
|
public void authenticateWhenClientPrincipalNotAuthenticatedThenThrowOAuth2AuthenticationException() {
|
2020-10-31 01:22:47 +07:00
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
2020-10-24 06:41:17 +07:00
|
|
|
registeredClient.getClientId(), registeredClient.getClientSecret(), ClientAuthenticationMethod.BASIC, null);
|
2020-10-31 01:22:47 +07:00
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
|
|
|
"refresh-token", clientPrincipal);
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
|
|
|
}
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
@Test
|
|
|
|
public void authenticateWhenRefreshTokenIssuedToAnotherClientThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
|
|
|
TestRegisteredClients.registeredClient2().build());
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal);
|
2020-10-31 01:22:47 +07:00
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
2020-10-07 16:53:37 +07:00
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-31 01:22:47 +07:00
|
|
|
public void authenticateWhenClientNotAuthorizedToRefreshTokenThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient()
|
|
|
|
.authorizationGrantTypes(grantTypes -> grantTypes.remove(AuthorizationGrantType.REFRESH_TOKEN))
|
|
|
|
.build();
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal);
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.UNAUTHORIZED_CLIENT);
|
|
|
|
}
|
2020-10-07 16:53:37 +07:00
|
|
|
|
2020-10-31 01:22:47 +07:00
|
|
|
@Test
|
|
|
|
public void authenticateWhenExpiredRefreshTokenThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
2020-11-10 01:38:33 +07:00
|
|
|
OAuth2RefreshToken expiredRefreshToken = new OAuth2RefreshToken2(
|
2020-10-31 01:22:47 +07:00
|
|
|
"expired-refresh-token", Instant.now().minusSeconds(120), Instant.now().minusSeconds(60));
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization = OAuth2Authorization.from(authorization).token(expiredRefreshToken).build();
|
2020-10-31 01:22:47 +07:00
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-10-31 01:22:47 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal);
|
2020-10-31 01:22:47 +07:00
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
2020-10-07 16:53:37 +07:00
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
2020-10-31 01:22:47 +07:00
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
2020-10-07 16:53:37 +07:00
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
|
|
|
|
}
|
2020-11-28 19:14:22 +07:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void authenticateWhenRevokedRefreshTokenThenThrowOAuth2AuthenticationException() {
|
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
|
OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2(
|
|
|
|
"refresh-token", Instant.now().minusSeconds(120), Instant.now().plusSeconds(1000));
|
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
2021-02-06 01:20:17 +07:00
|
|
|
.token(refreshToken, (metadata) -> metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true))
|
2020-11-28 19:14:22 +07:00
|
|
|
.build();
|
|
|
|
when(this.authorizationService.findByToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
eq(authorization.getRefreshToken().getToken().getTokenValue()),
|
2021-02-09 23:44:21 +07:00
|
|
|
eq(OAuth2TokenType.REFRESH_TOKEN)))
|
2020-11-28 19:14:22 +07:00
|
|
|
.thenReturn(authorization);
|
|
|
|
|
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
|
OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken(
|
2021-02-06 01:20:17 +07:00
|
|
|
authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal);
|
2020-11-28 19:14:22 +07:00
|
|
|
|
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
|
|
|
|
.extracting("errorCode")
|
|
|
|
.isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
|
|
|
|
}
|
2021-01-18 21:31:06 +07:00
|
|
|
|
|
|
|
private static Jwt createJwt(Set<String> scope) {
|
|
|
|
Instant issuedAt = Instant.now();
|
|
|
|
Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS);
|
|
|
|
return Jwt.withTokenValue("refreshed-access-token")
|
|
|
|
.header(JoseHeaderNames.ALG, SignatureAlgorithm.RS256.getName())
|
|
|
|
.issuedAt(issuedAt)
|
|
|
|
.expiresAt(expiresAt)
|
|
|
|
.claim(OAuth2ParameterNames.SCOPE, scope)
|
|
|
|
.build();
|
|
|
|
}
|
2020-10-07 16:53:37 +07:00
|
|
|
}
|