|
23 | 23 | import io.jsonwebtoken.JwtException; |
24 | 24 | import io.jsonwebtoken.Jwts; |
25 | 25 | import io.jsonwebtoken.MalformedJwtException; |
| 26 | +import io.jsonwebtoken.SigningKeyResolverAdapter; |
26 | 27 | import io.jsonwebtoken.UnsupportedJwtException; |
27 | 28 | import io.jsonwebtoken.security.Keys; |
28 | 29 | import io.jsonwebtoken.security.MacAlgorithm; |
@@ -100,18 +101,21 @@ public Set<String> getUserGroupsFromToken(final Jws<Claims> jws) throws JwtExcep |
100 | 101 |
|
101 | 102 | private Jws<Claims> parseTokenFromBase64EncodedString(final String base64EncodedToken) throws JwtException { |
102 | 103 | try { |
103 | | - return Jwts.parser().keyLocator(header -> { |
104 | | - if (header instanceof JwsHeader) { |
105 | | - final String keyId = (String) header.get(KEY_ID_CLAIM); |
| 104 | + return Jwts.parser().setSigningKeyResolver(new SigningKeyResolverAdapter() { |
| 105 | + @Override |
| 106 | + public byte[] resolveSigningKeyBytes(JwsHeader header, Claims claims) { |
| 107 | + final String identity = claims.getSubject(); |
| 108 | + |
| 109 | + // Get the key based on the key id in the claims |
| 110 | + final String keyId = claims.get(KEY_ID_CLAIM, String.class); |
106 | 111 | final Key key = keyService.getKey(keyId); |
107 | 112 |
|
108 | 113 | // Ensure we were able to find a key that was previously issued by this key service for this user |
109 | 114 | if (key == null || key.getKey() == null) { |
110 | | - throw new UnsupportedJwtException("Unable to determine signing key for kid: " + keyId); |
| 115 | + throw new UnsupportedJwtException("Unable to determine signing key for " + identity + " [kid: " + keyId + "]"); |
111 | 116 | } |
112 | | - return Keys.hmacShaKeyFor(key.getKey().getBytes(StandardCharsets.UTF_8)); |
113 | | - } else { |
114 | | - throw new UnsupportedJwtException("JWE is not currently supported"); |
| 117 | + |
| 118 | + return key.getKey().getBytes(StandardCharsets.UTF_8); |
115 | 119 | } |
116 | 120 | }).build().parseSignedClaims(base64EncodedToken); |
117 | 121 | } catch (final MalformedJwtException | UnsupportedJwtException | SignatureException | ExpiredJwtException | IllegalArgumentException e) { |
|
0 commit comments