11package com .auth0 .jwtdecodejava ;
22
33import 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 ;
4+ import com .auth0 .jwtdecodejava .exceptions .*;
115import com .auth0 .jwtdecodejava .impl .PublicClaims ;
126import com .auth0 .jwtdecodejava .interfaces .JWT ;
137
14- import java .security .InvalidKeyException ;
15- import java .security .NoSuchAlgorithmException ;
16- import java .security .PublicKey ;
17- import java .security .SignatureException ;
188import java .util .Arrays ;
199import java .util .Date ;
2010import java .util .HashMap ;
2111import java .util .Map ;
2212
23- import static com .auth0 .jwtdecodejava .algorithms .NoneAlgorithm .none ;
24-
2513/**
2614 * 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.
2715 */
2816public class JWTVerifier {
2917 private final Algorithm algorithm ;
30- private final String secret ;
31- private final PublicKey key ;
3218 private final Map <String , Object > claims ;
3319
34- private JWTVerifier (Algorithm algorithm , String secret , PublicKey key ) {
20+ private JWTVerifier (Algorithm algorithm ) throws IllegalArgumentException {
21+ if (algorithm == null ) {
22+ throw new IllegalArgumentException ("The Algorithm cannot be null." );
23+ }
3524 this .algorithm = algorithm ;
36- this .key = key ;
37- this .secret = secret ;
3825 this .claims = new HashMap <>();
3926 }
4027
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-
5028 /**
5129 * Initialize a JWTVerifier instance using a HS Algorithm.
5230 *
5331 * @param algorithm a HSAlgorithm. Valid values are HS256, HS384, HS512.
54- * @param secret to use when verifying the signature.
5532 * @return a JWTVerifier instance to configure.
5633 * @throws IllegalArgumentException if the provided algorithm is null or if the secret is null.
5734 */
58- public static JWTVerifier init (HSAlgorithm algorithm , String secret ) throws IllegalArgumentException {
59- return init (algorithm , null , secret );
35+ public static JWTVerifier init (Algorithm algorithm ) throws IllegalArgumentException {
36+ return new JWTVerifier (algorithm );
6037 }
6138
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- }
8639
8740 /**
8841 * Require a specific Issuer ("iss") claim.
@@ -159,9 +112,10 @@ public JWTVerifier withJWTId(String jwtId) {
159112 *
160113 * @param token the String representation of the JWT.
161114 * @return a verified JWT.
115+ * @throws JWTDecodeException if any part of the Token contained an invalid JWT or JSON format.
162116 * @throws JWTVerificationException if any of the required contents inside the JWT is invalid.
163117 */
164- public JWT verify (String token ) throws JWTVerificationException {
118+ public JWT verify (String token ) throws JWTDecodeException , JWTVerificationException {
165119 JWT jwt = JWTDecoder .decode (token );
166120 verifyAlgorithm (jwt , algorithm );
167121 verifySignature (SignUtils .splitToken (token ));
@@ -170,25 +124,11 @@ public JWT verify(String token) throws JWTVerificationException {
170124 }
171125
172126 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- }
127+ algorithm .verify (parts );
188128 }
189129
190130 private void verifyAlgorithm (JWT jwt , Algorithm expectedAlgorithm ) throws AlgorithmMismatchException {
191- if (!expectedAlgorithm .equals (jwt .getAlgorithm ())) {
131+ if (!expectedAlgorithm .getName (). equals (jwt .getAlgorithm ())) {
192132 throw new AlgorithmMismatchException ("The provided Algorithm doesn't match the one defined in the JWT's Header." );
193133 }
194134 }
0 commit comments