Skip to content

Commit 50bdaeb

Browse files
committed
Add AuthorityUtils Methods
This commit adds a couple of utility methods for working with authorities by type. Now that there are infrastructural authorities that Spring Secuirty works with directly, it's helpful to be able to filter them out of the authority list.
1 parent b31fdcd commit 50bdaeb

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

core/src/main/java/org/springframework/security/core/authority/AuthorityUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashSet;
2323
import java.util.List;
2424
import java.util.Set;
25+
import java.util.stream.Stream;
2526

2627
import org.springframework.security.core.GrantedAuthority;
2728
import org.springframework.util.Assert;
@@ -39,6 +40,8 @@ public final class AuthorityUtils {
3940

4041
public static final List<GrantedAuthority> NO_AUTHORITIES = Collections.emptyList();
4142

43+
private static String[] KNOWN_PREFIXES = { "ROLE_", "SCOPE_", "FACTOR_" };
44+
4245
private AuthorityUtils() {
4346
}
4447

@@ -93,4 +96,38 @@ public static List<GrantedAuthority> createAuthorityList(Collection<String> auth
9396
return grantedAuthorities;
9497
}
9598

99+
/**
100+
* Return a {@link Stream} containing only the authorities of the given type;
101+
* {@code "ROLE"}, {@code "SCOPE"}, or {@code "FACTOR"}.
102+
* @param type the authority type; {@code "ROLE"}, {@code "SCOPE"}, or
103+
* {@code "FACTOR"}
104+
* @param authorities the list of authorities
105+
* @return a {@link Stream} containing the authorities of the given type
106+
*/
107+
public static Stream<GrantedAuthority> authoritiesOfType(String type, Collection<GrantedAuthority> authorities) {
108+
return authorities.stream().filter((a) -> a.getAuthority().startsWith(type + "_"));
109+
}
110+
111+
/**
112+
* Return the simple name of a {@link GrantedAuthority}, which is its name, less any
113+
* common prefix; that is, {@code ROLE_}, {@code SCOPE_}, or {@code FACTOR_}.
114+
* <p>
115+
* For example, if the authority is {@code ROLE_USER}, then the simple name is
116+
* {@code user}.
117+
* <p>
118+
* If the authority is {@code FACTOR_PASSWORD}, then the simple name is
119+
* {@code password}.
120+
* @param authority the granted authority
121+
* @return the simple name of the authority
122+
*/
123+
public static String getSimpleName(GrantedAuthority authority) {
124+
String name = authority.getAuthority();
125+
for (String prefix : KNOWN_PREFIXES) {
126+
if (name.startsWith(prefix)) {
127+
return name.substring(prefix.length());
128+
}
129+
}
130+
return name;
131+
}
132+
96133
}

core/src/test/java/org/springframework/security/core/authority/AuthorityUtilsTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,38 @@ public void createAuthorityList() {
5454
assertThat(authorities).element(2).extracting(GrantedAuthority::getAuthority).isEqualTo("ROLE_C");
5555
}
5656

57+
@Test
58+
public void getSimpleNameWhenRoleThenRemovesPrefix() {
59+
GrantedAuthority role = new SimpleGrantedAuthority("ROLE_ADMIN");
60+
assertThat(AuthorityUtils.getSimpleName(role)).isEqualTo("ADMIN");
61+
}
62+
63+
@Test
64+
public void getSimpleNameWhenScopeThenRemovesPrefix() {
65+
GrantedAuthority role = new SimpleGrantedAuthority("SCOPE_message:read");
66+
assertThat(AuthorityUtils.getSimpleName(role)).isEqualTo("message:read");
67+
}
68+
69+
@Test
70+
public void getSimpleNameWhenFactorThenRemovesPrefix() {
71+
GrantedAuthority role = new SimpleGrantedAuthority("FACTOR_PASSWORD");
72+
assertThat(AuthorityUtils.getSimpleName(role)).isEqualTo("PASSWORD");
73+
}
74+
75+
@Test
76+
public void authoritiesOfTypeWhenEmptyThenReturnsEmptyStream() {
77+
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
78+
List<GrantedAuthority> factors = AuthorityUtils.authoritiesOfType("FACTOR", authorities).toList();
79+
assertThat(factors).isEmpty();
80+
}
81+
82+
@Test
83+
public void authoritiesOfTypeWhenFactorsThenReturnsOnlyFactors() {
84+
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER", "FACTOR_PASSWORD",
85+
"FACTOR_OTT");
86+
List<GrantedAuthority> factors = AuthorityUtils.authoritiesOfType("FACTOR", authorities).toList();
87+
assertThat(factors).extracting(GrantedAuthority::getAuthority)
88+
.containsExactlyInAnyOrder("FACTOR_PASSWORD", "FACTOR_OTT");
89+
}
90+
5791
}

0 commit comments

Comments
 (0)