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
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 dev.rafex.ether.jwt.internal.ClaimsMapper;
30import dev.rafex.ether.jwt.internal.JwtCodec;
31import dev.rafex.ether.jwt.internal.JwtSigner;
32import dev.rafex.ether.jwt.internal.TokenValidator;
33
34import java.time.Instant;
35import java.util.Objects;
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)
static VerificationCode validate(final TokenClaims claims, final JsonNode payload, final JwtConfig config, final Instant now, final String tokenTypeRaw)
Supported JWT signature algorithms.
static JwtAlgorithm fromHeaderValue(final String value)
Stable verification error/success codes.
API for verifying JWT tokens.