|
@@ -16,23 +16,19 @@
|
|
|
|
|
|
package org.springframework.security.oauth2.server.resource.authentication;
|
|
package org.springframework.security.oauth2.server.resource.authentication;
|
|
|
|
|
|
-import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
-
|
|
|
|
-import java.time.Instant;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
-import org.assertj.core.util.Maps;
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
-import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
|
|
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
|
|
|
|
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
+import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Tests for {@link JwtGrantedAuthoritiesConverter}
|
|
* Tests for {@link JwtGrantedAuthoritiesConverter}
|
|
*
|
|
*
|
|
@@ -43,7 +39,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
- Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write"));
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -55,7 +51,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
- Jwt jwt = this.jwt(Collections.singletonMap("scope", "message:read message:write"));
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
|
@@ -68,7 +64,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
|
- Jwt jwt = this.jwt(Collections.singletonMap("scope", ""));
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scope", "").build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -78,7 +74,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
- Jwt jwt = this.jwt(Collections.singletonMap("scp", Arrays.asList("message:read", "message:write")));
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -90,7 +86,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
- Jwt jwt = this.jwt(Collections.singletonMap("scp", Arrays.asList("message:read", "message:write")));
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
|
@@ -103,7 +99,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
|
- Jwt jwt = this.jwt(Maps.newHashMap("scp", Collections.emptyList()));
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scp", Collections.emptyList()).build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -113,10 +109,10 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() {
|
|
public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("scp", Arrays.asList("message:read", "message:write"));
|
|
|
|
- claims.put("scope", "missive:read missive:write");
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt()
|
|
|
|
+ .claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
|
+ .build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -128,10 +124,10 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("scp", Arrays.asList("message:read", "message:write"));
|
|
|
|
- claims.put("scope", "");
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt()
|
|
|
|
+ .claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
|
+ .claim("scope", "")
|
|
|
|
+ .build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -141,10 +137,10 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("scp", Collections.emptyList());
|
|
|
|
- claims.put("scope", Collections.emptyList());
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt()
|
|
|
|
+ .claim("scp", Collections.emptyList())
|
|
|
|
+ .claim("scope", Collections.emptyList())
|
|
|
|
+ .build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -154,9 +150,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasNoScopeAndNoScpAttributeThenTranslatesToNoAuthorities() {
|
|
public void convertWhenTokenHasNoScopeAndNoScpAttributeThenTranslatesToNoAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("roles", Arrays.asList("message:read", "message:write"));
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("roles", Arrays.asList("message:read", "message:write")).build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -166,9 +160,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities() {
|
|
public void convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("scope", new String[] {"message:read", "message:write"});
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scope", new String[] {"message:read", "message:write"}).build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -178,10 +170,10 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToAuthorities() {
|
|
public void convertWhenTokenHasCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("roles", Arrays.asList("message:read", "message:write"));
|
|
|
|
- claims.put("scope", "missive:read missive:write");
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt()
|
|
|
|
+ .claim("roles", Arrays.asList("message:read", "message:write"))
|
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
|
+ .build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
@@ -194,10 +186,10 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("roles", Collections.emptyList());
|
|
|
|
- claims.put("scope", "missive:read missive:write");
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt()
|
|
|
|
+ .claim("roles", Collections.emptyList())
|
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
|
+ .build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
@@ -208,9 +200,7 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
- claims.put("scope", "missive:read missive:write");
|
|
|
|
- Jwt jwt = this.jwt(claims);
|
|
|
|
|
|
+ Jwt jwt = jwt().claim("scope", "missive:read missive:write").build();
|
|
|
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
@@ -218,11 +208,4 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
assertThat(authorities).isEmpty();
|
|
assertThat(authorities).isEmpty();
|
|
}
|
|
}
|
|
-
|
|
|
|
- private Jwt jwt(Map<String, Object> claims) {
|
|
|
|
- Map<String, Object> headers = new HashMap<>();
|
|
|
|
- headers.put("alg", JwsAlgorithms.RS256);
|
|
|
|
-
|
|
|
|
- return new Jwt("token", Instant.now(), Instant.now().plusSeconds(3600), headers, claims);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|