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