2121
2222import com .auth0 .jwt .algorithms .Algorithm ;
2323import com .auth0 .jwt .exceptions .JWTCreationException ;
24- import com .auth0 .jwt .impl .PublicClaims ;
24+ import com .auth0 .jwt .impl .Claims ;
2525import com .auth0 .jwt .jwts .JWT ;
2626
2727import java .util .Date ;
@@ -36,47 +36,37 @@ public class AccessJwtCreator {
3636
3737 protected JWTCreator .Builder jwt ;
3838 protected HashMap <String , Boolean > requiredClaims ;
39- protected Set <String > publicClaims ;
4039
41- public AccessJwtCreator () {
40+ private AccessJwtCreator () {
4241 jwt = JWT .create ();
4342 requiredClaims = new HashMap <String , Boolean >() {{
44- put ("Issuer" , false );
45- put ("Subject" , false );
46- put ("Iat" , false );
47- }};
48- publicClaims = new HashSet <String >() {{
49- add (PublicClaims .ISSUER );
50- add (PublicClaims .SUBJECT );
51- add (PublicClaims .EXPIRES_AT );
52- add (PublicClaims .NOT_BEFORE );
53- add (PublicClaims .ISSUED_AT );
54- add (PublicClaims .JWT_ID );
55- add (PublicClaims .AUDIENCE );
43+ put (Claims .ISSUER , false );
44+ put (Claims .SUBJECT , false );
45+ put (Claims .ISSUED_AT , false );
5646 }};
5747 }
5848
5949 /**
60- * Add a specific Issuer ("issuer" ) claim to the Payload.
50+ * Add a specific Issuer (Claims.ISSUER ) claim to the Payload.
6151 *
6252 * @param issuer the Issuer value.
6353 * @return this same Builder instance.
6454 */
6555 public AccessJwtCreator withIssuer (String issuer ) {
6656 jwt .withIssuer (issuer );
67- requiredClaims .put ("Issuer" , true );
57+ requiredClaims .put (Claims . ISSUER , true );
6858 return this ;
6959 }
7060
7161 /**
72- * Add a specific Subject ("subject" ) claim to the Payload.
62+ * Add a specific Subject (Claims.SUBJECT ) claim to the Payload.
7363 *
7464 * @param subject the Subject value.
7565 * @return this same Builder instance.
7666 */
7767 public AccessJwtCreator withSubject (String subject ) {
7868 jwt .withSubject (subject );
79- requiredClaims .put ("Subject" , true );
69+ requiredClaims .put (Claims . SUBJECT , true );
8070 return this ;
8171 }
8272
@@ -93,14 +83,14 @@ public AccessJwtCreator withAudience(String... audience) {
9383 }
9484
9585 /**
96- * Add a specific Issued At ("iat" ) claim to the Payload.
86+ * Add a specific Issued At (Claims.ISSUED_AT ) claim to the Payload.
9787 *
9888 * @param iat the Issued At value.
9989 * @return this same Builder instance.
10090 */
10191 public AccessJwtCreator withIat (Date iat ) {
10292 jwt .withIssuedAt (iat );
103- requiredClaims .put ("Iat" , true );
93+ requiredClaims .put (Claims . ISSUED_AT , true );
10494 return this ;
10595 }
10696
@@ -124,7 +114,13 @@ public AccessJwtCreator withExp(Date exp) {
124114 * @throws IllegalArgumentException if the name is null.
125115 */
126116 public AccessJwtCreator withNonStandardClaim (String name , String value ) {
127- jwt .withNonStandardClaim (name , value );
117+ if (name .equalsIgnoreCase ("subject" ) || name .equalsIgnoreCase (Claims .SUBJECT )) {
118+ withSubject (value );
119+ } else if (name .equalsIgnoreCase ("issuer" ) || name .equalsIgnoreCase (Claims .ISSUER )) {
120+ withIssuer (value );
121+ } else {
122+ jwt .withNonStandardClaim (name , value );
123+ }
128124 return this ;
129125 }
130126
@@ -189,7 +185,11 @@ public AccessJwtCreator withNonStandardClaim(String name, Double value) throws I
189185 * @throws IllegalArgumentException if the name is null.
190186 */
191187 public AccessJwtCreator withNonStandardClaim (String name , Date value ) throws IllegalArgumentException {
192- jwt .withNonStandardClaim (name , value );
188+ if (name .equalsIgnoreCase (Claims .ISSUED_AT ) || name .equalsIgnoreCase ("issuedAt" ) || name .equalsIgnoreCase ("issued_at" )) {
189+ withIat (value );
190+ } else {
191+ jwt .withNonStandardClaim (name , value );
192+ }
193193 return this ;
194194 }
195195
@@ -203,7 +203,7 @@ public AccessJwtCreator withNonStandardClaim(String name, Date value) throws Ill
203203 */
204204 public AccessJwtCreator withArrayClaim (String name , String ... items ) throws IllegalArgumentException {
205205 jwt .withArrayClaim (name , items );
206- if (publicClaims . contains (name ))
206+ if (requiredClaims . containsKey (name ))
207207 requiredClaims .put (name , true );
208208 return this ;
209209 }
@@ -230,11 +230,11 @@ public AccessJwtCreator setIsNoneAlgorithmAllowed(boolean isNoneAlgorithmAllowed
230230 * @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
231231 */
232232 public String sign (Algorithm algorithm ) throws Exception {
233- if (!jwt .getIsNoneAlgorithmAllowed () && algorithm . equals ( Algorithm .none ())) {
233+ if (!jwt .getIsNoneAlgorithmAllowed () && Algorithm .none (). equals ( algorithm )) {
234234 throw new IllegalAccessException ("None algorithm isn't allowed" );
235235 }
236- String JWS = jwt .sign (algorithm );
237236 verifyClaims ();
237+ String JWS = jwt .sign (algorithm );
238238 return JWS ;
239239 }
240240
@@ -248,11 +248,11 @@ public String sign(Algorithm algorithm) throws Exception {
248248 * @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
249249 */
250250 public String signBase16Encoding (Algorithm algorithm ) throws Exception {
251- if (!jwt .getIsNoneAlgorithmAllowed () && algorithm . equals ( Algorithm .none ())) {
251+ if (!jwt .getIsNoneAlgorithmAllowed () && Algorithm .none (). equals ( algorithm )) {
252252 throw new IllegalAccessException ("None algorithm isn't allowed" );
253253 }
254- String JWS = jwt .sign (algorithm , EncodeType .Base16 );
255254 verifyClaims ();
255+ String JWS = jwt .sign (algorithm , EncodeType .Base16 );
256256 return JWS ;
257257 }
258258
@@ -266,11 +266,11 @@ public String signBase16Encoding(Algorithm algorithm) throws Exception {
266266 * @throws JWTCreationException if the claims could not be converted to a valid JSON or there was a problem with the signing key.
267267 */
268268 public String signBase32Encoding (Algorithm algorithm ) throws Exception {
269- if (!jwt .getIsNoneAlgorithmAllowed () && algorithm . equals ( Algorithm .none ())) {
269+ if (!jwt .getIsNoneAlgorithmAllowed () && Algorithm .none (). equals ( algorithm )) {
270270 throw new IllegalAccessException ("None algorithm isn't allowed" );
271271 }
272- String JWS = jwt .sign (algorithm , EncodeType .Base32 );
273272 verifyClaims ();
273+ String JWS = jwt .sign (algorithm , EncodeType .Base32 );
274274 return JWS ;
275275 }
276276
0 commit comments