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
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 com.fasterxml.jackson.databind.ObjectMapper;
31import com.fasterxml.jackson.databind.node.ObjectNode;
32
33import java.nio.charset.StandardCharsets;
34import java.util.Base64;
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(
88 final String encodedHeader,
89 final String encodedPayload,
90 final String encodedSignature,
91 final JsonNode header,
92 final JsonNode payload) {
93 this.encodedHeader = encodedHeader;
94 this.encodedPayload = encodedPayload;
95 this.encodedSignature = encodedSignature;
96 this.header = header;
97 this.payload = payload;
98 }
99
100 public String encodedHeader() {
101 return encodedHeader;
102 }
103
104 public String encodedPayload() {
105 return encodedPayload;
106 }
107
108 public String encodedSignature() {
109 return encodedSignature;
110 }
111
112 public JsonNode header() {
113 return header;
114 }
115
116 public JsonNode payload() {
117 return payload;
118 }
119
120 public String signingInput() {
121 return encodedHeader + "." + encodedPayload;
122 }
123 }
124}
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