Skip to content

Commit 05e2835

Browse files
committed
Get rid of jjwt, use auth0.java-jwt instead
1 parent 2027306 commit 05e2835

File tree

5 files changed

+36
-51
lines changed

5 files changed

+36
-51
lines changed

pom.xml

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46

57
<parent>
@@ -42,9 +44,10 @@
4244
<vault-java-driver.version>5.1.0</vault-java-driver.version>
4345
<jackson.version>2.19.2</jackson.version>
4446
<slf4j.version>1.7.36</slf4j.version>
45-
<jjwt.version>0.12.6</jjwt.version>
47+
<auth0.java-jwt.version>4.5.0</auth0.java-jwt.version>
4648

47-
<mockito-junit.version>4.6.1</mockito-junit.version>
49+
<mockito-junit.version>5.20.0</mockito-junit.version>
50+
<mockito-inline.version>5.2.0</mockito-inline.version>
4851
<junit-jupiter.version>5.8.2</junit-jupiter.version>
4952
<hamcrest.version>1.3</hamcrest.version>
5053
<log4j.version>2.17.2</log4j.version>
@@ -69,21 +72,11 @@
6972
<artifactId>slf4j-api</artifactId>
7073
<version>${slf4j.version}</version>
7174
</dependency>
72-
<!-- Jsonwebtoken -->
75+
<!-- Auth0/JWT -->
7376
<dependency>
74-
<groupId>io.jsonwebtoken</groupId>
75-
<artifactId>jjwt-api</artifactId>
76-
<version>${jjwt.version}</version>
77-
</dependency>
78-
<dependency>
79-
<groupId>io.jsonwebtoken</groupId>
80-
<artifactId>jjwt-impl</artifactId>
81-
<version>${jjwt.version}</version>
82-
</dependency>
83-
<dependency>
84-
<groupId>io.jsonwebtoken</groupId>
85-
<artifactId>jjwt-jackson</artifactId>
86-
<version>${jjwt.version}</version>
77+
<groupId>com.auth0</groupId>
78+
<artifactId>java-jwt</artifactId>
79+
<version>${auth0.java-jwt.version}</version>
8780
</dependency>
8881
<!-- Jackson -->
8982
<dependency>
@@ -135,7 +128,7 @@
135128
<dependency>
136129
<groupId>org.mockito</groupId>
137130
<artifactId>mockito-inline</artifactId>
138-
<version>${mockito-junit.version}</version>
131+
<version>${mockito-inline.version}</version>
139132
<scope>test</scope>
140133
</dependency>
141134
<dependency>

tests/src/test/java/io/scalecube/security/tokens/jwt/JsonwebtokenResolverTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
import static org.mockito.Mockito.mock;
1212
import static org.mockito.Mockito.when;
1313

