Skip to content

Commit 8f18516

Browse files
committed
alternative group claim
1 parent 863e920 commit 8f18516

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/org/apache/geronimo/microprofile/impl/jwtauth/servlet/GeronimoJwtAuthFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
public class GeronimoJwtAuthFilter implements Filter {
4242
private String headerName;
4343
private String cookieName;
44+
private String groupsName;
4445
private String prefix;
4546
private JwtParser service;
4647
private GeronimoJwtAuthExtension extension;
@@ -55,6 +56,7 @@ public void init(final FilterConfig filterConfig) {
5556
final GeronimoJwtAuthConfig config = current.select(GeronimoJwtAuthConfig.class).get();
5657
headerName = config.read("header.name", "Authorization");
5758
cookieName = config.read("cookie.name", "Bearer");
59+
groupsName = config.read("groups.name", "");
5860
prefix = Optional.of(config.read("header.prefix", "bearer"))
5961
.filter(s -> !s.isEmpty()).map(s -> s + " ")
6062
.orElse("");
@@ -81,7 +83,7 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
8183
}
8284

8385
try {
84-
final JwtRequest req = new JwtRequest(service, headerName, cookieName, prefix, httpServletRequest);
86+
final JwtRequest req = new JwtRequest(service, headerName, cookieName, groupsName, prefix, httpServletRequest);
8587
extension.execute(req.asTokenAccessor(), () -> chain.doFilter(req, response));
8688
} catch (final Exception e) { // when not used with JAX-RS but directly Servlet
8789
final HttpServletResponse httpServletResponse = HttpServletResponse.class.cast(response);

src/main/java/org/apache/geronimo/microprofile/impl/jwtauth/servlet/JwtRequest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818

1919
import static java.util.Collections.emptySet;
2020
import static java.util.stream.Collectors.toList;
21+
import static java.util.stream.Collectors.toSet;
2122

2223
import java.security.Principal;
24+
import java.util.Collections;
2325
import java.util.LinkedHashSet;
2426
import java.util.Locale;
2527
import java.util.Set;
2628
import java.util.concurrent.Callable;
2729
import java.util.function.Supplier;
2830
import java.util.stream.Stream;
2931

32+
import javax.json.JsonArray;
33+
import javax.json.JsonString;
34+
import javax.json.JsonValue;
3035
import javax.security.auth.Subject;
3136
import javax.servlet.http.Cookie;
3237
import javax.servlet.http.HttpServletRequest;
@@ -40,12 +45,14 @@
4045
public class JwtRequest extends HttpServletRequestWrapper implements TokenAccessor {
4146
private final Supplier<JsonWebToken> tokenExtractor;
4247
private final String headerName;
48+
private final String groupsName;
4349
private volatile JsonWebToken token; // cache for perf reasons
4450

45-
public JwtRequest(final JwtParser service, final String header, final String cookie,
51+
public JwtRequest(final JwtParser service, final String header, final String cookie, final String groupsName,
4652
final String prefix, final HttpServletRequest request) {
4753
super(request);
4854
this.headerName = header;
55+
this.groupsName = groupsName;
4956

5057
this.tokenExtractor = () -> {
5158
if (token != null) {
@@ -132,6 +139,20 @@ public Principal getUserPrincipal() {
132139

133140
@Override
134141
public boolean isUserInRole(final String role) {
142+
if (tokenExtractor.get().containsClaim(groupsName)) {
143+
JsonValue jsonValue = tokenExtractor.get().getClaim(groupsName);
144+
Set<String> groups = Collections.EMPTY_SET;
145+
if (jsonValue.getValueType() == JsonValue.ValueType.ARRAY) {
146+
groups = JsonArray.class.cast(jsonValue).stream()
147+
.map(grp -> ((JsonString)grp).getString())
148+
.collect(toSet());
149+
} else if (jsonValue.getValueType() == JsonValue.ValueType.STRING){
150+
groups = Stream.of(JsonString.class.cast(jsonValue).getString().split(","))
151+
.collect(toSet());
152+
}
153+
return groups.stream().anyMatch(v -> v.equals(role));
154+
}
155+
135156
return tokenExtractor.get().getGroups().contains(role);
136157
}
137158

0 commit comments

Comments
 (0)