Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
TokenValidator.java
Go to the documentation of this file.
1package dev.rafex.ether.jwt.internal;
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 com.fasterxml.jackson.databind.JsonNode;
30import dev.rafex.ether.jwt.JwtConfig;
31import dev.rafex.ether.jwt.TokenClaims;
32import dev.rafex.ether.jwt.TokenType;
33import dev.rafex.ether.jwt.VerificationCode;
34
35import java.time.Instant;
36
37public final class TokenValidator {
38
39 private TokenValidator() {
40 }
41
43 final TokenClaims claims,
44 final JsonNode payload,
45 final JwtConfig config,
46 final Instant now,
47 final String tokenTypeRaw) {
48
49 if (config.requireSubject() && isBlank(claims.subject())) {
51 }
52
53 if (config.validateExpiration()) {
54 if (claims.expiresAt() == null) {
56 }
57 if (now.isAfter(claims.expiresAt().plus(config.clockSkew()))) {
59 }
60 } else if (config.requireExpiration() && claims.expiresAt() == null) {
62 }
63
64 if (config.validateNotBefore() && claims.notBefore() != null) {
65 if (now.plus(config.clockSkew()).isBefore(claims.notBefore())) {
67 }
68 }
69
70 final String expectedIssuer = config.expectedIssuer();
71 if (expectedIssuer != null && !expectedIssuer.equals(claims.issuer())) {
73 }
74
75 if (!config.expectedAudience().isEmpty()) {
76 boolean matches = false;
77 for (final String value : claims.audience()) {
78 if (config.expectedAudience().contains(value)) {
79 matches = true;
80 break;
81 }
82 }
83 if (!matches) {
85 }
86 }
87
88 if (tokenTypeRaw != null && claims.tokenType() == null) {
90 }
91
92 if (claims.tokenType() == TokenType.APP && config.requireClientIdForAppTokens() && isBlank(claims.clientId())) {
94 }
95
96 return VerificationCode.OK;
97 }
98
99 private static boolean isBlank(final String value) {
100 return value == null || value.isBlank();
101 }
102}
Configures JWT signing and verification behavior.
Normalized claims extracted from a JWT token.
static VerificationCode validate(final TokenClaims claims, final JsonNode payload, final JwtConfig config, final Instant now, final String tokenTypeRaw)
Supported business token types.
Stable verification error/success codes.