Skip to content

Commit 8e0f089

Browse files
Add claims exp and iat to admin JWT token (#865)
* upgrade java-jwt dependency * Add unit test to verify exp claim
1 parent 8e0e3e2 commit 8e0f089

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ dependencies {
6666
compile group: 'com.cronutils', name: 'cron-utils', version: '9.2.0'
6767
compile group: 'io.micrometer', name: 'micrometer-core', version: '1.10.2'
6868
compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
69-
compile group: 'com.auth0', name: 'java-jwt', version:'3.10.2'
69+
compile group: 'com.auth0', name: 'java-jwt', version:'4.4.0'
7070
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.21.9'
7171
compile group: 'com.google.api.grpc', name: 'proto-google-common-protos', version: '2.10.0'
7272
compile group: 'io.grpc', name: 'grpc-testing', version: '1.54.2'

src/main/java/com/uber/cadence/serviceclient/auth/AdminJwtAuthorizationProvider.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.security.interfaces.RSAPrivateKey;
2525
import java.security.interfaces.RSAPublicKey;
26-
import java.sql.Date;
27-
import java.time.Clock;
26+
import java.time.Instant;
2827

2928
public class AdminJwtAuthorizationProvider implements IAuthorizationProvider {
3029

@@ -38,10 +37,13 @@ public AdminJwtAuthorizationProvider(RSAPublicKey publicKey, RSAPrivateKey priva
3837

3938
@Override
4039
public byte[] getAuthToken() {
40+
final Instant now = Instant.now();
4141
final JWTCreator.Builder jwtBuilder = JWT.create();
42+
int JWT_TTL_SECONDS = 60 * 10;
4243
jwtBuilder.withClaim("admin", true);
43-
jwtBuilder.withClaim("ttl", 60 * 10);
44-
jwtBuilder.withIssuedAt(Date.from(Clock.systemUTC().instant()));
44+
jwtBuilder.withClaim("ttl", JWT_TTL_SECONDS);
45+
jwtBuilder.withIssuedAt(now);
46+
jwtBuilder.withExpiresAt(now.plusSeconds(JWT_TTL_SECONDS));
4547
return jwtBuilder
4648
.sign(Algorithm.RSA256(this.rsaPublicKey, this.rsaPrivateKey))
4749
.getBytes(StandardCharsets.UTF_8);

src/test/java/com/uber/cadence/serviceclient/auth/AdminJwtAuthorizationProviderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.security.spec.InvalidKeySpecException;
2929
import java.security.spec.PKCS8EncodedKeySpec;
3030
import java.security.spec.X509EncodedKeySpec;
31+
import java.util.Date;
3132
import org.apache.commons.codec.binary.Base64;
3233
import org.junit.Test;
3334

@@ -56,6 +57,10 @@ public void testCreateAuthToken() throws NoSuchAlgorithmException, InvalidKeySpe
5657
assertTrue(adminClaim.asBoolean());
5758
final Claim ttlClaim = decodedJwt.getClaim("ttl");
5859
assertEquals((int) (60 * 10), (int) ttlClaim.asInt());
60+
final Date expClaim = decodedJwt.getExpiresAt();
61+
// Check if expClaim and issued at + ttl is the same time
62+
assertEquals(
63+
expClaim.toInstant(), decodedJwt.getIssuedAt().toInstant().plusSeconds(ttlClaim.asInt()));
5964
}
6065

6166
private static String testPublicKey =

0 commit comments

Comments
 (0)