|
@@ -19,24 +19,27 @@ package org.springframework.security.oauth2.server.resource.authentication;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
|
+import java.util.Collections;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
-import org.springframework.security.oauth2.core.OAuth2TokenAttributes;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
+import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal;
|
|
|
|
+import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
|
+import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
|
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
|
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames;
|
|
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionException;
|
|
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionException;
|
|
import org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector;
|
|
import org.springframework.security.oauth2.server.resource.introspection.ReactiveOpaqueTokenIntrospector;
|
|
-import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.when;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
+import static org.springframework.security.oauth2.core.TestOAuth2AuthenticatedPrincipals.active;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ACTIVE;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.ACTIVE;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.AUDIENCE;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.EXPIRES_AT;
|
|
@@ -45,27 +48,27 @@ import static org.springframework.security.oauth2.server.resource.introspection.
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SCOPE;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.SUBJECT;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
|
import static org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionClaimNames.USERNAME;
|
|
-import static org.springframework.security.oauth2.server.resource.introspection.TestOAuth2TokenIntrospectionClientResponses.active;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Tests for {@link OAuth2IntrospectionReactiveAuthenticationManager}
|
|
|
|
|
|
+ * Tests for {@link OpaqueTokenReactiveAuthenticationManager}
|
|
|
|
+ *
|
|
|
|
+ * @author Josh Cummings
|
|
*/
|
|
*/
|
|
-public class OAuth2IntrospectionReactiveAuthenticationManagerTests {
|
|
|
|
|
|
+public class OpaqueTokenReactiveAuthenticationManagerTests {
|
|
@Test
|
|
@Test
|
|
public void authenticateWhenActiveTokenThenOk() throws Exception {
|
|
public void authenticateWhenActiveTokenThenOk() throws Exception {
|
|
- Map<String, Object> claims = active();
|
|
|
|
- claims.put("extension_field", "twenty-seven");
|
|
|
|
- ReactiveOpaqueTokenIntrospector introspectionClient = mock(ReactiveOpaqueTokenIntrospector.class);
|
|
|
|
- when(introspectionClient.introspect(any())).thenReturn(Mono.just(claims));
|
|
|
|
- OAuth2IntrospectionReactiveAuthenticationManager provider =
|
|
|
|
- new OAuth2IntrospectionReactiveAuthenticationManager(introspectionClient);
|
|
|
|
|
|
+ OAuth2AuthenticatedPrincipal authority = active(attributes -> attributes.put("extension_field", "twenty-seven"));
|
|
|
|
+ ReactiveOpaqueTokenIntrospector introspector = mock(ReactiveOpaqueTokenIntrospector.class);
|
|
|
|
+ when(introspector.introspect(any())).thenReturn(Mono.just(authority));
|
|
|
|
+ OpaqueTokenReactiveAuthenticationManager provider =
|
|
|
|
+ new OpaqueTokenReactiveAuthenticationManager(introspector);
|
|
|
|
|
|
Authentication result =
|
|
Authentication result =
|
|
provider.authenticate(new BearerTokenAuthenticationToken("token")).block();
|
|
provider.authenticate(new BearerTokenAuthenticationToken("token")).block();
|
|
|
|
|
|
- assertThat(result.getPrincipal()).isInstanceOf(OAuth2TokenAttributes.class);
|
|
|
|
|
|
+ assertThat(result.getPrincipal()).isInstanceOf(DefaultOAuth2AuthenticatedPrincipal.class);
|
|
|
|
|
|
- Map<String, Object> attributes = ((OAuth2TokenAttributes) result.getPrincipal()).getAttributes();
|
|
|
|
|
|
+ Map<String, Object> attributes = ((DefaultOAuth2AuthenticatedPrincipal) result.getPrincipal()).getAttributes();
|
|
assertThat(attributes)
|
|
assertThat(attributes)
|
|
.isNotNull()
|
|
.isNotNull()
|
|
.containsEntry(ACTIVE, true)
|
|
.containsEntry(ACTIVE, true)
|
|
@@ -85,18 +88,17 @@ public class OAuth2IntrospectionReactiveAuthenticationManagerTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void authenticateWhenMissingScopeAttributeThenNoAuthorities() {
|
|
public void authenticateWhenMissingScopeAttributeThenNoAuthorities() {
|
|
- Map<String, Object> claims = active();
|
|
|
|
- claims.remove(SCOPE);
|
|
|
|
- ReactiveOpaqueTokenIntrospector introspectionClient = mock(ReactiveOpaqueTokenIntrospector.class);
|
|
|
|
- when(introspectionClient.introspect(any())).thenReturn(Mono.just(claims));
|
|
|
|
- OAuth2IntrospectionReactiveAuthenticationManager provider =
|
|
|
|
- new OAuth2IntrospectionReactiveAuthenticationManager(introspectionClient);
|
|
|
|
|
|
+ OAuth2AuthenticatedPrincipal authority = new DefaultOAuth2AuthenticatedPrincipal(Collections.singletonMap("claim", "value"), null);
|
|
|
|
+ ReactiveOpaqueTokenIntrospector introspector = mock(ReactiveOpaqueTokenIntrospector.class);
|
|
|
|
+ when(introspector.introspect(any())).thenReturn(Mono.just(authority));
|
|
|
|
+ OpaqueTokenReactiveAuthenticationManager provider =
|
|
|
|
+ new OpaqueTokenReactiveAuthenticationManager(introspector);
|
|
|
|
|
|
Authentication result =
|
|
Authentication result =
|
|
provider.authenticate(new BearerTokenAuthenticationToken("token")).block();
|
|
provider.authenticate(new BearerTokenAuthenticationToken("token")).block();
|
|
- assertThat(result.getPrincipal()).isInstanceOf(OAuth2TokenAttributes.class);
|
|
|
|
|
|
+ assertThat(result.getPrincipal()).isInstanceOf(DefaultOAuth2AuthenticatedPrincipal.class);
|
|
|
|
|
|
- Map<String, Object> attributes = ((OAuth2TokenAttributes) result.getPrincipal()).getAttributes();
|
|
|
|
|
|
+ Map<String, Object> attributes = ((DefaultOAuth2AuthenticatedPrincipal) result.getPrincipal()).getAttributes();
|
|
assertThat(attributes)
|
|
assertThat(attributes)
|
|
.isNotNull()
|
|
.isNotNull()
|
|
.doesNotContainKey(SCOPE);
|
|
.doesNotContainKey(SCOPE);
|
|
@@ -106,11 +108,11 @@ public class OAuth2IntrospectionReactiveAuthenticationManagerTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void authenticateWhenIntrospectionEndpointThrowsExceptionThenInvalidToken() {
|
|
public void authenticateWhenIntrospectionEndpointThrowsExceptionThenInvalidToken() {
|
|
- ReactiveOpaqueTokenIntrospector introspectionClient = mock(ReactiveOpaqueTokenIntrospector.class);
|
|
|
|
- when(introspectionClient.introspect(any()))
|
|
|
|
|
|
+ ReactiveOpaqueTokenIntrospector introspector = mock(ReactiveOpaqueTokenIntrospector.class);
|
|
|
|
+ when(introspector.introspect(any()))
|
|
.thenReturn(Mono.error(new OAuth2IntrospectionException("with \"invalid\" chars")));
|
|
.thenReturn(Mono.error(new OAuth2IntrospectionException("with \"invalid\" chars")));
|
|
- OAuth2IntrospectionReactiveAuthenticationManager provider =
|
|
|
|
- new OAuth2IntrospectionReactiveAuthenticationManager(introspectionClient);
|
|
|
|
|
|
+ OpaqueTokenReactiveAuthenticationManager provider =
|
|
|
|
+ new OpaqueTokenReactiveAuthenticationManager(introspector);
|
|
|
|
|
|
assertThatCode(() -> provider.authenticate(new BearerTokenAuthenticationToken("token")).block())
|
|
assertThatCode(() -> provider.authenticate(new BearerTokenAuthenticationToken("token")).block())
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
.isInstanceOf(OAuth2AuthenticationException.class)
|
|
@@ -120,7 +122,7 @@ public class OAuth2IntrospectionReactiveAuthenticationManagerTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void constructorWhenIntrospectionClientIsNullThenIllegalArgumentException() {
|
|
public void constructorWhenIntrospectionClientIsNullThenIllegalArgumentException() {
|
|
- assertThatCode(() -> new OAuth2IntrospectionReactiveAuthenticationManager(null))
|
|
|
|
|
|
+ assertThatCode(() -> new OpaqueTokenReactiveAuthenticationManager(null))
|
|
.isInstanceOf(IllegalArgumentException.class);
|
|
.isInstanceOf(IllegalArgumentException.class);
|
|
}
|
|
}
|
|
}
|
|
}
|