11package com .bastiaanjansen .jwt ;
22
3- import com .bastiaanjansen .jwt .Exceptions .JWTExpiredException ;
4- import com .bastiaanjansen .jwt .Exceptions .JWTValidationException ;
3+ import com .bastiaanjansen .jwt .exceptions .JWTExpiredException ;
4+ import com .bastiaanjansen .jwt .exceptions .JWTValidationException ;
55
66import java .nio .charset .StandardCharsets ;
77import java .util .*;
@@ -68,15 +68,15 @@ private void verifyPayload(Payload payload) throws JWTValidationException {
6868
6969 private void validateNotBefore (Payload payload , Date currentDate ) throws JWTValidationException {
7070 // Checks that if the not-before (nbf) claim is set, the current date is after or equal to the not-before date.
71- if (payload .containsClaim (Payload .Registered .NOT_BEFORE )) {
71+ if (payload .containsClaim (Claims .Registered .NOT_BEFORE . getValue () )) {
7272 Date notBefore = payload .getNotBefore ();
7373 if (currentDate .getTime () <= notBefore .getTime ())
7474 throw new JWTValidationException ("JWT is only valid after " + notBefore );
7575 }
7676 }
7777
7878 private void validateExpirationTime (Payload payload , Date currentDate ) throws JWTExpiredException {
79- if (payload .containsClaim (Payload .Registered .EXPIRATION_TIME )) {
79+ if (payload .containsClaim (Claims .Registered .EXPIRATION_TIME . getValue () )) {
8080 Date expirationTime = payload .getExpirationTime ();
8181 if (currentDate .getTime () > expirationTime .getTime ())
8282 throw new JWTExpiredException ("JWT expired on " + expirationTime );
@@ -93,73 +93,83 @@ public Builder() {
9393 }
9494
9595 public Builder withType (String type ) {
96- withHeader (Header .Registered .TYPE , type ::equals );
96+ withHeader (Claims .Registered .TYPE . getValue () , type ::equals );
9797 return this ;
9898 }
9999
100100 public Builder withContentType (String type ) {
101- withHeader (Header .Registered .CONTENT_TYPE , type ::equals );
101+ withHeader (Claims .Registered .CONTENT_TYPE . getValue () , type ::equals );
102102 return this ;
103103 }
104104
105105 public Builder withAlgorithm (String algorithm ) {
106- withHeader (Header .Registered .ALGORITHM , algorithm ::equals );
106+ withHeader (Claims .Registered .ALGORITHM . getValue () , algorithm ::equals );
107107 return this ;
108108 }
109109
110110 public Builder withIssuer (String issuer ) {
111- withClaim (Payload .Registered .ISSUER , issuer ::equals );
111+ withClaim (Claims .Registered .ISSUER . getValue () , issuer ::equals );
112112 return this ;
113113 }
114114
115115 public Builder withSubject (String subject ) {
116- withClaim (Payload .Registered .SUBJECT , subject ::equals );
116+ withClaim (Claims .Registered .SUBJECT . getValue () , subject ::equals );
117117 return this ;
118118 }
119119
120120 public Builder withOneOfAudience (String ... audience ) {
121- withClaim (Payload .Registered .AUDIENCE , value -> {
121+ withClaim (Claims .Registered .AUDIENCE . getValue () , value -> {
122122 for (String audienceItem : audience ) {
123123 if (Arrays .asList ((Object []) value ).contains (audienceItem ))
124124 return true ;
125125 }
126126 return false ;
127127 });
128+
129+ return this ;
130+ }
131+
132+ public Builder withAllOfAudience (String ... audience ) {
133+ withClaim (Claims .Registered .AUDIENCE .getValue (), value -> {
134+ String [] values = (String []) value ;
135+ return Arrays .asList (values ).containsAll (Arrays .asList (audience ));
136+ });
137+
128138 return this ;
129139 }
130140
131141 public Builder withExpirationTime (Date expirationTime ) {
132- withClaim (Payload .Registered .EXPIRATION_TIME , value -> value .equals (expirationTime .getTime ()));
142+ withClaim (Claims .Registered .EXPIRATION_TIME . getValue () , value -> value .equals (expirationTime .getTime ()));
133143 return this ;
134144 }
135145
136146 public Builder withExpirationTime (long timeSinceEpoch ) {
137- withClaim (Payload .Registered .EXPIRATION_TIME , value -> value .equals (timeSinceEpoch ));
147+ withClaim (Claims .Registered .EXPIRATION_TIME . getValue () , value -> value .equals (timeSinceEpoch ));
138148 return this ;
139149 }
140150
141151 public Builder withNotBefore (Date notBefore ) {
142- withClaim (Payload .Registered .NOT_BEFORE , value -> value .equals (notBefore .getTime ()));
152+ withClaim (Claims .Registered .NOT_BEFORE . getValue () , value -> value .equals (notBefore .getTime ()));
143153 return this ;
144154 }
145155
146156 public Builder withNotBefore (long timeSinceEpoch ) {
147- withClaim (Payload .Registered .NOT_BEFORE , value -> value .equals (timeSinceEpoch ));
157+ withClaim (Claims .Registered .NOT_BEFORE . getValue () , value -> value .equals (timeSinceEpoch ));
148158 return this ;
149159 }
150160
151161 public Builder withIssuedAt (Date issuedAt ) {
152- withClaim (Payload .Registered .ISSUED_AT , value -> value .equals (issuedAt .getTime ()));
162+ withClaim (Claims .Registered .ISSUED_AT . getValue () , value -> value .equals (issuedAt .getTime ()));
153163 return this ;
154164 }
155165
156166 public Builder withIssuedAt (long timeSinceEpoch ) {
157- withClaim (Payload .Registered .ISSUED_AT , value -> value .equals (timeSinceEpoch ));
167+ withClaim (Claims .Registered .ISSUED_AT . getValue () , value -> value .equals (timeSinceEpoch ));
158168 return this ;
159169 }
160170
161171 public Builder withID (String id ) {
162- withClaim (Payload .Registered .JWT_ID , id ::equals );
172+ withClaim (Claims .Registered .JWT_ID . getValue () , id ::equals );
163173 return this ;
164174 }
165175
@@ -195,10 +205,10 @@ public DefaultJWTValidator build() {
195205 }
196206
197207 public Map <String , ClaimValidator > getHeaderValidators () {
198- return headerValidators ;
208+ return new HashMap <>( headerValidators ) ;
199209 }
200210
201211 public Map <String , ClaimValidator > getPayloadValidators () {
202- return payloadValidators ;
212+ return new HashMap <>( payloadValidators ) ;
203213 }
204214}
0 commit comments