14-
import io.jsonwebtoken.Locator;
1514
import io.scalecube.security.environment.IntegrationEnvironmentFixture;
1615
import io.scalecube.security.environment.VaultEnvironment;
17-
import java.security.Key;
1816
import java.time.Duration;
1917
import java.util.concurrent.TimeUnit;
2018
import org.junit.jupiter.api.Test;
@@ -58,7 +56,7 @@ void testParseTokenSuccessfully(VaultEnvironment vaultEnvironment) {
5856
void testJwksKeyLocatorThrowsError(VaultEnvironment vaultEnvironment) {
5957
final var token = vaultEnvironment.newServiceToken();
6058

61-
Locator<Key> keyLocator = mock(Locator.class);
59+
final var keyLocator = mock(JwksKeyLocator.class);
6260
when(keyLocator.locate(any())).thenThrow(new RuntimeException("Cannot get key"));
6361

6462
try {
@@ -75,7 +73,7 @@ void testJwksKeyLocatorThrowsError(VaultEnvironment vaultEnvironment) {
7573
void testJwksKeyLocatorThrowsRetryableError(VaultEnvironment vaultEnvironment) {
7674
final var token = vaultEnvironment.newServiceToken();
7775

78-
Locator<Key> keyLocator = mock(Locator.class);
76+
final var keyLocator = mock(JwksKeyLocator.class);
7977
when(keyLocator.locate(any())).thenThrow(new JwtUnavailableException("JWKS timeout"));
8078

8179
try {

tokens/pom.xml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
35
<modelVersion>4.0.0</modelVersion>
46

57
<parent>
@@ -13,16 +15,8 @@
1315

1416
<dependencies>
1517
<dependency>
16-
<groupId>io.jsonwebtoken</groupId>
17-
<artifactId>jjwt-api</artifactId>
18-
</dependency>
19-
<dependency>
20-
<groupId>io.jsonwebtoken</groupId>
21-
<artifactId>jjwt-impl</artifactId>
22-
</dependency>
23-
<dependency>
24-
<groupId>io.jsonwebtoken</groupId>
25-
<artifactId>jjwt-jackson</artifactId>
18+
<groupId>com.auth0</groupId>
19+
<artifactId>java-jwt</artifactId>
2620
</dependency>
2721
<dependency>
2822
<groupId>org.slf4j</groupId>

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

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

3-
import io.jsonwebtoken.JwtParser;
4-
import io.jsonwebtoken.Jwts;
5-
import io.jsonwebtoken.Locator;
6-
import java.security.Key;
3+
import com.auth0.jwt.JWT;
4+
import com.auth0.jwt.algorithms.Algorithm;
5+
import java.security.interfaces.RSAPublicKey;
76
import java.util.concurrent.CompletableFuture;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
@@ -12,18 +11,22 @@ public class JsonwebtokenResolver implements JwtTokenResolver {
1211

1312
private static final Logger LOGGER = LoggerFactory.getLogger(JsonwebtokenResolver.class);
1413

15-
private final JwtParser jwtParser;
14+
private final JwksKeyLocator keyLocator;
1615

17-
public JsonwebtokenResolver(Locator<Key> keyLocator) {
18-
jwtParser = Jwts.parser().keyLocator(keyLocator).build();
16+
public JsonwebtokenResolver(JwksKeyLocator keyLocator) {
17+
this.keyLocator = keyLocator;
1918
}
2019

2120
@Override
2221
public CompletableFuture<JwtToken> resolveToken(String token) {
2322
return CompletableFuture.supplyAsync(
2423
() -> {
25-
final var claimsJws = jwtParser.parseSignedClaims(token);
26-
return new JwtToken(claimsJws.getHeader(), claimsJws.getPayload());
24+
final var rawToken = JWT.decode(token);
25+
final var kid = rawToken.getKeyId();
26+
final var publicKey = (RSAPublicKey) keyLocator.locate(kid);
27+
final var verifier = JWT.require(Algorithm.RSA256(publicKey, null)).build();
28+
verifier.verify(token);
29+
return JwtToken.parseToken(token);
2730
})
2831
.handle(
2932
(jwtToken, ex) -> {

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import com.fasterxml.jackson.databind.DeserializationFeature;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.SerializationFeature;
9-
import io.jsonwebtoken.JwsHeader;
10-
import io.jsonwebtoken.LocatorAdapter;
119
import java.io.BufferedInputStream;
1210
import java.io.IOException;
1311
import java.io.InputStream;
@@ -29,7 +27,7 @@
2927
import java.util.concurrent.ConcurrentHashMap;
3028
import java.util.concurrent.locks.ReentrantLock;
3129

32-
public class JwksKeyLocator extends LocatorAdapter<Key> {
30+
public class JwksKeyLocator {
3331

3432
private static final ObjectMapper OBJECT_MAPPER = newObjectMapper();
3533

@@ -54,16 +52,15 @@ public static Builder builder() {
5452
return new Builder();
5553
}
5654

57-
@Override
58-
protected Key locate(JwsHeader header) {
55+
public Key locate(String kid) {
5956
try {
6057
return keyResolutions
6158
.computeIfAbsent(
62-
header.getKeyId(),
63-
kid -> {
64-
final var key = findKeyById(computeKeyList(), kid);
59+
kid,
60+
id -> {
61+
final var key = findKeyById(computeKeyList(), id);
6562
if (key == null) {
66-
throw new JwtUnavailableException("Cannot find key by kid: " + kid);
63+
throw new JwtUnavailableException("Cannot find key by kid: " + id);
6764
}
6865
return new CachedKey(key, System.currentTimeMillis() + keyTtl);
6966
})

0 commit comments

Comments
 (0)