Skip to content

Commit

Permalink
add tokenExpirySeconds to getToken
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-gr4vy committed Nov 29, 2023
1 parent c852efa commit 4d6e1e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
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, 60);
}

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, 60);
}

public String getToken(String key, String[] scopes, Map<String, Object> embed, UUID checkoutSessionId, int tokenExpirySeconds) 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() + tokenExpirySeconds * 1000);

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, 900);

assert token != null;
}

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

0 comments on commit 4d6e1e7

Please sign in to comment.