33import com .auth0 .jwtdecodejava .algorithms .Algorithm ;
44import com .auth0 .jwtdecodejava .exceptions .*;
55import com .auth0 .jwtdecodejava .impl .PublicClaims ;
6- import com .auth0 .jwtdecodejava .interfaces .JWT ;
76
8- import java .util .Arrays ;
9- import java .util .Date ;
10- import java .util .HashMap ;
11- import java .util .Map ;
7+ import java .util .*;
128
139/**
1410 * 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.
1511 */
16- public class JWTVerifier {
12+ class JWTVerifier {
1713 private final Algorithm algorithm ;
1814 private final Map <String , Object > claims ;
1915
20- private JWTVerifier (Algorithm algorithm ) throws IllegalArgumentException {
21- if (algorithm == null ) {
22- throw new IllegalArgumentException ("The Algorithm cannot be null." );
23- }
16+ private JWTVerifier (Algorithm algorithm , Map <String , Object > claims ) {
2417 this .algorithm = algorithm ;
25- this .claims = new HashMap <>( );
18+ this .claims = Collections . unmodifiableMap ( claims );
2619 }
2720
2821 /**
2922 * Initialize a JWTVerifier instance using a HS Algorithm.
3023 *
31- * @param algorithm a HSAlgorithm. Valid values are HS256, HS384, HS512 .
24+ * @param algorithm the Algorithm to use on the JWT verification .
3225 * @return a JWTVerifier instance to configure.
33- * @throws IllegalArgumentException if the provided algorithm is null or if the secret is null .
26+ * @throws IllegalArgumentException if the provided algorithm is null.
3427 */
35- public static JWTVerifier init (Algorithm algorithm ) throws IllegalArgumentException {
36- return new JWTVerifier (algorithm );
28+ static JWTVerifier . Verification init (Algorithm algorithm ) throws IllegalArgumentException {
29+ return new Verification (algorithm );
3730 }
3831
39-
4032 /**
41- * Require a specific Issuer ("iss") claim.
42- *
43- * @return this same JWTVerifier instance.
33+ * The Verification class holds the Claims required by a JWT to be valid.
4434 */
45- public JWTVerifier withIssuer (String issuer ) {
46- requireClaim (PublicClaims .ISSUER , issuer );
47- return this ;
48- }
35+ static class Verification {
36+ private final Algorithm algorithm ;
37+ private final Map <String , Object > claims ;
4938
50- /**
51- * Require a specific Subject ("sub") claim.
52- *
53- * @return this same JWTVerifier instance.
54- */
55- public JWTVerifier withSubject (String subject ) {
56- requireClaim (PublicClaims .SUBJECT , subject );
57- return this ;
58- }
39+ Verification (Algorithm algorithm ) throws IllegalArgumentException {
40+ if (algorithm == null ) {
41+ throw new IllegalArgumentException ("The Algorithm cannot be null." );
42+ }
5943
60- /**
61- * Require a specific Audience ("aud") claim.
62- *
63- * @return this same JWTVerifier instance.
64- */
65- public JWTVerifier withAudience (String [] audience ) {
66- requireClaim (PublicClaims .AUDIENCE , audience );
67- return this ;
68- }
44+ this .algorithm = algorithm ;
45+ this .claims = new HashMap <>();
46+ }
6947
70- /**
71- * Require a specific Expires At ("exp ") claim.
72- *
73- * @return this same JWTVerifier instance.
74- */
75- public JWTVerifier withExpiresAt ( Date expiresAt ) {
76- requireClaim (PublicClaims .EXPIRES_AT , expiresAt );
77- return this ;
78- }
48+ /**
49+ * Require a specific Issuer ("iss ") claim.
50+ *
51+ * @return this same Verification instance.
52+ */
53+ public Verification withIssuer ( String issuer ) {
54+ requireClaim (PublicClaims .ISSUER , issuer );
55+ return this ;
56+ }
7957
80- /**
81- * Require a specific Not Before ("nbf ") claim.
82- *
83- * @return this same JWTVerifier instance.
84- */
85- public JWTVerifier withNotBefore ( Date notBefore ) {
86- requireClaim (PublicClaims .NOT_BEFORE , notBefore );
87- return this ;
88- }
58+ /**
59+ * Require a specific Subject ("sub ") claim.
60+ *
61+ * @return this same Verification instance.
62+ */
63+ public Verification withSubject ( String subject ) {
64+ requireClaim (PublicClaims .SUBJECT , subject );
65+ return this ;
66+ }
8967
90- /**
91- * Require a specific Issued At ("iat ") claim.
92- *
93- * @return this same JWTVerifier instance.
94- */
95- public JWTVerifier withIssuedAt ( Date issuedAt ) {
96- requireClaim (PublicClaims .ISSUED_AT , issuedAt );
97- return this ;
98- }
68+ /**
69+ * Require a specific Audience ("aud ") claim.
70+ *
71+ * @return this same Verification instance.
72+ */
73+ public Verification withAudience ( String [] audience ) {
74+ requireClaim (PublicClaims .AUDIENCE , audience );
75+ return this ;
76+ }
9977
100- /**
101- * Require a specific JWT Id ("jti") claim.
102- *
103- * @return this same JWTVerifier instance.
104- */
105- public JWTVerifier withJWTId (String jwtId ) {
106- requireClaim (PublicClaims .JWT_ID , jwtId );
107- return this ;
78+ /**
79+ * Require a specific Expires At ("exp") claim.
80+ *
81+ * @return this same Verification instance.
82+ */
83+ public Verification withExpiresAt (Date expiresAt ) {
84+ requireClaim (PublicClaims .EXPIRES_AT , expiresAt );
85+ return this ;
86+ }
87+
88+ /**
89+ * Require a specific Not Before ("nbf") claim.
90+ *
91+ * @return this same Verification instance.
92+ */
93+ public Verification withNotBefore (Date notBefore ) {
94+ requireClaim (PublicClaims .NOT_BEFORE , notBefore );
95+ return this ;
96+ }
97+
98+ /**
99+ * Require a specific Issued At ("iat") claim.
100+ *
101+ * @return this same Verification instance.
102+ */
103+ public Verification withIssuedAt (Date issuedAt ) {
104+ requireClaim (PublicClaims .ISSUED_AT , issuedAt );
105+ return this ;
106+ }
107+
108+ /**
109+ * Require a specific JWT Id ("jti") claim.
110+ *
111+ * @return this same Verification instance.
112+ */
113+ public Verification withJWTId (String jwtId ) {
114+ requireClaim (PublicClaims .JWT_ID , jwtId );
115+ return this ;
116+ }
117+
118+ /**
119+ * Creates a new and reusable instance of the JWTVerifier with the configuration already provided.
120+ *
121+ * @return a new JWTVerifier instance.
122+ */
123+ public JWTVerifier build () {
124+ return new JWTVerifier (algorithm , claims );
125+ }
126+
127+ private void requireClaim (String name , Object value ) {
128+ if (value == null ) {
129+ claims .remove (name );
130+ return ;
131+ }
132+ claims .put (name , value );
133+ }
108134 }
109135
136+
110137 /**
111138 * Perform the verification against the given Token, using any previous configured options.
112139 *
@@ -116,7 +143,7 @@ public JWTVerifier withJWTId(String jwtId) {
116143 * @throws JWTVerificationException if any of the required contents inside the JWT is invalid.
117144 */
118145 public JWT verify (String token ) throws JWTDecodeException , JWTVerificationException {
119- JWT jwt = JWTDecoder .decode (token );
146+ JWT jwt = new JWT ( JWTDecoder .decode (token ) );
120147 verifyAlgorithm (jwt , algorithm );
121148 verifySignature (SignUtils .splitToken (token ));
122149 verifyClaims (jwt , claims );
@@ -155,12 +182,4 @@ private void assertValidClaim(JWT jwt, String claimName, Object expectedValue) t
155182 throw new InvalidClaimException (String .format ("The Claim '%s' value doesn't match the required one." , claimName ));
156183 }
157184 }
158-
159- private void requireClaim (String name , Object value ) {
160- if (value == null ) {
161- claims .remove (name );
162- return ;
163- }
164- claims .put (name , value );
165- }
166185}
0 commit comments