|
| 1 | +package com.auth0.jwtdecodejava; |
| 2 | + |
| 3 | +import com.auth0.jwtdecodejava.algorithms.Algorithm; |
| 4 | +import com.auth0.jwtdecodejava.algorithms.HSAlgorithm; |
| 5 | +import com.auth0.jwtdecodejava.algorithms.NoneAlgorithm; |
| 6 | +import com.auth0.jwtdecodejava.algorithms.RSAlgorithm; |
| 7 | +import com.auth0.jwtdecodejava.exceptions.AlgorithmMismatchException; |
| 8 | +import com.auth0.jwtdecodejava.exceptions.InvalidClaimException; |
| 9 | +import com.auth0.jwtdecodejava.exceptions.JWTVerificationException; |
| 10 | +import com.auth0.jwtdecodejava.exceptions.SignatureVerificationException; |
| 11 | +import com.auth0.jwtdecodejava.impl.PublicClaims; |
| 12 | +import com.auth0.jwtdecodejava.interfaces.JWT; |
| 13 | + |
| 14 | +import java.security.InvalidKeyException; |
| 15 | +import java.security.NoSuchAlgorithmException; |
| 16 | +import java.security.PublicKey; |
| 17 | +import java.security.SignatureException; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Date; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static com.auth0.jwtdecodejava.algorithms.NoneAlgorithm.none; |
| 24 | + |
| 25 | +/** |
| 26 | + * The JWTVerifier class holds the verify method to assert that a given Token has not only a proper JWT format, but also it's signature matches. |
| 27 | + */ |
| 28 | +public class JWTVerifier { |
| 29 | + private final Algorithm algorithm; |
| 30 | + private final String secret; |
| 31 | + private final PublicKey key; |
| 32 | + private final Map<String, Object> claims; |
| 33 | + |
| 34 | + private JWTVerifier(Algorithm algorithm, String secret, PublicKey key) { |
| 35 | + this.algorithm = algorithm; |
| 36 | + this.key = key; |
| 37 | + this.secret = secret; |
| 38 | + this.claims = new HashMap<>(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Initialize a JWTVerifier instance using the Algorithm "none". |
| 43 | + * |
| 44 | + * @return a JWTVerifier instance to configure. |
| 45 | + */ |
| 46 | + public static JWTVerifier init() { |
| 47 | + return init(none, null, null); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Initialize a JWTVerifier instance using a HS Algorithm. |
| 52 | + * |
| 53 | + * @param algorithm a HSAlgorithm. Valid values are HS256, HS384, HS512. |
| 54 | + * @param secret to use when verifying the signature. |
| 55 | + * @return a JWTVerifier instance to configure. |
| 56 | + * @throws IllegalArgumentException if the provided algorithm is null or if the secret is null. |
| 57 | + */ |
| 58 | + public static JWTVerifier init(HSAlgorithm algorithm, String secret) throws IllegalArgumentException { |
| 59 | + return init(algorithm, null, secret); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Initialize a JWTVerifier instance using a RS Algorithm. |
| 64 | + * |
| 65 | + * @param algorithm a RSAlgorithm. Valid values are RS256, RS384, RS512. |
| 66 | + * @param publicKey to use when verifying the signature. |
| 67 | + * @return a JWTVerifier instance to configure. |
| 68 | + * @throws IllegalArgumentException if the provided algorithm is null or if the publicKey is null. |
| 69 | + */ |
| 70 | + public static JWTVerifier init(RSAlgorithm algorithm, PublicKey publicKey) throws IllegalArgumentException { |
| 71 | + return init(algorithm, publicKey, null); |
| 72 | + } |
| 73 | + |
| 74 | + private static JWTVerifier init(Algorithm algorithm, PublicKey publicKey, String secret) throws IllegalArgumentException { |
| 75 | + if (algorithm == null) { |
| 76 | + throw new IllegalArgumentException("The Algorithm cannot be null."); |
| 77 | + } |
| 78 | + if (algorithm instanceof HSAlgorithm && secret == null) { |
| 79 | + throw new IllegalArgumentException(String.format("You can't use the %s algorithm without providing a valid Secret.", algorithm.name())); |
| 80 | + } |
| 81 | + if (algorithm instanceof RSAlgorithm && publicKey == null) { |
| 82 | + throw new IllegalArgumentException(String.format("You can't use the %s algorithm without providing a valid PublicKey.", algorithm.name())); |
| 83 | + } |
| 84 | + return new JWTVerifier(algorithm, secret, publicKey); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Require a specific Issuer ("iss") claim. |
| 89 | + * |
| 90 | + * @return this same JWTVerifier instance. |
| 91 | + */ |
| 92 | + public JWTVerifier withIssuer(String issuer) { |
| 93 | + requireClaim(PublicClaims.ISSUER, issuer); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Require a specific Subject ("sub") claim. |
| 99 | + * |
| 100 | + * @return this same JWTVerifier instance. |
| 101 | + */ |
| 102 | + public JWTVerifier withSubject(String subject) { |
| 103 | + requireClaim(PublicClaims.SUBJECT, subject); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Require a specific Audience ("aud") claim. |
| 109 | + * |
| 110 | + * @return this same JWTVerifier instance. |
| 111 | + */ |
| 112 | + public JWTVerifier withAudience(String[] audience) { |
| 113 | + requireClaim(PublicClaims.AUDIENCE, audience); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Require a specific Expires At ("exp") claim. |
| 119 | + * |
| 120 | + * @return this same JWTVerifier instance. |
| 121 | + */ |
| 122 | + public JWTVerifier withExpiresAt(Date expiresAt) { |
| 123 | + requireClaim(PublicClaims.EXPIRES_AT, expiresAt); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Require a specific Not Before ("nbf") claim. |
| 129 | + * |
| 130 | + * @return this same JWTVerifier instance. |
| 131 | + */ |
| 132 | + public JWTVerifier withNotBefore(Date notBefore) { |
| 133 | + requireClaim(PublicClaims.NOT_BEFORE, notBefore); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Require a specific Issued At ("iat") claim. |
| 139 | + * |
| 140 | + * @return this same JWTVerifier instance. |
| 141 | + */ |
| 142 | + public JWTVerifier withIssuedAt(Date issuedAt) { |
| 143 | + requireClaim(PublicClaims.ISSUED_AT, issuedAt); |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Require a specific JWT Id ("jti") claim. |
| 149 | + * |
| 150 | + * @return this same JWTVerifier instance. |
| 151 | + */ |
| 152 | + public JWTVerifier withJWTId(String jwtId) { |
| 153 | + requireClaim(PublicClaims.JWT_ID, jwtId); |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Perform the verification against the given Token, using any previous configured options. |
| 159 | + * |
| 160 | + * @param token the String representation of the JWT. |
| 161 | + * @return a verified JWT. |
| 162 | + * @throws JWTVerificationException if any of the required contents inside the JWT is invalid. |
| 163 | + */ |
| 164 | + public JWT verify(String token) throws JWTVerificationException { |
| 165 | + JWT jwt = JWTDecoder.decode(token); |
| 166 | + verifyAlgorithm(jwt, algorithm); |
| 167 | + verifySignature(SignUtils.splitToken(token)); |
| 168 | + verifyClaims(jwt, claims); |
| 169 | + return jwt; |
| 170 | + } |
| 171 | + |
| 172 | + private void verifySignature(String[] parts) throws SignatureVerificationException { |
| 173 | + if (algorithm instanceof HSAlgorithm) { |
| 174 | + try { |
| 175 | + SignUtils.verifyHS((HSAlgorithm) algorithm, parts, secret); |
| 176 | + } catch (NoSuchAlgorithmException | InvalidKeyException e) { |
| 177 | + throw new SignatureVerificationException(algorithm, e); |
| 178 | + } |
| 179 | + } else if (algorithm instanceof RSAlgorithm) { |
| 180 | + try { |
| 181 | + SignUtils.verifyRS((RSAlgorithm) algorithm, parts, key); |
| 182 | + } catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException e) { |
| 183 | + throw new SignatureVerificationException(algorithm, e); |
| 184 | + } |
| 185 | + } else if (algorithm instanceof NoneAlgorithm && !parts[2].isEmpty()) { |
| 186 | + throw new SignatureVerificationException(algorithm); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private void verifyAlgorithm(JWT jwt, Algorithm expectedAlgorithm) throws AlgorithmMismatchException { |
| 191 | + if (!expectedAlgorithm.equals(jwt.getAlgorithm())) { |
| 192 | + throw new AlgorithmMismatchException("The provided Algorithm doesn't match the one defined in the JWT's Header."); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private void verifyClaims(JWT jwt, Map<String, Object> claims) { |
| 197 | + for (Map.Entry<String, Object> entry : claims.entrySet()) { |
| 198 | + assertValidClaim(jwt, entry.getKey(), entry.getValue()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private void assertValidClaim(JWT jwt, String claimName, Object expectedValue) throws InvalidClaimException { |
| 203 | + boolean isValid; |
| 204 | + if (PublicClaims.AUDIENCE.equals(claimName)) { |
| 205 | + isValid = Arrays.equals(jwt.getAudience(), (String[]) expectedValue); |
| 206 | + } else if (PublicClaims.NOT_BEFORE.equals(claimName) || PublicClaims.EXPIRES_AT.equals(claimName) || PublicClaims.ISSUED_AT.equals(claimName)) { |
| 207 | + Date dateValue = (Date) expectedValue; |
| 208 | + isValid = dateValue.equals(jwt.getClaim(claimName).asDate()); |
| 209 | + } else { |
| 210 | + String stringValue = (String) expectedValue; |
| 211 | + isValid = stringValue.equals(jwt.getClaim(claimName).asString()); |
| 212 | + } |
| 213 | + |
| 214 | + if (!isValid) { |
| 215 | + throw new InvalidClaimException(String.format("The Claim '%s' value doesn't match the required one.", claimName)); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private void requireClaim(String name, Object value) { |
| 220 | + if (value == null) { |
| 221 | + claims.remove(name); |
| 222 | + return; |
| 223 | + } |
| 224 | + claims.put(name, value); |
| 225 | + } |
| 226 | +} |
0 commit comments