@@ -99,9 +99,7 @@ public Verification withAudience(String... audience) {
9999 * @throws IllegalArgumentException if leeway is negative.
100100 */
101101 public Verification acceptLeeway (long leeway ) throws IllegalArgumentException {
102- if (leeway < 0 ) {
103- throw new IllegalArgumentException ("Leeway value can't be negative." );
104- }
102+ assertPositive (leeway );
105103 this .defaultLeeway = leeway ;
106104 return this ;
107105 }
@@ -115,9 +113,7 @@ public Verification acceptLeeway(long leeway) throws IllegalArgumentException {
115113 * @throws IllegalArgumentException if leeway is negative.
116114 */
117115 public Verification acceptExpiresAt (long leeway ) throws IllegalArgumentException {
118- if (leeway < 0 ) {
119- throw new IllegalArgumentException ("Leeway value can't be negative." );
120- }
116+ assertPositive (leeway );
121117 requireClaim (PublicClaims .EXPIRES_AT , leeway );
122118 return this ;
123119 }
@@ -131,9 +127,7 @@ public Verification acceptExpiresAt(long leeway) throws IllegalArgumentException
131127 * @throws IllegalArgumentException if leeway is negative.
132128 */
133129 public Verification acceptNotBefore (long leeway ) throws IllegalArgumentException {
134- if (leeway < 0 ) {
135- throw new IllegalArgumentException ("Leeway value can't be negative." );
136- }
130+ assertPositive (leeway );
137131 requireClaim (PublicClaims .NOT_BEFORE , leeway );
138132 return this ;
139133 }
@@ -147,9 +141,7 @@ public Verification acceptNotBefore(long leeway) throws IllegalArgumentException
147141 * @throws IllegalArgumentException if leeway is negative.
148142 */
149143 public Verification acceptIssuedAt (long leeway ) throws IllegalArgumentException {
150- if (leeway < 0 ) {
151- throw new IllegalArgumentException ("Leeway value can't be negative." );
152- }
144+ assertPositive (leeway );
153145 requireClaim (PublicClaims .ISSUED_AT , leeway );
154146 return this ;
155147 }
@@ -173,15 +165,96 @@ public Verification withJWTId(String jwtId) {
173165 * @return this same Verification instance.
174166 * @throws IllegalArgumentException if the name is null.
175167 */
176- public Verification withClaim (String name , Object value ) throws IllegalArgumentException {
177- if (name == null ) {
178- throw new IllegalArgumentException ("The Custom Claim's name can't be null." );
179- }
168+ public Verification withClaim (String name , Boolean value ) throws IllegalArgumentException {
169+ assertNonNull (name );
170+ requireClaim (name , value );
171+ return this ;
172+ }
173+
174+ /**
175+ * Require a specific Claim value.
176+ *
177+ * @param name the Claim's name.
178+ * @param value the Claim's value.
179+ * @return this same Verification instance.
180+ * @throws IllegalArgumentException if the name is null.
181+ */
182+ public Verification withClaim (String name , Integer value ) throws IllegalArgumentException {
183+ assertNonNull (name );
184+ requireClaim (name , value );
185+ return this ;
186+ }
180187
188+ /**
189+ * Require a specific Claim value.
190+ *
191+ * @param name the Claim's name.
192+ * @param value the Claim's value.
193+ * @return this same Verification instance.
194+ * @throws IllegalArgumentException if the name is null.
195+ */
196+ public Verification withClaim (String name , Double value ) throws IllegalArgumentException {
197+ assertNonNull (name );
181198 requireClaim (name , value );
182199 return this ;
183200 }
184201
202+ /**
203+ * Require a specific Claim value.
204+ *
205+ * @param name the Claim's name.
206+ * @param value the Claim's value.
207+ * @return this same Verification instance.
208+ * @throws IllegalArgumentException if the name is null.
209+ */
210+ public Verification withClaim (String name , String value ) throws IllegalArgumentException {
211+ assertNonNull (name );
212+ requireClaim (name , value );
213+ return this ;
214+ }
215+
216+ /**
217+ * Require a specific Claim value.
218+ *
219+ * @param name the Claim's name.
220+ * @param value the Claim's value.
221+ * @return this same Verification instance.
222+ * @throws IllegalArgumentException if the name is null.
223+ */
224+ public Verification withClaim (String name , Date value ) throws IllegalArgumentException {
225+ assertNonNull (name );
226+ requireClaim (name , value );
227+ return this ;
228+ }
229+
230+ /**
231+ * Require a specific Array Claim to contain at least the given items.
232+ *
233+ * @param name the Claim's name.
234+ * @param items the items the Claim must contain.
235+ * @return this same Verification instance.
236+ * @throws IllegalArgumentException if the name is null.
237+ */
238+ public Verification withArrayClaim (String name , String ... items ) throws IllegalArgumentException {
239+ assertNonNull (name );
240+ requireClaim (name , items );
241+ return this ;
242+ }
243+
244+ /**
245+ * Require a specific Array Claim to contain at least the given items.
246+ *
247+ * @param name the Claim's name.
248+ * @param items the items the Claim must contain.
249+ * @return this same Verification instance.
250+ * @throws IllegalArgumentException if the name is null.
251+ */
252+ public Verification withArrayClaim (String name , Integer ... items ) throws IllegalArgumentException {
253+ assertNonNull (name );
254+ requireClaim (name , items );
255+ return this ;
256+ }
257+
185258 /**
186259 * Creates a new and reusable instance of the JWTVerifier with the configuration already provided.
187260 *
@@ -203,6 +276,18 @@ JWTVerifier build(Clock clock) {
203276 return new JWTVerifier (algorithm , claims , clock );
204277 }
205278
279+ private void assertPositive (long leeway ) {
280+ if (leeway < 0 ) {
281+ throw new IllegalArgumentException ("Leeway value can't be negative." );
282+ }
283+ }
284+
285+ private void assertNonNull (String name ) {
286+ if (name == null ) {
287+ throw new IllegalArgumentException ("The Custom Claim's name can't be null." );
288+ }
289+ }
290+
206291 private void addLeewayToDateClaims () {
207292 if (!claims .containsKey (PublicClaims .EXPIRES_AT )) {
208293 claims .put (PublicClaims .EXPIRES_AT , defaultLeeway );
@@ -296,8 +381,10 @@ private void assertValidClaim(Claim claim, String claimName, Object value) {
296381 isValid = value .equals (claim .asDouble ());
297382 } else if (value instanceof Date ) {
298383 isValid = value .equals (claim .asDate ());
299- } else {
300- isValid = Objects .deepEquals (value , claim .as (value .getClass ()));
384+ } else if (value instanceof Object []) {
385+ List <Object > claimArr = Arrays .asList (claim .as (Object [].class ));
386+ List <Object > valueArr = Arrays .asList ((Object []) value );
387+ isValid = claimArr .containsAll (valueArr );
301388 }
302389
303390 if (!isValid ) {
0 commit comments