Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JwtSigner.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 dev.rafex.ether.jwt.JwtAlgorithm;
30import dev.rafex.ether.jwt.JwtConfig;
31import dev.rafex.ether.jwt.KeyProvider;
32
33import javax.crypto.Mac;
34import javax.crypto.spec.SecretKeySpec;
35import java.nio.charset.StandardCharsets;
36import java.security.MessageDigest;
37import java.security.Signature;
38import java.util.Base64;
39
40public final class JwtSigner {
41
42 private JwtSigner() {
43 }
44
45 public static String sign(final String signingInput, final JwtConfig config) {
46 try {
47 final KeyProvider keyProvider = config.keyProvider();
48 if (keyProvider.algorithm() == JwtAlgorithm.HS256) {
49 final byte[] signature = signHmac(signingInput.getBytes(StandardCharsets.UTF_8), keyProvider.hmacSecret());
50 return Base64.getUrlEncoder().withoutPadding().encodeToString(signature);
51 }
52 final Signature rsa = Signature.getInstance("SHA256withRSA");
53 rsa.initSign(keyProvider.privateKey());
54 rsa.update(signingInput.getBytes(StandardCharsets.UTF_8));
55 return Base64.getUrlEncoder().withoutPadding().encodeToString(rsa.sign());
56 } catch (final Exception e) {
57 throw new IllegalStateException("error while signing token", e);
58 }
59 }
60
61 public static boolean verify(final String signingInput, final String encodedSignature, final JwtConfig config) {
62 try {
63 final KeyProvider keyProvider = config.keyProvider();
64 if (keyProvider.algorithm() == JwtAlgorithm.HS256) {
65 final byte[] expected = signHmac(signingInput.getBytes(StandardCharsets.UTF_8), keyProvider.hmacSecret());
66 final byte[] provided = Base64.getUrlDecoder().decode(encodedSignature);
67 return MessageDigest.isEqual(expected, provided);
68 }
69 final Signature rsa = Signature.getInstance("SHA256withRSA");
70 rsa.initVerify(keyProvider.publicKey());
71 rsa.update(signingInput.getBytes(StandardCharsets.UTF_8));
72 return rsa.verify(Base64.getUrlDecoder().decode(encodedSignature));
73 } catch (final Exception e) {
74 return false;
75 }
76 }
77
78 private static byte[] signHmac(final byte[] data, final byte[] secret) throws Exception {
79 final Mac mac = Mac.getInstance("HmacSHA256");
80 mac.init(new SecretKeySpec(secret, "HmacSHA256"));
81 return mac.doFinal(data);
82 }
83}
Configures JWT signing and verification behavior.
static boolean verify(final String signingInput, final String encodedSignature, final JwtConfig config)
static String sign(final String signingInput, final JwtConfig config)
Supported JWT signature algorithms.
Provides cryptographic material used to sign and verify JWT tokens.