Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
DefaultTokenVerifier.java
Go to the documentation of this file.
1package dev.rafex.ether.jwt;
2
3import java.time.Instant;
4import java.util.Objects;
5
6/*-
7 * #%L
8 * ether-jwt
9 * %%
10 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
11 * %%
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 * #L%
30 */
31
32import dev.rafex.ether.jwt.internal.ClaimsMapper;
33import dev.rafex.ether.jwt.internal.JwtCodec;
34import dev.rafex.ether.jwt.internal.JwtSigner;
35import dev.rafex.ether.jwt.internal.TokenValidator;
36
37/** Default implementation of {@link TokenVerifier}. */
38public final class DefaultTokenVerifier implements TokenVerifier {
39
40 private final JwtConfig config;
41
42 public DefaultTokenVerifier(final JwtConfig config) {
43 this.config = Objects.requireNonNull(config, "config");
44 validateVerifierConfig(config);
45 }
46
47 @Override
48 public VerificationResult verify(final String token, final Instant now) {
49 try {
50 final JwtCodec.ParsedJwt parsed = JwtCodec.parse(token);
51 final JwtAlgorithm tokenAlg = JwtAlgorithm.fromHeaderValue(parsed.header().path("alg").asText(null));
52 if (tokenAlg == null || tokenAlg != config.keyProvider().algorithm()) {
54 }
55
56 if (!JwtSigner.verify(parsed.signingInput(), parsed.encodedSignature(), config)) {
58 }
59
60 final String tokenTypeRaw = ClaimsMapper.tokenTypeRaw(parsed.payload());
61 final TokenClaims claims = ClaimsMapper.fromPayload(parsed.payload());
62 final VerificationCode validationResult = TokenValidator.validate(claims, parsed.payload(), config,
63 now == null ? Instant.now() : now, tokenTypeRaw);
64
65 if (validationResult != VerificationCode.OK) {
66 return VerificationResult.fail(validationResult);
67 }
68 return VerificationResult.ok(claims);
69 } catch (final IllegalArgumentException e) {
71 } catch (final Exception e) {
73 }
74 }
75
76 private static void validateVerifierConfig(final JwtConfig config) {
77 if (config.keyProvider().algorithm() == JwtAlgorithm.HS256 && config.keyProvider().hmacSecret() == null) {
78 throw new IllegalArgumentException("HS256 requires explicit hmac secret");
79 }
80 if (config.keyProvider().algorithm() == JwtAlgorithm.RS256 && config.keyProvider().publicKey() == null) {
81 throw new IllegalArgumentException("RS256 requires public key for verification");
82 }
83 }
84}
VerificationResult verify(final String token, final Instant now)
Configures JWT signing and verification behavior.
Normalized claims extracted from a JWT token.
Result returned by token verification.
static VerificationResult fail(final VerificationCode code)
static String tokenTypeRaw(final JsonNode payload)
static TokenClaims fromPayload(final JsonNode payload)
static ParsedJwt parse(final String token)
Definition JwtCodec.java:43
static boolean verify(final String signingInput, final String encodedSignature, final JwtConfig config)
Valida tokens JWT contra las reglas de configuración.
static VerificationCode validate(final TokenClaims claims, final JsonNode payload, final JwtConfig config, final Instant now, final String tokenTypeRaw)
Valida las reclamaciones (claims) del token según la configuración.
Supported JWT signature algorithms.
static JwtAlgorithm fromHeaderValue(final String value)
Stable verification error/success codes.
API for verifying JWT tokens.
Typed configuration entry points and composition APIs for Ether applications.