Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
TokenClaims.java
Go to the documentation of this file.
1package dev.rafex.ether.jwt;
2
3/*-
4 * #%L
5 * ether-jwt
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.time.Instant;
30import java.util.ArrayList;
31import java.util.Collections;
32import java.util.LinkedHashMap;
33import java.util.List;
34import java.util.Map;
35import java.util.Objects;
36
37/** Normalized claims extracted from a JWT token. */
38public final class TokenClaims {
39
40 private final String subject;
41 private final String issuer;
42 private final List<String> audience;
43 private final Instant expiresAt;
44 private final Instant issuedAt;
45 private final Instant notBefore;
46 private final String jwtId;
47 private final List<String> roles;
48 private final TokenType tokenType;
49 private final String clientId;
50 private final Map<String, Object> extras;
51
52 private TokenClaims(final Builder builder) {
53 this.subject = builder.subject;
54 this.issuer = builder.issuer;
55 this.audience = immutableList(builder.audience);
56 this.expiresAt = builder.expiresAt;
57 this.issuedAt = builder.issuedAt;
58 this.notBefore = builder.notBefore;
59 this.jwtId = builder.jwtId;
60 this.roles = immutableList(builder.roles);
61 this.tokenType = builder.tokenType;
62 this.clientId = builder.clientId;
63 this.extras = Collections.unmodifiableMap(new LinkedHashMap<>(builder.extras));
64 }
65
66 public static Builder builder() {
67 return new Builder();
68 }
69
70 public String subject() {
71 return subject;
72 }
73
74 public String issuer() {
75 return issuer;
76 }
77
78 public List<String> audience() {
79 return audience;
80 }
81
82 public Instant expiresAt() {
83 return expiresAt;
84 }
85
86 public Instant issuedAt() {
87 return issuedAt;
88 }
89
90 public Instant notBefore() {
91 return notBefore;
92 }
93
94 public String jwtId() {
95 return jwtId;
96 }
97
98 public List<String> roles() {
99 return roles;
100 }
101
103 return tokenType;
104 }
105
106 public String clientId() {
107 return clientId;
108 }
109
110 public Map<String, Object> extras() {
111 return extras;
112 }
113
114 public Builder toBuilder() {
115 return builder()
116 .subject(subject)
117 .issuer(issuer)
118 .audience(audience)
119 .expiresAt(expiresAt)
120 .issuedAt(issuedAt)
121 .notBefore(notBefore)
122 .jwtId(jwtId)
123 .roles(roles)
124 .tokenType(tokenType)
125 .clientId(clientId)
126 .extras(extras);
127 }
128
129 private static List<String> immutableList(final List<String> input) {
130 if (input == null || input.isEmpty()) {
131 return List.of();
132 }
133 final List<String> values = new ArrayList<>();
134 for (final String value : input) {
135 if (value != null && !value.isBlank()) {
136 values.add(value);
137 }
138 }
139 return Collections.unmodifiableList(values);
140 }
141
142 public static final class Builder {
143 private String subject;
144 private String issuer;
145 private List<String> audience = new ArrayList<>();
146 private Instant expiresAt;
147 private Instant issuedAt;
148 private Instant notBefore;
149 private String jwtId;
150 private List<String> roles = new ArrayList<>();
151 private TokenType tokenType;
152 private String clientId;
153 private Map<String, Object> extras = new LinkedHashMap<>();
154
155 private Builder() {
156 }
157
158 public Builder subject(final String subject) {
159 this.subject = blankToNull(subject);
160 return this;
161 }
162
163 public Builder issuer(final String issuer) {
164 this.issuer = blankToNull(issuer);
165 return this;
166 }
167
168 public Builder audience(final List<String> audience) {
169 this.audience = audience == null ? new ArrayList<>() : new ArrayList<>(audience);
170 return this;
171 }
172
173 public Builder audience(final String... audience) {
174 final List<String> values = new ArrayList<>();
175 if (audience != null) {
176 Collections.addAll(values, audience);
177 }
178 this.audience = values;
179 return this;
180 }
181
182 public Builder expiresAt(final Instant expiresAt) {
183 this.expiresAt = expiresAt;
184 return this;
185 }
186
187 public Builder issuedAt(final Instant issuedAt) {
188 this.issuedAt = issuedAt;
189 return this;
190 }
191
192 public Builder notBefore(final Instant notBefore) {
193 this.notBefore = notBefore;
194 return this;
195 }
196
197 public Builder jwtId(final String jwtId) {
198 this.jwtId = blankToNull(jwtId);
199 return this;
200 }
201
202 public Builder roles(final List<String> roles) {
203 this.roles = roles == null ? new ArrayList<>() : new ArrayList<>(roles);
204 return this;
205 }
206
207 public Builder roles(final String... roles) {
208 final List<String> values = new ArrayList<>();
209 if (roles != null) {
210 Collections.addAll(values, roles);
211 }
212 this.roles = values;
213 return this;
214 }
215
216 public Builder tokenType(final TokenType tokenType) {
217 this.tokenType = tokenType;
218 return this;
219 }
220
221 public Builder clientId(final String clientId) {
222 this.clientId = blankToNull(clientId);
223 return this;
224 }
225
226 public Builder extras(final Map<String, Object> extras) {
227 this.extras = extras == null ? new LinkedHashMap<>() : new LinkedHashMap<>(extras);
228 return this;
229 }
230
231 public Builder extra(final String key, final Object value) {
232 if (key == null || key.isBlank()) {
233 throw new IllegalArgumentException("extra claim key is required");
234 }
235 extras.put(key, value);
236 return this;
237 }
238
239 public TokenClaims build() {
240 return new TokenClaims(this);
241 }
242
243 private static String blankToNull(final String value) {
244 return value == null || value.isBlank() ? null : value;
245 }
246 }
247}
Map< String, Object > extras()
Supported business token types.