Skip to content

Commit 4fec451

Browse files
judomueleftherias
authored andcommitted
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
1 parent 7af5804 commit 4fec451

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Collection<GrantedAuthority> convert(Jwt jwt) {
6868
* @since 5.2
6969
*/
7070
public void setAuthorityPrefix(String authorityPrefix) {
71-
Assert.hasText(authorityPrefix, "authorityPrefix cannot be empty");
71+
Assert.notNull(authorityPrefix, "authorityPrefix cannot be null");
7272
this.authorityPrefix = authorityPrefix;
7373
}
7474

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverterTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
*/
3838
public class JwtGrantedAuthoritiesConverterTests {
3939

40+
@Test(expected = IllegalArgumentException.class)
41+
public void setAuthorityPrefixWithNullThenException() {
42+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
43+
jwtGrantedAuthoritiesConverter.setAuthorityPrefix(null);
44+
}
45+
4046
@Test
4147
public void convertWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
4248
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
@@ -62,6 +68,19 @@ public void convertWithCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTransl
6268
new SimpleGrantedAuthority("ROLE_message:write"));
6369
}
6470

71+
@Test
72+
public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScopeAttributeThenTranslatedToAuthorities() {
73+
Jwt jwt = jwt().claim("scope", "message:read message:write").build();
74+
75+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
76+
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
77+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
78+
79+
assertThat(authorities).containsExactly(
80+
new SimpleGrantedAuthority("message:read"),
81+
new SimpleGrantedAuthority("message:write"));
82+
}
83+
6584
@Test
6685
public void convertWhenTokenHasEmptyScopeAttributeThenTranslatedToNoAuthorities() {
6786
Jwt jwt = jwt().claim("scope", "").build();
@@ -97,6 +116,19 @@ public void convertWithCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslat
97116
new SimpleGrantedAuthority("ROLE_message:write"));
98117
}
99118

119+
@Test
120+
public void convertWithBlankAsCustomAuthorityPrefixWhenTokenHasScpAttributeThenTranslatedToAuthorities() {
121+
Jwt jwt = jwt().claim("scp", "message:read message:write").build();
122+
123+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
124+
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("");
125+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
126+
127+
assertThat(authorities).containsExactly(
128+
new SimpleGrantedAuthority("message:read"),
129+
new SimpleGrantedAuthority("message:write"));
130+
}
131+
100132
@Test
101133
public void convertWhenTokenHasEmptyScpAttributeThenTranslatedToNoAuthorities() {
102134
Jwt jwt = jwt().claim("scp", Collections.emptyList()).build();

0 commit comments

Comments
 (0)