|
@@ -37,6 +37,12 @@ import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
|
|
*/
|
|
*/
|
|
public class JwtGrantedAuthoritiesConverterTests {
|
|
public class JwtGrantedAuthoritiesConverterTests {
|
|
|
|
|
|
|
|
+ @Test(expected = IllegalArgumentException.class)
|
|
|
|
+ public void setAuthorityPrefixWithNullThenException() {
|
|
|
|
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
|
+ jwtGrantedAuthoritiesConverter.setAuthorityPrefix(null);
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
|
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
|
@@ -62,6 +68,19 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
new SimpleGrantedAuthority("ROLE_message:write"));
|
|
new SimpleGrantedAuthority("ROLE_message:write"));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
|
|
|
|
+ Jwt jwt = jwt().claim("scope", "message:read message:write").build();
|
|
|
|
+
|
|
|
|
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
|
+ jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
|
|
|
|
+ Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
|
+
|
|
|
|
+ assertThat(authorities).containsExactly(
|
|
|
|
+ new SimpleGrantedAuthority("message:read"),
|
|
|
|
+ new SimpleGrantedAuthority("message:write"));
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
|
|
Jwt jwt = jwt().claim("scope", "").build();
|
|
Jwt jwt = jwt().claim("scope", "").build();
|
|
@@ -97,6 +116,19 @@ public class JwtGrantedAuthoritiesConverterTests {
|
|
new SimpleGrantedAuthority("ROLE_message:write"));
|
|
new SimpleGrantedAuthority("ROLE_message:write"));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
|
|
|
|
+ Jwt jwt = jwt().claim("scp", "message:read message:write").build();
|
|
|
|
+
|
|
|
|
+ JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
|
|
|
|
+ jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
|
|
|
|
+ Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
|
|
|
|
+
|
|
|
|
+ assertThat(authorities).containsExactly(
|
|
|
|
+ new SimpleGrantedAuthority("message:read"),
|
|
|
|
+ new SimpleGrantedAuthority("message:write"));
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
|
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
|
|
Jwt jwt = jwt().claim("scp", Collections.emptyList()).build();
|
|
Jwt jwt = jwt().claim("scp", Collections.emptyList()).build();
|