Skip to content

Commit 6326f1b

Browse files
author
Zihlu Wang
authored
Merge pull request #52 from siujamo/develop
2 parents ad5b5ba + 68f42f6 commit 6326f1b

File tree

5 files changed

+98
-34
lines changed

5 files changed

+98
-34
lines changed

simple-jwt/src/main/java/com/onixbyte/jwt/TokenCreator.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
package com.onixbyte.jwt;
1919

20-
import com.fasterxml.jackson.core.JsonProcessingException;
21-
22-
import java.security.InvalidKeyException;
23-
import java.security.NoSuchAlgorithmException;
24-
2520
/**
2621
*
2722
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.onixbyte.jwt;
2+
3+
public interface TokenManager<T> extends TokenCreator, TokenResolver {
4+
5+
T extract(String token);
6+
7+
}

simple-jwt/src/main/java/com/onixbyte/jwt/TokenResolver.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.onixbyte.jwt;
1919

20+
import com.onixbyte.jwt.constant.RegisteredClaims;
2021
import com.onixbyte.jwt.data.RawTokenComponent;
2122

2223
import java.util.Map;
@@ -27,30 +28,61 @@
2728
public interface TokenResolver {
2829

2930
/**
31+
* Verifies the HMAC signature of the provided JWT.
32+
* <p>
33+
* Splits the token into its components and uses the configured algorithm and secret to check
34+
* the signature's validity. If the signature does not match, an exception is thrown by the
35+
* underlying cryptographic utility.
3036
*
31-
* @param token
37+
* @param token the JWT string to verify
38+
* @throws IllegalArgumentException if the token is malformed or the signature verification
39+
* fails due to an invalid algorithm, key, or
40+
* mismatched signature
3241
*/
3342
void verify(String token);
3443

3544
/**
45+
* Retrieves the header claims from the provided JWT.
46+
* <p>
47+
* Decodes the Base64-encoded header and deserialises it into a map of strings.
3648
*
37-
* @param token
38-
* @return
49+
* @param token the JWT string from which to extract the header
50+
* @return a map containing the header claims as key-value pairs
51+
* @throws IllegalArgumentException if the token is malformed or the header cannot be
52+
* deserialised due to invalid JSON format
3953
*/
4054
Map<String, String> getHeader(String token);
4155

4256
/**
57+
* Retrieves the payload claims from the provided JWT, excluding registered claims.
58+
* <p>
59+
* Decodes the Base64-encoded payload, deserialises it into a map, and removes any registered
60+
* claims as defined in {@link RegisteredClaims}.
4361
*
44-
* @param payload
45-
* @return
62+
* @param token the JWT string from which to extract the payload
63+
* @return a map containing the custom payload claims as key-value pairs
64+
* @throws IllegalArgumentException if the token is malformed or the payload cannot be
65+
* deserialised due to invalid JSON format
4666
*/
47-
Map<String, Object> getPayload(String payload);
67+
Map<String, Object> getPayload(String token);
4868

4969
/**
70+
* Splits a JWT into its raw components: header, payload, and signature.
5071
*
51-
* @param token
52-
* @return
72+
* @param token the JWT string to split
73+
* @return a {@link RawTokenComponent} containing the header, payload, and signature as strings
74+
* @throws IllegalArgumentException if the token does not consist of exactly three parts
75+
* separated by dots
5376
*/
54-
RawTokenComponent splitToken(String token);
77+
default RawTokenComponent splitToken(String token) {
78+
var tokenTuple = token.split("\\.");
79+
80+
if (tokenTuple.length != 3) {
81+
throw new IllegalArgumentException(
82+
"The provided JWT is invalid: it must consist of exactly three parts separated by dots.");
83+
}
84+
85+
return new RawTokenComponent(tokenTuple[0], tokenTuple[1], tokenTuple[2]);
86+
}
5587

5688
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.onixbyte.jwt.impl;
2+
3+
import com.onixbyte.devkit.utils.MapUtil;
4+
import com.onixbyte.devkit.utils.ObjectMapAdapter;
5+
import com.onixbyte.jwt.TokenCreator;
6+
import com.onixbyte.jwt.TokenManager;
7+
import com.onixbyte.jwt.TokenPayload;
8+
import com.onixbyte.jwt.TokenResolver;
9+
import com.onixbyte.jwt.constant.Algorithm;
10+
11+
import java.util.Map;
12+
13+
public class HmacTokenManager<T> implements TokenManager<T> {
14+
15+
private final TokenCreator tokenCreator;
16+
private final TokenResolver tokenResolver;
17+
private final ObjectMapAdapter<T> adapter;
18+
19+
public HmacTokenManager(Algorithm algorithm, String issuer, String secret, ObjectMapAdapter<T> adapter) {
20+
this.tokenCreator = new HmacTokenCreator(algorithm, issuer, secret);
21+
this.tokenResolver = new HmacTokenResolver(algorithm, secret);
22+
this.adapter = adapter;
23+
}
24+
25+
@Override
26+
public T extract(String token) {
27+
var payloadMap = getPayload(token);
28+
return MapUtil.mapToObject(payloadMap, adapter);
29+
}
30+
31+
@Override
32+
public String sign(TokenPayload payload) {
33+
return tokenCreator.sign(payload);
34+
}
35+
36+
@Override
37+
public void verify(String token) {
38+
tokenResolver.verify(token);
39+
}
40+
41+
@Override
42+
public Map<String, String> getHeader(String token) {
43+
return tokenResolver.getHeader(token);
44+
}
45+
46+
@Override
47+
public Map<String, Object> getPayload(String token) {
48+
return tokenResolver.getPayload(token);
49+
}
50+
}

simple-jwt/src/main/java/com/onixbyte/jwt/impl/HmacTokenResolver.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,6 @@ public HmacTokenResolver(Algorithm algorithm, String secret) {
7373
this.objectMapper = ObjectMapperHolder.getInstance().getObjectMapper();
7474
}
7575

76-
/**
77-
* Splits a JWT into its raw components: header, payload, and signature.
78-
*
79-
* @param token the JWT string to split
80-
* @return a {@link RawTokenComponent} containing the header, payload, and signature as strings
81-
* @throws IllegalArgumentException if the token does not consist of exactly three parts
82-
* separated by dots
83-
*/
84-
@Override
85-
public RawTokenComponent splitToken(String token) {
86-
var tokenTuple = token.split("\\.");
87-
88-
if (tokenTuple.length != 3) {
89-
throw new IllegalArgumentException(
90-
"The provided JWT is invalid: it must consist of exactly three parts separated by dots.");
91-
}
92-
93-
return new RawTokenComponent(tokenTuple[0], tokenTuple[1], tokenTuple[2]);
94-
}
95-
9676
/**
9777
* Verifies the HMAC signature of the provided JWT.
9878
* <p>

0 commit comments

Comments
 (0)