Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): add configurable expiration time to tokens (CHIPS-505) #35

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>gr4vy</artifactId>
<packaging>jar</packaging>
<name>gr4vy</name>
<version>0.18.0</version>
<version>0.19.0</version>
<url>https://gr4vy.com</url>
<description>Gr4vy Java SDK</description>

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/gr4vy/sdk/Gr4vyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ public String getEmbedToken(Map<String, Object> embed, UUID checkoutSessionId) t
}

public String getToken(String key, String[] scopes, Map<String, Object> embed) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, JOSEException, ParseException {
return getToken(key, scopes, embed, null);
return getToken(key, scopes, embed, null, 60000);
}

public String getToken(String key, String[] scopes, Map<String, Object> embed, UUID checkoutSessionId) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, JOSEException, ParseException {
return getToken(key, scopes, embed, checkoutSessionId, 60000);
}

public String getToken(String key, String[] scopes, Map<String, Object> embed, UUID checkoutSessionId, int tokenExpiryMillis) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, JOSEException, ParseException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

Reader reader = new StringReader(key);
Expand All @@ -167,13 +171,13 @@ public String getToken(String key, String[] scopes, Map<String, Object> embed, U
JWSSigner signer = new ECDSASigner(e);

Date now = new Date();
Date expire = new Date(now.getTime() + 60 * 1000);
Date expire = new Date(now.getTime() + tokenExpiryMillis);

JWTClaimsSet.Builder claimsSet = new JWTClaimsSet.Builder()
.jwtID(UUID.randomUUID().toString())
.notBeforeTime(now)
.issueTime(now)
.issuer("Gr4vy SDK 0.1.0 - Java")
.issuer("Gr4vy SDK 0.19.0 - Java")
.expirationTime(expire)
.claim("scopes", scopes);

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/gr4vy/sdk/Gr4vyClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public void getTokenTest() throws Gr4vyException, NoSuchAlgorithmException, NoSu
assert token != null;
}

@Test
public void getTokenTestWithTokenExpiry() throws Gr4vyException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, IOException, JOSEException, ParseException {
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");

String key = client.getKey();
String[] scopes = {"*.read", "*.write"};
String token = client.getToken(key, scopes, null, null, 900000);

assert token != null;
}

@Test
public void addBuyersTest() throws Gr4vyException {
Gr4vyClient client = new Gr4vyClient("spider", "private_key.pem", "sandbox");
Expand Down
Loading