Skip to content

Commit b205698

Browse files
author
zihluwang
committed
feat: added support for different types of token claims
1 parent f660f8a commit b205698

File tree

1 file changed

+96
-9
lines changed

1 file changed

+96
-9
lines changed

simple-jwt/src/main/java/com/onixbyte/jwt/TokenPayload.java

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,101 @@ public TokenPayload withIssuedAt(LocalDateTime issuedAt) {
227227
* @throws IllegalStateException if the claim name is a registered claim
228228
*/
229229
public TokenPayload withClaim(String name, String value) {
230-
if (RegisteredClaims.VALUES.contains(name)) {
231-
throw new IllegalStateException("Please set registered claims with pre-defined methods");
232-
}
230+
checkClaimName(name);
231+
232+
this.payload.put(name, value);
233+
return this;
234+
}
235+
236+
/**
237+
* Adds a custom claim to the JWT payload.
238+
* <p>
239+
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
240+
* Registered claims must be set using their dedicated methods to ensure proper handling.
241+
*
242+
* @param name the name of the custom claim
243+
* @param value the value of the custom claim
244+
* @return this {@link TokenPayload} instance for method chaining
245+
* @throws IllegalStateException if the claim name is a registered claim
246+
*/
247+
public TokenPayload withClaim(String name, Long value) {
248+
checkClaimName(name);
249+
250+
this.payload.put(name, value);
251+
return this;
252+
}
253+
254+
/**
255+
* Adds a custom claim to the JWT payload.
256+
* <p>
257+
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
258+
* Registered claims must be set using their dedicated methods to ensure proper handling.
259+
*
260+
* @param name the name of the custom claim
261+
* @param value the value of the custom claim
262+
* @return this {@link TokenPayload} instance for method chaining
263+
* @throws IllegalStateException if the claim name is a registered claim
264+
*/
265+
public TokenPayload withClaim(String name, Double value) {
266+
checkClaimName(name);
267+
268+
this.payload.put(name, value);
269+
return this;
270+
}
271+
272+
/**
273+
* Adds a custom claim to the JWT payload.
274+
* <p>
275+
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
276+
* Registered claims must be set using their dedicated methods to ensure proper handling.
277+
*
278+
* @param name the name of the custom claim
279+
* @param value the value of the custom claim
280+
* @return this {@link TokenPayload} instance for method chaining
281+
* @throws IllegalStateException if the claim name is a registered claim
282+
*/
283+
public TokenPayload withClaim(String name, Boolean value) {
284+
checkClaimName(name);
233285

234286
this.payload.put(name, value);
235287
return this;
236288
}
237289

290+
/**
291+
* Adds a custom claim to the JWT payload.
292+
* <p>
293+
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
294+
* Registered claims must be set using their dedicated methods to ensure proper handling.
295+
*
296+
* @param name the name of the custom claim
297+
* @param value the value of the custom claim
298+
* @return this {@link TokenPayload} instance for method chaining
299+
* @throws IllegalStateException if the claim name is a registered claim
300+
*/
301+
public TokenPayload withClaim(String name, LocalDateTime value) {
302+
checkClaimName(name);
303+
304+
this.payload.put(name, value);
305+
return this;
306+
}
307+
308+
/**
309+
* Adds a custom claim with null value to the JWT payload.
310+
* <p>
311+
* Stores a custom key-value pair in the payload, provided the key is not a registered claim.
312+
* Registered claims must be set using their dedicated methods to ensure proper handling.
313+
*
314+
* @param name the name of the custom claim
315+
* @return this {@link TokenPayload} instance for method chaining
316+
* @throws IllegalStateException if the claim name is a registered claim
317+
*/
318+
public TokenPayload withNullClaim(String name) {
319+
checkClaimName(name);
320+
321+
this.payload.put(name, null);
322+
return this;
323+
}
324+
238325
/**
239326
* Checks if the JWT payload has a valid issuer.
240327
* <p>
@@ -261,28 +348,28 @@ public Map<String, Object> getPayload() {
261348
Optional.of(audiences)
262349
.filter((aud) -> !aud.isEmpty())
263350
.ifPresent((aud) -> _payload.put(RegisteredClaims.AUDIENCE, aud));
264-
265351
Optional.ofNullable(subject)
266352
.filter((sub) -> !sub.isBlank())
267353
.ifPresent((sub) -> _payload.put(RegisteredClaims.SUBJECT, subject));
268-
269354
Optional.ofNullable(expiresAt)
270355
.ifPresent((exp) -> _payload.put(RegisteredClaims.EXPIRES_AT, exp));
271-
272356
Optional.ofNullable(tokenId)
273357
.filter((jti) -> !jti.isBlank())
274358
.ifPresent((jti) -> _payload.put(RegisteredClaims.TOKEN_ID, jti));
275-
276359
Optional.ofNullable(issuer)
277360
.filter((iss) -> !iss.isBlank())
278361
.ifPresent((iss) -> _payload.put(RegisteredClaims.ISSUER, iss));
279-
280362
Optional.ofNullable(issuedAt)
281363
.ifPresent((iat) -> _payload.put(RegisteredClaims.ISSUED_AT, iat));
282-
283364
Optional.ofNullable(notBefore)
284365
.ifPresent((nbf) -> _payload.put(RegisteredClaims.NOT_BEFORE, nbf));
285366

286367
return _payload;
287368
}
369+
370+
private void checkClaimName(String name) {
371+
if (RegisteredClaims.VALUES.contains(name)) {
372+
throw new IllegalStateException("Please set registered claims with pre-defined methods");
373+
}
374+
}
288375
}

0 commit comments

Comments
 (0)