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<>() : new LinkedHashSet<>(expectedAudience);
123 this.expectedAudience.removeIf(value -> value == null || value.isBlank());
124 return this;
125 }
126
127 public Builder expectedAudience(final String... expectedAudience) {
128 final Set<String> values = new LinkedHashSet<>();
129 if (expectedAudience != null) {
130 for (final String aud : expectedAudience) {
131 if (aud != null && !aud.isBlank()) {
132 values.add(aud);
133 }
134 }
135 }
136 this.expectedAudience = values;
137 return this;
138 }
139
140 public Builder clockSkew(final Duration clockSkew) {
141 this.clockSkew = Objects.requireNonNull(clockSkew, "clockSkew");
142 if (clockSkew.isNegative()) {
143 throw new IllegalArgumentException("clockSkew must be >= 0");
144 }
145 return this;
146 }
147
148 public Builder validateExpiration(final boolean validateExpiration) {
149 this.validateExpiration = validateExpiration;
150 return this;
151 }
152
153 public Builder validateNotBefore(final boolean validateNotBefore) {
154 this.validateNotBefore = validateNotBefore;
155 return this;
156 }
157
158 public Builder requireExpiration(final boolean requireExpiration) {
159 this.requireExpiration = requireExpiration;
160 return this;
161 }
162
163 public Builder requireSubject(final boolean requireSubject) {
164 this.requireSubject = requireSubject;
165 return this;
166 }
167
168 public Builder requireClientIdForAppTokens(final boolean requireClientIdForAppTokens) {
169 this.requireClientIdForAppTokens = requireClientIdForAppTokens;
170 return this;
171 }
172
173 public JwtConfig build() {
174 return new JwtConfig(this);
175 }
176
177 private static String blankToNull(final String value) {
178 return value == null || value.isBlank() ? null : value;
179 }
180 }
181}
Set< String > expectedAudience()
static Builder builder(final KeyProvider keyProvider)
Provides cryptographic material used to sign and verify JWT tokens.