Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
TokenSpec.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.Duration;
30import java.time.Instant;
31import java.util.LinkedHashMap;
32import java.util.Map;
33import java.util.Objects;
34import java.util.UUID;
35
36/** Specification used by {@link TokenIssuer} to issue JWT tokens. */
37public final class TokenSpec {
38
39 private final TokenClaims claims;
40
41 private TokenSpec(final Builder builder) {
42 final Instant issuedAt = builder.issuedAt == null ? Instant.now() : builder.issuedAt;
43 final Instant expiresAt = resolveExpiresAt(issuedAt, builder.expiresAt, builder.ttl);
44
45 if (builder.subject == null || builder.subject.isBlank()) {
46 throw new IllegalArgumentException("subject is required");
47 }
48 if (expiresAt == null) {
49 throw new IllegalArgumentException("expiresAt or ttl is required");
50 }
51
52 claims = TokenClaims.builder()
53 .subject(builder.subject)
54 .issuer(builder.issuer)
55 .audience(builder.audience)
56 .issuedAt(issuedAt)
57 .expiresAt(expiresAt)
58 .notBefore(builder.notBefore)
59 .jwtId(builder.jwtId == null || builder.jwtId.isBlank() ? UUID.randomUUID().toString() : builder.jwtId)
60 .roles(builder.roles)
61 .tokenType(builder.tokenType)
62 .clientId(builder.clientId)
63 .extras(builder.customClaims)
64 .build();
65 }
66
67 public static Builder builder() {
68 return new Builder();
69 }
70
72 return claims;
73 }
74
75 private static Instant resolveExpiresAt(final Instant issuedAt, final Instant expiresAt, final Duration ttl) {
76 if (expiresAt != null) {
77 return expiresAt;
78 }
79 if (ttl != null) {
80 if (ttl.isNegative() || ttl.isZero()) {
81 throw new IllegalArgumentException("ttl must be > 0");
82 }
83 return issuedAt.plus(ttl);
84 }
85 return null;
86 }
87
88 public static final class Builder {
89 private String subject;
90 private String issuer;
91 private String[] audience;
92 private Instant issuedAt;
93 private Instant expiresAt;
94 private Duration ttl;
95 private Instant notBefore;
96 private String jwtId;
97 private String[] roles;
98 private TokenType tokenType;
99 private String clientId;
100 private Map<String, Object> customClaims = new LinkedHashMap<>();
101
102 private Builder() {
103 }
104
105 public Builder subject(final String subject) {
106 this.subject = subject;
107 return this;
108 }
109
110 public Builder issuer(final String issuer) {
111 this.issuer = issuer;
112 return this;
113 }
114
115 public Builder audience(final String... audience) {
116 this.audience = audience == null ? new String[0] : audience;
117 return this;
118 }
119
120 public Builder issuedAt(final Instant issuedAt) {
121 this.issuedAt = issuedAt;
122 return this;
123 }
124
125 public Builder expiresAt(final Instant expiresAt) {
126 this.expiresAt = expiresAt;
127 return this;
128 }
129
130 public Builder ttl(final Duration ttl) {
131 this.ttl = ttl;
132 return this;
133 }
134
135 public Builder notBefore(final Instant notBefore) {
136 this.notBefore = notBefore;
137 return this;
138 }
139
140 public Builder jwtId(final String jwtId) {
141 this.jwtId = jwtId;
142 return this;
143 }
144
145 public Builder roles(final String... roles) {
146 this.roles = roles == null ? new String[0] : roles;
147 return this;
148 }
149
150 public Builder tokenType(final TokenType tokenType) {
151 this.tokenType = tokenType;
152 return this;
153 }
154
155 public Builder clientId(final String clientId) {
156 this.clientId = clientId;
157 return this;
158 }
159
160 public Builder claim(final String key, final Object value) {
161 if (key == null || key.isBlank()) {
162 throw new IllegalArgumentException("claim key is required");
163 }
164 customClaims.put(key, value);
165 return this;
166 }
167
168 public Builder claims(final Map<String, Object> claims) {
169 customClaims = claims == null ? new LinkedHashMap<>() : new LinkedHashMap<>(claims);
170 return this;
171 }
172
173 public TokenSpec build() {
174 return new TokenSpec(this);
175 }
176 }
177}
Normalized claims extracted from a JWT token.