Skip to content

Commit d843818

Browse files
committed
Polish JwtGrantedAuthoritiesConverter
Rework the implementation so that it is clearer that authorities are derived from a single claim. Issue: gh-6273
1 parent 09a1199 commit d843818

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package org.springframework.security.oauth2.server.resource.authentication;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.Collections;
22-
import java.util.stream.Collectors;
2323

2424
import org.springframework.core.convert.converter.Converter;
2525
import org.springframework.security.core.GrantedAuthority;
@@ -35,42 +35,51 @@
3535
* @since 5.2
3636
*/
3737
public final class JwtGrantedAuthoritiesConverter implements Converter<Jwt, Collection<GrantedAuthority>> {
38-
private static final String SCOPE_AUTHORITY_PREFIX = "SCOPE_";
38+
private static final String DEFAULT_AUTHORITY_PREFIX = "SCOPE_";
3939

40-
private static final Collection<String> WELL_KNOWN_SCOPE_ATTRIBUTE_NAMES =
40+
private static final Collection<String> WELL_KNOWN_AUTHORITIES_CLAIM_NAMES =
4141
Arrays.asList("scope", "scp");
4242

4343
/**
44-
* Extracts the authorities
44+
* Extract {@link GrantedAuthority}s from the given {@link Jwt}.
45+
*
4546
* @param jwt The {@link Jwt} token
4647
* @return The {@link GrantedAuthority authorities} read from the token scopes
4748
*/
4849
@Override
4950
public Collection<GrantedAuthority> convert(Jwt jwt) {
50-
return getScopes(jwt)
51-
.stream()
52-
.map(authority -> SCOPE_AUTHORITY_PREFIX + authority)
53-
.map(SimpleGrantedAuthority::new)
54-
.collect(Collectors.toList());
51+
Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
52+
for (String authority : getAuthorities(jwt)) {
53+
grantedAuthorities.add(new SimpleGrantedAuthority(DEFAULT_AUTHORITY_PREFIX + authority));
54+
}
55+
return grantedAuthorities;
5556
}
5657

57-
/**
58-
* Gets the scopes from a {@link Jwt} token
59-
* @param jwt The {@link Jwt} token
60-
* @return The scopes from the token
61-
*/
62-
private Collection<String> getScopes(Jwt jwt) {
63-
for ( String attributeName : WELL_KNOWN_SCOPE_ATTRIBUTE_NAMES ) {
64-
Object scopes = jwt.getClaims().get(attributeName);
65-
if (scopes instanceof String) {
66-
if (StringUtils.hasText((String) scopes)) {
67-
return Arrays.asList(((String) scopes).split(" "));
68-
} else {
69-
return Collections.emptyList();
70-
}
71-
} else if (scopes instanceof Collection) {
72-
return (Collection<String>) scopes;
58+
private String getAuthoritiesClaimName(Jwt jwt) {
59+
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) {
60+
if (jwt.containsClaim(claimName)) {
61+
return claimName;
62+
}
63+
}
64+
return null;
65+
}
66+
67+
private Collection<String> getAuthorities(Jwt jwt) {
68+
String claimName = getAuthoritiesClaimName(jwt);
69+
70+
if (claimName == null) {
71+
return Collections.emptyList();
72+
}
73+
74+
Object authorities = jwt.getClaim(claimName);
75+
if (authorities instanceof String) {
76+
if (StringUtils.hasText((String) authorities)) {
77+
return Arrays.asList(((String) authorities).split(" "));
78+
} else {
79+
return Collections.emptyList();
7380
}
81+
} else if (authorities instanceof Collection) {
82+
return (Collection<String>) authorities;
7483
}
7584

7685
return Collections.emptyList();

0 commit comments

Comments
 (0)