Skip to content

Commit 1080098

Browse files
committed
Made JwtToken parse(String token) as static method
1 parent 6bd90a4 commit 1080098

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

tokens/src/main/java/io/scalecube/security/tokens/jwt/JsonwebtokenResolver.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package io.scalecube.security.tokens.jwt;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import io.jsonwebtoken.Jwts;
54
import io.jsonwebtoken.Locator;
6-
import java.io.IOException;
7-
import java.nio.charset.StandardCharsets;
85
import java.security.Key;
9-
import java.util.Base64;
10-
import java.util.Map;
116
import java.util.Objects;
127
import java.util.concurrent.CompletableFuture;
138
import org.slf4j.Logger;
@@ -50,29 +45,6 @@ public CompletableFuture<JwtToken> resolve(String token) {
5045
});
5146
}
5247

53-
@Override
54-
public JwtToken parse(String token) {
55-
String[] parts = token.split("\\.");
56-
if (parts.length != 3) {
57-
throw new JwtTokenException("Invalid JWT format");
58-
}
59-
60-
try {
61-
final var urlDecoder = Base64.getUrlDecoder();
62-
final var headerJson = new String(urlDecoder.decode(parts[0]), StandardCharsets.UTF_8);
63-
final var payloadJson = new String(urlDecoder.decode(parts[1]), StandardCharsets.UTF_8);
64-
65-
final var mapper = new ObjectMapper();
66-
final var header = mapper.readValue(headerJson, Map.class);
67-
final var claims = mapper.readValue(payloadJson, Map.class);
68-
69-
//noinspection unchecked
70-
return new JwtToken(header, claims);
71-
} catch (IOException e) {
72-
throw new JwtTokenException("Failed to decode JWT", e);
73-
}
74-
}
75-
7648
private static String mask(String data) {
7749
if (data == null || data.length() < 5) {
7850
return "*****";
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package io.scalecube.security.tokens.jwt;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
6+
import java.util.Base64;
37
import java.util.Map;
48

5-
public record JwtToken(Map<String, Object> header, Map<String, Object> payload) {}
9+
public record JwtToken(Map<String, Object> header, Map<String, Object> payload) {
10+
11+
/**
12+
* Parses given JWT without verifying its signature.
13+
*
14+
* @param token jwt token
15+
* @return parsed token
16+
*/
17+
public static JwtToken parse(String token) {
18+
String[] parts = token.split("\\.");
19+
if (parts.length != 3) {
20+
throw new JwtTokenException("Invalid JWT format");
21+
}
22+
23+
try {
24+
final var urlDecoder = Base64.getUrlDecoder();
25+
final var headerJson = new String(urlDecoder.decode(parts[0]), StandardCharsets.UTF_8);
26+
final var payloadJson = new String(urlDecoder.decode(parts[1]), StandardCharsets.UTF_8);
27+
28+
final var mapper = new ObjectMapper();
29+
final var header = mapper.readValue(headerJson, Map.class);
30+
final var claims = mapper.readValue(payloadJson, Map.class);
31+
32+
//noinspection unchecked
33+
return new JwtToken(header, claims);
34+
} catch (IOException e) {
35+
throw new JwtTokenException("Failed to decode JWT", e);
36+
}
37+
}
38+
}

tokens/src/main/java/io/scalecube/security/tokens/jwt/JwtTokenResolver.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,4 @@ public interface JwtTokenResolver {
1111
* @return async result with {@link JwtToken}, or error
1212
*/
1313
CompletableFuture<JwtToken> resolve(String token);
14-
15-
/**
16-
* Parses given JWT without verifying its signature.
17-
*
18-
* @param token jwt token
19-
* @return parsed token
20-
*/
21-
JwtToken parse(String token);
2214
}

0 commit comments

Comments
 (0)