|
@@ -30,6 +30,7 @@ import org.junit.Test;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
+import org.springframework.security.oauth2.core.OAuth2TokenAttributes;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
@@ -46,14 +47,15 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
private final OAuth2AccessToken token =
|
|
|
new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER,
|
|
|
"token", Instant.now(), Instant.now().plusSeconds(3600));
|
|
|
- private final Map<String, Object> attributes = new HashMap<>();
|
|
|
private final String name = "sub";
|
|
|
+ private Map<String, Object> attributesMap = new HashMap<>();
|
|
|
+ private final OAuth2TokenAttributes attributes = new OAuth2TokenAttributes(attributesMap);
|
|
|
|
|
|
@Before
|
|
|
public void setUp() {
|
|
|
- this.attributes.put(SUBJECT, this.name);
|
|
|
- this.attributes.put(CLIENT_ID, "client_id");
|
|
|
- this.attributes.put(USERNAME, "username");
|
|
|
+ this.attributesMap.put(SUBJECT, this.name);
|
|
|
+ this.attributesMap.put(CLIENT_ID, "client_id");
|
|
|
+ this.attributesMap.put(USERNAME, "username");
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -67,7 +69,8 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
@Test
|
|
|
public void getNameWhenHasNoSubjectThenReturnsNull() {
|
|
|
OAuth2IntrospectionAuthenticationToken authenticated =
|
|
|
- new OAuth2IntrospectionAuthenticationToken(this.token, Collections.singletonMap("claim", "value"),
|
|
|
+ new OAuth2IntrospectionAuthenticationToken(this.token,
|
|
|
+ new OAuth2TokenAttributes(Collections.singletonMap("claim", "value")),
|
|
|
Collections.emptyList());
|
|
|
assertThat(authenticated.getName()).isNull();
|
|
|
}
|
|
@@ -76,7 +79,7 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
public void getNameWhenTokenHasUsernameThenReturnsUsernameAttribute() {
|
|
|
OAuth2IntrospectionAuthenticationToken authenticated =
|
|
|
new OAuth2IntrospectionAuthenticationToken(this.token, this.attributes, Collections.emptyList());
|
|
|
- assertThat(authenticated.getName()).isEqualTo(this.attributes.get(SUBJECT));
|
|
|
+ assertThat(authenticated.getName()).isEqualTo(this.attributes.getAttribute(SUBJECT));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -92,7 +95,8 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
.hasMessageContaining("attributes cannot be empty");
|
|
|
|
|
|
- assertThatCode(() -> new OAuth2IntrospectionAuthenticationToken(this.token, Collections.emptyMap(), null))
|
|
|
+ assertThatCode(() -> new OAuth2IntrospectionAuthenticationToken(this.token,
|
|
|
+ new OAuth2TokenAttributes(Collections.emptyMap()), null))
|
|
|
.isInstanceOf(IllegalArgumentException.class)
|
|
|
.hasMessageContaining("attributes cannot be empty");
|
|
|
}
|
|
@@ -100,7 +104,8 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
@Test
|
|
|
public void constructorWhenPassingAllAttributesThenTokenIsAuthenticated() {
|
|
|
OAuth2IntrospectionAuthenticationToken authenticated =
|
|
|
- new OAuth2IntrospectionAuthenticationToken(this.token, Collections.singletonMap("claim", "value"),
|
|
|
+ new OAuth2IntrospectionAuthenticationToken(this.token,
|
|
|
+ new OAuth2TokenAttributes(Collections.singletonMap("claim", "value")),
|
|
|
Collections.emptyList(), "harris");
|
|
|
assertThat(authenticated.isAuthenticated()).isTrue();
|
|
|
}
|
|
@@ -109,7 +114,7 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
public void getTokenAttributesWhenHasTokenThenReturnsThem() {
|
|
|
OAuth2IntrospectionAuthenticationToken authenticated =
|
|
|
new OAuth2IntrospectionAuthenticationToken(this.token, this.attributes, Collections.emptyList());
|
|
|
- assertThat(authenticated.getTokenAttributes()).isEqualTo(this.attributes);
|
|
|
+ assertThat(authenticated.getTokenAttributes()).isEqualTo(this.attributes.getAttributes());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -126,7 +131,8 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
JSONObject attributes = new JSONObject();
|
|
|
attributes.put("active", true);
|
|
|
OAuth2IntrospectionAuthenticationToken token =
|
|
|
- new OAuth2IntrospectionAuthenticationToken(this.token, attributes, Collections.emptyList());
|
|
|
+ new OAuth2IntrospectionAuthenticationToken(this.token, new OAuth2TokenAttributes(attributes),
|
|
|
+ Collections.emptyList());
|
|
|
assertThat(token.getPrincipal()).isNotSameAs(attributes);
|
|
|
assertThat(token.getTokenAttributes()).isNotSameAs(attributes);
|
|
|
}
|
|
@@ -136,7 +142,8 @@ public class OAuth2IntrospectionAuthenticationTokenTests {
|
|
|
public void toStringWhenAttributesContainsURLThenDoesNotFail() throws Exception {
|
|
|
JSONObject attributes = new JSONObject(Collections.singletonMap("iss", new URL("https://idp.example.com")));
|
|
|
OAuth2IntrospectionAuthenticationToken token =
|
|
|
- new OAuth2IntrospectionAuthenticationToken(this.token, attributes, Collections.emptyList());
|
|
|
+ new OAuth2IntrospectionAuthenticationToken(this.token, new OAuth2TokenAttributes(attributes),
|
|
|
+ Collections.emptyList());
|
|
|
assertThatCode(token::toString)
|
|
|
.doesNotThrowAnyException();
|
|
|
}
|