Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JwtConfig.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.util.LinkedHashSet;
31import java.util.Objects;
32import java.util.Set;
33
34/**
35 * Configures JWT signing and verification behavior.
36 */
37public final class JwtConfig {
38
39 private final KeyProvider keyProvider;
40 private final String expectedIssuer;
41 private final Set<String> expectedAudience;
42 private final Duration clockSkew;
43 private final boolean validateExpiration;
44 private final boolean validateNotBefore;
45 private final boolean requireExpiration;
46 private final boolean requireSubject;
47 private final boolean requireClientIdForAppTokens;
48
49 private JwtConfig(final Builder builder) {
50 keyProvider = Objects.requireNonNull(builder.keyProvider, "keyProvider");
51 expectedIssuer = builder.expectedIssuer;
52 expectedAudience = Set.copyOf(builder.expectedAudience);
53 clockSkew = builder.clockSkew;
54 validateExpiration = builder.validateExpiration;
55 validateNotBefore = builder.validateNotBefore;
56 requireExpiration = builder.requireExpiration;
57 requireSubject = builder.requireSubject;
58 requireClientIdForAppTokens = builder.requireClientIdForAppTokens;
59 }
60
61 public static Builder builder(final KeyProvider keyProvider) {
62 return new Builder(keyProvider);
63 }
64
66 return keyProvider;
67 }
68
69 public String expectedIssuer() {
70 return expectedIssuer;
71 }
72
73 public Set<String> expectedAudience() {
74 return expectedAudience;
75 }
76
77 public Duration clockSkew() {
78 return clockSkew;
79 }
80
81 public boolean validateExpiration() {
82 return validateExpiration;
83 }
84
85 public boolean validateNotBefore() {
86 return validateNotBefore;
87 }
88
89 public boolean requireExpiration() {
90 return requireExpiration;
91 }
92
93 public boolean requireSubject() {
94 return requireSubject;
95 }
96
97 public boolean requireClientIdForAppTokens() {
98 return requireClientIdForAppTokens;
99 }
100
101 public static final class Builder {
102 private final KeyProvider keyProvider;
103 private String expectedIssuer;
104 private Set<String> expectedAudience = new LinkedHashSet<>();
105 private Duration clockSkew = Duration.ZERO;
106 private boolean validateExpiration = true;
107 private boolean validateNotBefore = true;
108 private boolean requireExpiration = true;
109 private boolean requireSubject = true;
110 private boolean requireClientIdForAppTokens = true;
111
112 private Builder(final KeyProvider keyProvider) {
113 this.keyProvider = Objects.requireNonNull(keyProvider, "keyProvider");
114 }
115
116 public Builder expectedIssuer(final String expectedIssuer) {
117 this.expectedIssuer = blankToNull(expectedIssuer);
118 return this;
119 }
120
121 public Builder expectedAudience(final Set<String> expectedAudience) {
122 this.expectedAudience = expectedAudience == null ? new LinkedHashSet<>()
123 : new LinkedHashSet<>(expectedAudience);
124 this.expectedAudience.removeIf(value -> value == null || value.isBlank());
125 return this;
126 }
127
128 public Builder expectedAudience(final String... expectedAudience) {
129 final Set<String> values = new LinkedHashSet<>();
130 if (expectedAudience != null) {
131 for (final String aud : expectedAudience) {
132 if (aud != null && !aud.isBlank()) {
133 values.add(aud);
134 }
135 }
136 }
137 this.expectedAudience = values;
138 return this;
139 }
140
141 public Builder clockSkew(final Duration clockSkew) {
142 this.clockSkew = Objects.requireNonNull(clockSkew, "clockSkew");
143 if (clockSkew.isNegative()) {
144 throw new IllegalArgumentException("clockSkew must be >= 0");
145 }
146 return this;
147 }
148
149 public Builder validateExpiration(final boolean validateExpiration) {
150 this.validateExpiration = validateExpiration;
151 return this;
152 }
153
154 public Builder validateNotBefore(final boolean validateNotBefore) {
155 this.validateNotBefore = validateNotBefore;
156 return this;
157 }
158
159 public Builder requireExpiration(final boolean requireExpiration) {
160 this.requireExpiration = requireExpiration;
161 return this;
162 }
163
164 public Builder requireSubject(final boolean requireSubject) {
165 this.requireSubject = requireSubject;
166 return this;
167 }
168
169 public Builder requireClientIdForAppTokens(final boolean requireClientIdForAppTokens) {
170 this.requireClientIdForAppTokens = requireClientIdForAppTokens;
171 return this;
172 }
173
174 public JwtConfig build() {
175 return new JwtConfig(this);
176 }
177
178 private static String blankToNull(final String value) {
179 return value == null || value.isBlank() ? null : value;
180 }
181 }
182}
Set< String > expectedAudience()
static Builder builder(final KeyProvider keyProvider)
Provides cryptographic material used to sign and verify JWT tokens.