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