Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JwtCodec.java
Go to the documentation of this file.
1package dev.rafex.ether.jwt.internal;
2
3import java.nio.charset.StandardCharsets;
4import java.util.Base64;
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 com.fasterxml.jackson.databind.JsonNode;
33import com.fasterxml.jackson.databind.ObjectMapper;
34import com.fasterxml.jackson.databind.node.ObjectNode;
35
36public final class JwtCodec {
37
38 private static final ObjectMapper MAPPER = new ObjectMapper();
39
40 private JwtCodec() {
41 }
42
43 public static ParsedJwt parse(final String token) {
44 if (token == null || token.isBlank()) {
45 throw new IllegalArgumentException("token is required");
46 }
47 final String[] parts = token.split("\\.");
48 if (parts.length != 3) {
49 throw new IllegalArgumentException("invalid token format");
50 }
51
52 final JsonNode header = readJson(decode(parts[0]));
53 final JsonNode payload = readJson(decode(parts[1]));
54 return new ParsedJwt(parts[0], parts[1], parts[2], header, payload);
55 }
56
57 public static String encodeJson(final JsonNode node) {
58 return Base64.getUrlEncoder().withoutPadding().encodeToString(node.toString().getBytes(StandardCharsets.UTF_8));
59 }
60
61 public static String encodeHeader(final String alg) {
62 final ObjectNode header = MAPPER.createObjectNode();
63 header.put("alg", alg);
64 header.put("typ", "JWT");
65 return encodeJson(header);
66 }
67
68 public static String decode(final String value) {
69 return new String(Base64.getUrlDecoder().decode(value), StandardCharsets.UTF_8);
70 }
71
72 public static JsonNode readJson(final String json) {
73 try {
74 return MAPPER.readTree(json);
75 } catch (final Exception e) {
76 throw new IllegalArgumentException("invalid JSON", e);
77 }
78 }
79
80 public static final class ParsedJwt {
81 private final String encodedHeader;
82 private final String encodedPayload;
83 private final String encodedSignature;
84 private final JsonNode header;
85 private final JsonNode payload;
86
87 ParsedJwt(final String encodedHeader, final String encodedPayload, final String encodedSignature,
88 final JsonNode header, final JsonNode payload) {
89 this.encodedHeader = encodedHeader;
90 this.encodedPayload = encodedPayload;
91 this.encodedSignature = encodedSignature;
92 this.header = header;
93 this.payload = payload;
94 }
95
96 public String encodedHeader() {
97 return encodedHeader;
98 }
99
100 public String encodedPayload() {
101 return encodedPayload;
102 }
103
104 public String encodedSignature() {
105 return encodedSignature;
106 }
107
108 public JsonNode header() {
109 return header;
110 }
111
112 public JsonNode payload() {
113 return payload;
114 }
115
116 public String signingInput() {
117 return encodedHeader + "." + encodedPayload;
118 }
119 }
120}
static JsonNode readJson(final String json)
Definition JwtCodec.java:72
static String encodeHeader(final String alg)
Definition JwtCodec.java:61
static ParsedJwt parse(final String token)
Definition JwtCodec.java:43
static String encodeJson(final JsonNode node)
Definition JwtCodec.java:57
static String decode(final String value)
Definition JwtCodec.java:68