From 60d4d5b7eed20e1d1f3c6749e9324ed82efbf449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=BCller?= Date: Tue, 21 Apr 2020 17:18:55 +0200 Subject: [PATCH] Enables empty authorityPrefix - docs stated that empty authorityPrefix are allowed but implementation denied to use `""` - commit removes the `hasText`-limitation but restricts to `notNull` Fixes gh-8421 --- .../JwtGrantedAuthoritiesConverter.java | 2 +- .../JwtGrantedAuthoritiesConverterTests.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java b/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java index 2803483a3e..2e9eaec2e9 100644 --- a/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java +++ b/oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java @@ -68,7 +68,7 @@ public final class JwtGrantedAuthoritiesConverter implements Converter authorities = jwtGrantedAuthoritiesConverter.convert(jwt); + + assertThat(authorities).containsExactly( + new SimpleGrantedAuthority("message:read"), + new SimpleGrantedAuthority("message:write")); + } + @Test public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() { Jwt jwt = jwt().claim("scope", "").build(); @@ -97,6 +116,19 @@ public class JwtGrantedAuthoritiesConverterTests { 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 authorities = jwtGrantedAuthoritiesConverter.convert(jwt); + + assertThat(authorities).containsExactly( + new SimpleGrantedAuthority("message:read"), + new SimpleGrantedAuthority("message:write")); + } + @Test public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() { Jwt jwt = jwt().claim("scp", Collections.emptyList()).build();