Skip to content

Commit 808eaf5

Browse files
committed
Renaming, + Updated documentation
1 parent 458dd33 commit 808eaf5

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

jwt/src/main/java/io/scalecube/security/jwt/JwksKeyLocator.java renamed to jwt/src/main/java/io/scalecube/security/jwt/JwksKeyProvider.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
import java.util.concurrent.ConcurrentHashMap;
2828
import java.util.concurrent.locks.ReentrantLock;
2929

30-
public class JwksKeyLocator {
30+
/**
31+
* Provides public keys from a remote JWKS endpoint and caches them temporarily. Keys are fetched on
32+
* demand by their {@code kid} and automatically removed when expired.
33+
*/
34+
public class JwksKeyProvider {
3135

3236
private static final ObjectMapper OBJECT_MAPPER = newObjectMapper();
3337

@@ -40,7 +44,7 @@ public class JwksKeyLocator {
4044
private final Map<String, CachedKey> keyResolutions = new ConcurrentHashMap<>();
4145
private final ReentrantLock cleanupLock = new ReentrantLock();
4246

43-
private JwksKeyLocator(Builder builder) {
47+
private JwksKeyProvider(Builder builder) {
4448
this.jwksUri = Objects.requireNonNull(builder.jwksUri, "jwksUri");
4549
this.connectTimeout = Objects.requireNonNull(builder.connectTimeout, "connectTimeout");
4650
this.requestTimeout = Objects.requireNonNull(builder.requestTimeout, "requestTimeout");
@@ -55,7 +59,15 @@ public static Builder builder() {
5559
return new Builder();
5660
}
5761

58-
public Key locate(String kid) {
62+
/**
63+
* Returns the public key for the given {@code kid}. If not cached, the key is fetched from the
64+
* JWKS endpoint and cached for future use.
65+
*
66+
* @param kid key id of the public key to retrieve
67+
* @return {@link Key} object associated with given {@code kid}
68+
* @throws JwtUnavailableException if key cannot be found or JWKS cannot be retrieved
69+
*/
70+
public Key getKey(String kid) {
5971
try {
6072
return keyResolutions
6173
.computeIfAbsent(
@@ -226,8 +238,8 @@ public Builder httpClient(HttpClient httpClient) {
226238
return this;
227239
}
228240

229-
public JwksKeyLocator build() {
230-
return new JwksKeyLocator(this);
241+
public JwksKeyProvider build() {
242+
return new JwksKeyProvider(this);
231243
}
232244
}
233245
}

jwt/src/main/java/io/scalecube/security/jwt/JsonwebtokenResolver.java renamed to jwt/src/main/java/io/scalecube/security/jwt/JwksTokenResolver.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10-
public class JsonwebtokenResolver implements JwtTokenResolver {
10+
/**
11+
* Resolves and verifies JWT tokens using public keys provided by {@link JwksKeyProvider}. Tokens
12+
* are validated asynchronously and parsed into {@link JwtToken} instances.
13+
*/
14+
public class JwksTokenResolver implements JwtTokenResolver {
1115

12-
private static final Logger LOGGER = LoggerFactory.getLogger(JsonwebtokenResolver.class);
16+
private static final Logger LOGGER = LoggerFactory.getLogger(JwksTokenResolver.class);
1317

14-
private final JwksKeyLocator keyLocator;
18+
private final JwksKeyProvider keyProvider;
1519

16-
public JsonwebtokenResolver(JwksKeyLocator keyLocator) {
17-
this.keyLocator = keyLocator;
20+
public JwksTokenResolver(JwksKeyProvider keyProvider) {
21+
this.keyProvider = keyProvider;
1822
}
1923

2024
@Override
@@ -23,7 +27,7 @@ public CompletableFuture<JwtToken> resolveToken(String token) {
2327
() -> {
2428
final var rawToken = JWT.decode(token);
2529
final var kid = rawToken.getKeyId();
26-
final var publicKey = (RSAPublicKey) keyLocator.locate(kid);
30+
final var publicKey = (RSAPublicKey) keyProvider.getKey(kid);
2731
final var verifier = JWT.require(Algorithm.RSA256(publicKey, null)).build();
2832
verifier.verify(token);
2933
return JwtToken.parseToken(token);

jwt/src/main/java/io/scalecube/security/jwt/JwtToken.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
import java.util.Base64;
77
import java.util.Map;
88

9+
/**
10+
* Represents parsed JWT (JSON Web Token), including its header and payload claims.
11+
*
12+
* @param header JWT header as map of key-value pairs
13+
* @param payload JWT payload (claims) as map of key-value pairs
14+
*/
915
public record JwtToken(Map<String, Object> header, Map<String, Object> payload) {
1016

1117
/**
1218
* Parses given JWT without verifying its signature.
1319
*
1420
* @param token jwt token
15-
* @return parsed token
21+
* @return {@link JwtToken} object, or {@link JwtTokenException} will be thrown
1622
*/
1723
public static JwtToken parseToken(String token) {
1824
String[] parts = token.split("\\.");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
import java.util.concurrent.CompletableFuture;
44

5+
/**
6+
* Resolves and verifies JWT tokens asynchronously. Implementations parse the token, validate its
7+
* signature, and extract claims.
8+
*/
59
public interface JwtTokenResolver {
610

711
/**
812
* Verifies given JWT and parses its header and claims.
913
*
1014
* @param token jwt token
11-
* @return async result with {@link JwtToken}, or error
15+
* @return async result completing with {@link JwtToken}, or completing exceptionally with {@link
16+
* JwtTokenException} on failure
1217
*/
1318
CompletableFuture<JwtToken> resolveToken(String token);
1419
}

tests/src/test/java/io/scalecube/security/jwt/JsonwebtokenResolverTests.java renamed to tests/src/test/java/io/scalecube/security/jwt/JwksTokenResolverTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
import org.junit.jupiter.api.extension.ExtendWith;
2020

2121
@ExtendWith(IntegrationEnvironmentFixture.class)
22-
public class JsonwebtokenResolverTests {
22+
public class JwksTokenResolverTests {
2323

2424
@Test
2525
void testResolveTokenTokenSuccessfully(VaultEnvironment vaultEnvironment) throws Exception {
2626
final var token = vaultEnvironment.newServiceToken();
2727

2828
final var jwtToken =
29-
new JsonwebtokenResolver(
30-
JwksKeyLocator.builder()
29+
new JwksTokenResolver(
30+
JwksKeyProvider.builder()
3131
.jwksUri(vaultEnvironment.jwksUri())
3232
.connectTimeout(Duration.ofSeconds(3))
3333
.requestTimeout(Duration.ofSeconds(3))
@@ -53,14 +53,14 @@ void testParseTokenSuccessfully(VaultEnvironment vaultEnvironment) {
5353
}
5454

5555
@Test
56-
void testJwksKeyLocatorThrowsError(VaultEnvironment vaultEnvironment) {
56+
void testJwksKeyProviderThrowsError(VaultEnvironment vaultEnvironment) {
5757
final var token = vaultEnvironment.newServiceToken();
5858

59-
final var keyLocator = mock(JwksKeyLocator.class);
60-
when(keyLocator.locate(any())).thenThrow(new RuntimeException("Cannot get key"));
59+
final var keyProvider = mock(JwksKeyProvider.class);
60+
when(keyProvider.getKey(any())).thenThrow(new RuntimeException("Cannot get key"));
6161

6262
try {
63-
new JsonwebtokenResolver(keyLocator).resolveToken(token).get(3, TimeUnit.SECONDS);
63+
new JwksTokenResolver(keyProvider).resolveToken(token).get(3, TimeUnit.SECONDS);
6464
fail("Expected exception");
6565
} catch (Exception e) {
6666
final var ex = getRootCause(e);
@@ -70,14 +70,14 @@ void testJwksKeyLocatorThrowsError(VaultEnvironment vaultEnvironment) {
7070
}
7171

7272
@Test
73-
void testJwksKeyLocatorThrowsRetryableError(VaultEnvironment vaultEnvironment) {
73+
void testJwksKeyProviderThrowsRetryableError(VaultEnvironment vaultEnvironment) {
7474
final var token = vaultEnvironment.newServiceToken();
7575

76-
final var keyLocator = mock(JwksKeyLocator.class);
77-
when(keyLocator.locate(any())).thenThrow(new JwtUnavailableException("JWKS timeout"));
76+
final var keyProvider = mock(JwksKeyProvider.class);
77+
when(keyProvider.getKey(any())).thenThrow(new JwtUnavailableException("JWKS timeout"));
7878

7979
try {
80-
new JsonwebtokenResolver(keyLocator).resolveToken(token).get(3, TimeUnit.SECONDS);
80+
new JwksTokenResolver(keyProvider).resolveToken(token).get(3, TimeUnit.SECONDS);
8181
fail("Expected exception");
8282
} catch (Exception e) {
8383
final var ex = getRootCause(e);

tests/src/test/java/io/scalecube/security/vault/VaultServiceTokenTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
import io.scalecube.security.environment.IntegrationEnvironmentFixture;
1313
import io.scalecube.security.environment.VaultEnvironment;
14-
import io.scalecube.security.jwt.JsonwebtokenResolver;
15-
import io.scalecube.security.jwt.JwksKeyLocator;
14+
import io.scalecube.security.jwt.JwksKeyProvider;
15+
import io.scalecube.security.jwt.JwksTokenResolver;
1616
import io.scalecube.security.vault.VaultServiceRolesInstaller.ServiceRoles;
1717
import io.scalecube.security.vault.VaultServiceRolesInstaller.ServiceRoles.Role;
1818
import java.util.Collections;
@@ -141,8 +141,7 @@ void testGetServiceTokenSuccessfully(VaultEnvironment vaultEnvironment) throws E
141141
// Verify serviceToken
142142

143143
final var jwtToken =
144-
new JsonwebtokenResolver(
145-
JwksKeyLocator.builder().jwksUri(vaultEnvironment.jwksUri()).build())
144+
new JwksTokenResolver(JwksKeyProvider.builder().jwksUri(vaultEnvironment.jwksUri()).build())
146145
.resolveToken(serviceToken)
147146
.get(3, TimeUnit.SECONDS);
148147

0 commit comments

Comments
 (0)