Skip to content

Commit c37ffdb

Browse files
author
zihluwang
committed
feat: token payload information
1 parent 6814718 commit c37ffdb

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.jwt;
19+
20+
import com.onixbyte.jwt.constant.RegisteredClaims;
21+
22+
import java.time.LocalDateTime;
23+
import java.time.ZoneId;
24+
import java.util.*;
25+
26+
/**
27+
*
28+
*/
29+
public class TokenPayload {
30+
31+
public static TokenPayload createPayload() {
32+
return new TokenPayload();
33+
}
34+
35+
private final Map<String, Object> payload;
36+
private final List<String> audiences;
37+
38+
private String subject;
39+
private String issuer;
40+
private String tokenId;
41+
private Long expiresAt;
42+
private Long notBefore;
43+
private Long issuedAt;
44+
45+
/**
46+
* Private constructor to prevent instantiation of this utility class.
47+
*/
48+
private TokenPayload() {
49+
payload = new HashMap<>();
50+
audiences = new ArrayList<>();
51+
}
52+
53+
public TokenPayload withAudience(String audience) {
54+
audiences.add(audience);
55+
return this;
56+
}
57+
58+
public TokenPayload withAudiences(String... audiences) {
59+
this.audiences.addAll(Arrays.asList(audiences));
60+
return this;
61+
}
62+
63+
public TokenPayload withSubject(String subject) {
64+
this.subject = subject;
65+
return this;
66+
}
67+
68+
public TokenPayload withIssuer(String issuer) {
69+
this.issuer = issuer;
70+
return this;
71+
}
72+
73+
public TokenPayload withTokenId(String tokenId) {
74+
this.tokenId = tokenId;
75+
return this;
76+
}
77+
78+
public TokenPayload withExpiresAt(LocalDateTime expiresAt) {
79+
this.expiresAt = expiresAt.atZone(ZoneId.systemDefault())
80+
.toInstant()
81+
.getEpochSecond();
82+
return this;
83+
}
84+
85+
public TokenPayload withNotBefore(LocalDateTime notBefore) {
86+
this.notBefore = notBefore.atZone(ZoneId.systemDefault())
87+
.toInstant()
88+
.getEpochSecond();
89+
return this;
90+
}
91+
92+
public TokenPayload withIssuedAt(LocalDateTime issuedAt) {
93+
this.issuedAt = issuedAt.atZone(ZoneId.systemDefault())
94+
.toInstant()
95+
.getEpochSecond();
96+
return this;
97+
}
98+
99+
public TokenPayload withClaim(String name, String value) {
100+
if (RegisteredClaims.VALUES.contains(name)) {
101+
throw new IllegalStateException("Please set registered claims with pre-defined methods");
102+
}
103+
104+
this.payload.put(name, value);
105+
return this;
106+
}
107+
108+
public boolean hasIssuer() {
109+
return Objects.nonNull(issuer) && !issuer.isBlank();
110+
}
111+
112+
public Map<String, Object> getPayload() {
113+
var _payload = new HashMap<>(payload);
114+
115+
Optional.of(audiences)
116+
.filter((aud) -> !aud.isEmpty())
117+
.ifPresent((aud) -> _payload.put(RegisteredClaims.AUDIENCE, aud));
118+
119+
Optional.ofNullable(subject)
120+
.filter((sub) -> !sub.isBlank())
121+
.ifPresent((sub) -> _payload.put(RegisteredClaims.SUBJECT, subject));
122+
123+
Optional.ofNullable(expiresAt)
124+
.ifPresent((exp) -> _payload.put(RegisteredClaims.EXPIRES_AT, exp));
125+
126+
Optional.ofNullable(tokenId)
127+
.filter((jti) -> !jti.isBlank())
128+
.ifPresent((jti) -> _payload.put(RegisteredClaims.TOKEN_ID, jti));
129+
130+
Optional.ofNullable(issuer)
131+
.map((iss) -> !iss.isBlank())
132+
.ifPresent((iss) -> _payload.put(RegisteredClaims.ISSUER, iss));
133+
134+
Optional.ofNullable(issuedAt)
135+
.ifPresent((iat) -> _payload.put(RegisteredClaims.ISSUED_AT, iat));
136+
137+
Optional.ofNullable(notBefore)
138+
.ifPresent((nbf) -> _payload.put(RegisteredClaims.NOT_BEFORE, nbf));
139+
140+
return _payload;
141+
}
142+
143+
}

0 commit comments

Comments
 (0)