|
@@ -45,7 +45,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scope", "message:read message:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"),
|
|
@@ -54,7 +58,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scope", "message:read message:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -64,7 +72,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scope", "message:read message:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -74,7 +86,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scope", "").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scope", "")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).isEmpty();
|
|
@@ -82,7 +98,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_message:read"),
|
|
@@ -91,7 +111,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -101,7 +125,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", "message:read message:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", "message:read message:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -111,7 +139,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", Collections.emptyList()).build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", Collections.emptyList())
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).isEmpty();
|
|
@@ -119,8 +151,12 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasBothScopeAndScpThenScopeAttributeIsTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
- .claim("scope", "missive:read missive:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).containsExactly(new SimpleGrantedAuthority("SCOPE_missive:read"),
|
|
@@ -129,8 +165,12 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTranslatedToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", Arrays.asList("message:read", "message:write")).claim("scope", "")
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", Arrays.asList("message:read", "message:write"))
|
|
|
+ .claim("scope", "")
|
|
|
.build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).isEmpty();
|
|
@@ -138,15 +178,25 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scp", Collections.emptyList()).claim("scope", Collections.emptyList()).build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scp", Collections.emptyList())
|
|
|
+ .claim("scope", Collections.emptyList())
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
- assertThat(authorities).isEmpty();
|
|
|
+ assertThat(authorities)
|
|
|
+ .isEmpty();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasNoScopeAndNoScpAttributeThenTranslatesToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("roles", Arrays.asList("message:read", "message:write")).build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("roles", Arrays.asList("message:read", "message:write"))
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
assertThat(authorities).isEmpty();
|
|
@@ -154,16 +204,25 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scope", new String[] { "message:read", "message:write" }).build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scope", new String[] { "message:read", "message:write" })
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
- assertThat(authorities).isEmpty();
|
|
|
+ assertThat(authorities)
|
|
|
+ .isEmpty();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("roles", Arrays.asList("message:read", "message:write"))
|
|
|
- .claim("scope", "missive:read missive:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("roles", Arrays.asList("message:read", "message:write"))
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -173,8 +232,12 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasEmptyCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("roles", Collections.emptyList()).claim("scope", "missive:read missive:write")
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("roles", Collections.emptyList())
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
.build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
@@ -183,7 +246,11 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void convertWhenTokenHasNoCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToNoAuthorities() {
|
|
|
- Jwt jwt = TestJwts.jwt().claim("scope", "missive:read missive:write").build();
|
|
|
+ // @formatter:off
|
|
|
+ Jwt jwt = TestJwts.jwt()
|
|
|
+ .claim("scope", "missive:read missive:write")
|
|
|
+ .build();
|
|
|
+ // @formatter:on
|
|
|
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
|
|
|
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|