|
@@ -15,62 +15,72 @@
|
|
|
*/
|
|
|
package org.springframework.security.oauth2.server.authorization.authentication;
|
|
|
|
|
|
-import static java.time.Duration.ofHours;
|
|
|
-import static java.time.Instant.now;
|
|
|
-import static org.assertj.core.api.Assertions.assertThat;
|
|
|
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
-import static org.mockito.ArgumentMatchers.eq;
|
|
|
-import static org.mockito.ArgumentMatchers.isNull;
|
|
|
-import static org.mockito.Mockito.mock;
|
|
|
-import static org.mockito.Mockito.when;
|
|
|
-import static org.springframework.security.oauth2.core.OAuth2TokenIntrospectionClaimAccessor.ACTIVE;
|
|
|
-import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.CLIENT_ID;
|
|
|
-import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.SCOPE;
|
|
|
-import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.TOKEN_TYPE;
|
|
|
-import static org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames.USERNAME;
|
|
|
-import static org.springframework.security.oauth2.jwt.JwtClaimNames.EXP;
|
|
|
-import static org.springframework.security.oauth2.jwt.JwtClaimNames.IAT;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.Instant;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
+
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
|
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
|
-import org.springframework.security.oauth2.core.OAuth2TokenType;
|
|
|
+import org.springframework.security.oauth2.core.OAuth2TokenIntrospection;
|
|
|
import org.springframework.security.oauth2.jwt.JwtClaimNames;
|
|
|
+import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
|
|
+import org.springframework.security.oauth2.jwt.TestJwtClaimsSets;
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
|
|
import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
+import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
|
import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
|
|
|
|
|
-import java.time.Instant;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashSet;
|
|
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
|
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.ArgumentMatchers.isNull;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
|
* Tests for {@link OAuth2TokenIntrospectionAuthenticationProvider}.
|
|
|
*
|
|
|
* @author Gerardo Roza
|
|
|
+ * @author Joe Grandja
|
|
|
*/
|
|
|
public class OAuth2TokenIntrospectionAuthenticationProviderTests {
|
|
|
+ private RegisteredClientRepository registeredClientRepository;
|
|
|
private OAuth2AuthorizationService authorizationService;
|
|
|
private OAuth2TokenIntrospectionAuthenticationProvider authenticationProvider;
|
|
|
|
|
|
@Before
|
|
|
public void setUp() {
|
|
|
+ this.registeredClientRepository = mock(RegisteredClientRepository.class);
|
|
|
this.authorizationService = mock(OAuth2AuthorizationService.class);
|
|
|
- this.authenticationProvider = new OAuth2TokenIntrospectionAuthenticationProvider(this.authorizationService);
|
|
|
+ this.authenticationProvider = new OAuth2TokenIntrospectionAuthenticationProvider(
|
|
|
+ this.registeredClientRepository, this.authorizationService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void constructorWhenRegisteredClientRepositoryNullThenThrowIllegalArgumentException() {
|
|
|
+ assertThatThrownBy(() -> new OAuth2TokenIntrospectionAuthenticationProvider(null, this.authorizationService))
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
+ .hasMessage("registeredClientRepository cannot be null");
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void constructorWhenAuthorizationServiceNullThenThrowIllegalArgumentException() {
|
|
|
- assertThatThrownBy(() -> new OAuth2TokenIntrospectionAuthenticationProvider(null))
|
|
|
- .isInstanceOf(IllegalArgumentException.class).hasMessage("authorizationService cannot be null");
|
|
|
+ assertThatThrownBy(() -> new OAuth2TokenIntrospectionAuthenticationProvider(this.registeredClientRepository, null))
|
|
|
+ .isInstanceOf(IllegalArgumentException.class)
|
|
|
+ .hasMessage("authorizationService cannot be null");
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -83,8 +93,10 @@ public class OAuth2TokenIntrospectionAuthenticationProviderTests {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
TestingAuthenticationToken clientPrincipal = new TestingAuthenticationToken(
|
|
|
registeredClient.getClientId(), registeredClient.getClientSecret());
|
|
|
+
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- "token", clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
+ "token", clientPrincipal, null, null);
|
|
|
+
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError()).extracting("errorCode")
|
|
@@ -95,10 +107,11 @@ public class OAuth2TokenIntrospectionAuthenticationProviderTests {
|
|
|
public void authenticateWhenClientPrincipalNotAuthenticatedThenThrowOAuth2AuthenticationException() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
|
|
- registeredClient.getClientId(), registeredClient.getClientSecret(), ClientAuthenticationMethod.BASIC,
|
|
|
- null);
|
|
|
+ registeredClient.getClientId(), registeredClient.getClientSecret(), ClientAuthenticationMethod.BASIC, null);
|
|
|
+
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- "token", clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
+ "token", clientPrincipal, null, null);
|
|
|
+
|
|
|
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
|
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
|
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError()).extracting("errorCode")
|
|
@@ -106,144 +119,157 @@ public class OAuth2TokenIntrospectionAuthenticationProviderTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authenticateWhenInvalidOAuth2TokenTypeThenThrowOAuth2AuthenticationException() {
|
|
|
+ public void authenticateWhenInvalidTokenThenNotActive() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- "token", clientPrincipal, "unsupportedOAuth2TokenType");
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
- assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isFalse();
|
|
|
- }
|
|
|
|
|
|
- @Test
|
|
|
- public void authenticateWhenTokenNotFoundThenAuthenticatedButTokenNotActive() {
|
|
|
- RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- "token", clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
- assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isFalse();
|
|
|
+ "token", clientPrincipal, null, null);
|
|
|
+ OAuth2TokenIntrospectionAuthenticationToken authenticationResult =
|
|
|
+ (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
+
|
|
|
+ verify(this.authorizationService).findByToken(eq(authentication.getToken()), isNull());
|
|
|
+ assertThat(authenticationResult.isAuthenticated()).isFalse();
|
|
|
+ assertThat(authenticationResult.getTokenClaims().getClaims()).hasSize(1);
|
|
|
+ assertThat(authenticationResult.getTokenClaims().isActive()).isFalse();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authenticateWhenInvalidatedTokenThenAuthenticatedButTokenNotActive() {
|
|
|
+ public void authenticateWhenTokenInvalidatedThenNotActive() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
OAuth2AccessToken accessToken = authorization.getAccessToken().getToken();
|
|
|
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, accessToken);
|
|
|
when(this.authorizationService.findByToken(eq(accessToken.getTokenValue()), isNull()))
|
|
|
.thenReturn(authorization);
|
|
|
-
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
+
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- accessToken.getTokenValue(), clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
+ accessToken.getTokenValue(), clientPrincipal, null, null);
|
|
|
+ OAuth2TokenIntrospectionAuthenticationToken authenticationResult =
|
|
|
+ (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
+ verify(this.authorizationService).findByToken(eq(authentication.getToken()), isNull());
|
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isFalse();
|
|
|
+ assertThat(authenticationResult.getTokenClaims().getClaims()).hasSize(1);
|
|
|
+ assertThat(authenticationResult.getTokenClaims().isActive()).isFalse();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authenticateWhenValidAccessTokenThenActiveWithScopes() {
|
|
|
+ public void authenticateWhenTokenExpiredThenNotActive() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
+ Instant issuedAt = Instant.now().minus(Duration.ofHours(1));
|
|
|
+ Instant expiresAt = Instant.now().minus(Duration.ofMinutes(1));
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(
|
|
|
- OAuth2AccessToken.TokenType.BEARER, "access-token", Instant.now(), Instant.now().plusSeconds(300),
|
|
|
- new HashSet<>(Arrays.asList("scope1", "Scope2")));
|
|
|
- OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
- .accessToken(accessToken).build();
|
|
|
+ OAuth2AccessToken.TokenType.BEARER, "access-token", issuedAt, expiresAt);
|
|
|
+ OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).token(accessToken).build();
|
|
|
when(this.authorizationService.findByToken(eq(accessToken.getTokenValue()), isNull()))
|
|
|
.thenReturn(authorization);
|
|
|
-
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
+
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- accessToken.getTokenValue(), clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
+ accessToken.getTokenValue(), clientPrincipal, null, null);
|
|
|
+ OAuth2TokenIntrospectionAuthenticationToken authenticationResult =
|
|
|
+ (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
+ verify(this.authorizationService).findByToken(eq(authentication.getToken()), isNull());
|
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isTrue();
|
|
|
- assertThat(authenticationResult.getClaims()).containsEntry(ACTIVE, true)
|
|
|
- .containsEntry(IAT, accessToken.getIssuedAt()).containsEntry(EXP, accessToken.getExpiresAt())
|
|
|
- .containsEntry(TOKEN_TYPE, OAuth2AccessToken.TokenType.BEARER).containsEntry(CLIENT_ID, "client-1")
|
|
|
- .containsEntry(USERNAME, "principal").containsKey(SCOPE)
|
|
|
- .containsAllEntriesOf(authorization.getAccessToken().getClaims());
|
|
|
-
|
|
|
+ assertThat(authenticationResult.getTokenClaims().getClaims()).hasSize(1);
|
|
|
+ assertThat(authenticationResult.getTokenClaims().isActive()).isFalse();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authenticateWhenValidRefreshTokenThenActive() {
|
|
|
+ public void authenticateWhenTokenBeforeUseThenNotActive() {
|
|
|
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
|
|
|
- OAuth2RefreshToken refreshToken = authorization.getRefreshToken().getToken();
|
|
|
- when(this.authorizationService.findByToken(eq(refreshToken.getTokenValue()), isNull()))
|
|
|
+ Instant issuedAt = Instant.now();
|
|
|
+ Instant notBefore = issuedAt.plus(Duration.ofMinutes(5));
|
|
|
+ Instant expiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
|
+ OAuth2AccessToken accessToken = new OAuth2AccessToken(
|
|
|
+ OAuth2AccessToken.TokenType.BEARER, "access-token", issuedAt, expiresAt);
|
|
|
+ Map<String, Object> accessTokenClaims = Collections.singletonMap(JwtClaimNames.NBF, notBefore);
|
|
|
+ OAuth2Authorization authorization = TestOAuth2Authorizations
|
|
|
+ .authorization(registeredClient, accessToken, accessTokenClaims)
|
|
|
+ .build();
|
|
|
+ when(this.authorizationService.findByToken(eq(accessToken.getTokenValue()), isNull()))
|
|
|
.thenReturn(authorization);
|
|
|
-
|
|
|
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
+
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- refreshToken.getTokenValue(), clientPrincipal, OAuth2TokenType.REFRESH_TOKEN.getValue());
|
|
|
+ accessToken.getTokenValue(), clientPrincipal, null, null);
|
|
|
+ OAuth2TokenIntrospectionAuthenticationToken authenticationResult =
|
|
|
+ (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
+ verify(this.authorizationService).findByToken(eq(authentication.getToken()), isNull());
|
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isTrue();
|
|
|
- assertThat(authenticationResult.getClaims()).containsEntry(ACTIVE, true)
|
|
|
- .containsEntry(IAT, refreshToken.getIssuedAt()).containsEntry(EXP, refreshToken.getExpiresAt())
|
|
|
- .containsEntry(USERNAME, "principal").containsEntry(CLIENT_ID, "client-1").hasSize(5);
|
|
|
+ assertThat(authenticationResult.getTokenClaims().getClaims()).hasSize(1);
|
|
|
+ assertThat(authenticationResult.getTokenClaims().isActive()).isFalse();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authenticateWhenExpiredTokenThenAuthenticatedButTokenNotActive() {
|
|
|
- RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- Instant expiresAt = Instant.now().minus(ofHours(1));
|
|
|
- Instant issuedAt = expiresAt.minus(ofHours(1));
|
|
|
+ public void authenticateWhenValidAccessTokenThenActive() {
|
|
|
+ RegisteredClient authorizedClient = TestRegisteredClients.registeredClient().build();
|
|
|
+ Instant issuedAt = Instant.now();
|
|
|
+ Instant expiresAt = issuedAt.plus(Duration.ofHours(1));
|
|
|
OAuth2AccessToken accessToken = new OAuth2AccessToken(
|
|
|
- OAuth2AccessToken.TokenType.BEARER, "access-token", issuedAt, expiresAt);
|
|
|
- OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).token(accessToken)
|
|
|
+ OAuth2AccessToken.TokenType.BEARER, "access-token", issuedAt, expiresAt,
|
|
|
+ new HashSet<>(Arrays.asList("scope1", "scope2")));
|
|
|
+ JwtClaimsSet jwtClaims = TestJwtClaimsSets.jwtClaimsSet().build();
|
|
|
+ OAuth2Authorization authorization = TestOAuth2Authorizations
|
|
|
+ .authorization(authorizedClient, accessToken, jwtClaims.getClaims())
|
|
|
.build();
|
|
|
when(this.authorizationService.findByToken(eq(accessToken.getTokenValue()), isNull()))
|
|
|
.thenReturn(authorization);
|
|
|
+ when(this.registeredClientRepository.findById(eq(authorizedClient.getId()))).thenReturn(authorizedClient);
|
|
|
+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
|
|
+ TestRegisteredClients.registeredClient2().build());
|
|
|
|
|
|
- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- accessToken.getTokenValue(), clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
+ accessToken.getTokenValue(), clientPrincipal, null, null);
|
|
|
+ OAuth2TokenIntrospectionAuthenticationToken authenticationResult =
|
|
|
+ (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
+ verify(this.authorizationService).findByToken(eq(authentication.getToken()), isNull());
|
|
|
+ verify(this.registeredClientRepository).findById(eq(authorizedClient.getId()));
|
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isFalse();
|
|
|
- assertThat(authenticationResult.getClaims()).isNull();
|
|
|
+ OAuth2TokenIntrospection tokenClaims = authenticationResult.getTokenClaims();
|
|
|
+ assertThat(tokenClaims.isActive()).isTrue();
|
|
|
+ assertThat(tokenClaims.getClientId()).isEqualTo(authorizedClient.getClientId());
|
|
|
+ assertThat(tokenClaims.getIssuedAt()).isEqualTo(accessToken.getIssuedAt());
|
|
|
+ assertThat(tokenClaims.getExpiresAt()).isEqualTo(accessToken.getExpiresAt());
|
|
|
+ assertThat(tokenClaims.getScope()).containsExactlyInAnyOrderElementsOf(accessToken.getScopes());
|
|
|
+ assertThat(tokenClaims.getTokenType()).isEqualTo(accessToken.getTokenType().getValue());
|
|
|
+ assertThat(tokenClaims.getNotBefore()).isEqualTo(jwtClaims.getNotBefore());
|
|
|
+ assertThat(tokenClaims.getSubject()).isEqualTo(jwtClaims.getSubject());
|
|
|
+ assertThat(tokenClaims.getAudience()).containsExactlyInAnyOrderElementsOf(jwtClaims.getAudience());
|
|
|
+ assertThat(tokenClaims.getIssuer()).isEqualTo(jwtClaims.getIssuer());
|
|
|
+ assertThat(tokenClaims.getId()).isEqualTo(jwtClaims.getId());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void authenticateWhenInvalidNotBeforeClaimThenAuthenticatedButTokenNotActive() {
|
|
|
- RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
|
- Instant issuedAt = Instant.now();
|
|
|
- Instant expiresAt = issuedAt.plus(ofHours(1));
|
|
|
- OAuth2AccessToken accessToken = new OAuth2AccessToken(
|
|
|
- OAuth2AccessToken.TokenType.BEARER, "access-token", issuedAt, expiresAt);
|
|
|
- OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
|
|
|
- .token(
|
|
|
- accessToken,
|
|
|
- (metadata) -> metadata.put(
|
|
|
- OAuth2Authorization.Token.CLAIMS_METADATA_NAME,
|
|
|
- Collections.singletonMap(JwtClaimNames.NBF, now().plus(ofHours(1)))))
|
|
|
- .build();
|
|
|
- when(this.authorizationService.findByToken(eq(accessToken.getTokenValue()), isNull()))
|
|
|
+ public void authenticateWhenValidRefreshTokenThenActive() {
|
|
|
+ RegisteredClient authorizedClient = TestRegisteredClients.registeredClient().build();
|
|
|
+ OAuth2Authorization authorization = TestOAuth2Authorizations.authorization().build();
|
|
|
+ OAuth2RefreshToken refreshToken = authorization.getRefreshToken().getToken();
|
|
|
+ when(this.authorizationService.findByToken(eq(refreshToken.getTokenValue()), isNull()))
|
|
|
.thenReturn(authorization);
|
|
|
+ when(this.registeredClientRepository.findById(eq(authorizedClient.getId()))).thenReturn(authorizedClient);
|
|
|
+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
|
|
|
+ TestRegisteredClients.registeredClient2().build());
|
|
|
|
|
|
- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient);
|
|
|
OAuth2TokenIntrospectionAuthenticationToken authentication = new OAuth2TokenIntrospectionAuthenticationToken(
|
|
|
- accessToken.getTokenValue(), clientPrincipal, OAuth2TokenType.ACCESS_TOKEN.getValue());
|
|
|
+ refreshToken.getTokenValue(), clientPrincipal, null, null);
|
|
|
+ OAuth2TokenIntrospectionAuthenticationToken authenticationResult =
|
|
|
+ (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
|
|
|
|
|
- OAuth2TokenIntrospectionAuthenticationToken authenticationResult = (OAuth2TokenIntrospectionAuthenticationToken) this.authenticationProvider
|
|
|
- .authenticate(authentication);
|
|
|
+ verify(this.authorizationService).findByToken(eq(authentication.getToken()), isNull());
|
|
|
+ verify(this.registeredClientRepository).findById(eq(authorizedClient.getId()));
|
|
|
assertThat(authenticationResult.isAuthenticated()).isTrue();
|
|
|
- assertThat(authenticationResult.isTokenActive()).isFalse();
|
|
|
- assertThat(authenticationResult.getClaims()).isNull();
|
|
|
+ OAuth2TokenIntrospection tokenClaims = authenticationResult.getTokenClaims();
|
|
|
+ assertThat(tokenClaims.getClaims()).hasSize(4);
|
|
|
+ assertThat(tokenClaims.isActive()).isTrue();
|
|
|
+ assertThat(tokenClaims.getClientId()).isEqualTo(authorizedClient.getClientId());
|
|
|
+ assertThat(tokenClaims.getIssuedAt()).isEqualTo(refreshToken.getIssuedAt());
|
|
|
+ assertThat(tokenClaims.getExpiresAt()).isEqualTo(refreshToken.getExpiresAt());
|
|
|
}
|
|
|
+
|
|
|
}
|