Skip to content

Commit

Permalink
Merge pull request #57 from skyflowapi/SK-176/update-context-to-ctx
Browse files Browse the repository at this point in the history
SK-176 update context to ctx, change ttl data type to integer
  • Loading branch information
Shaik-Luqmaan authored Nov 29, 2022
2 parents 2a47ebb + d0517a6 commit 39300c9
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 52 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.7.1] - 2022-11-29
### Changed
- `setContext` to `setCtx` method.
- `setTimetoLive` accepts seconds in `Integer` instead of `Double`.


## [1.7.0] - 2022-11-22
### Added
- `upsert` support for insert method.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The Skyflow Java SDK is designed to help with integrating Skyflow into a Java ba

Add this dependency to your project's build file:
```
implementation 'com.skyflow:skyflow-java:1.7.0'
implementation 'com.skyflow:skyflow-java:1.7.1'
```

#### Maven users
Expand All @@ -46,7 +46,7 @@ Add this dependency to your project's POM:
<dependency>
<groupId>com.skyflow</groupId>
<artifactId>skyflow-java</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
</dependency>
```
---
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.skyflow</groupId>
<artifactId>skyflow-java</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
2 changes: 1 addition & 1 deletion samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.skyflow</groupId>
<artifactId>skyflow-java</artifactId>
<version>1.4.1</version>
<version>1.7.1</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/*
Copyright (c) 2022 Skyflow, Inc.
*/
package com.skyflow.serviceaccount.util;
package com.example;

import com.skyflow.entities.ResponseToken;
import com.skyflow.errors.SkyflowException;
import com.skyflow.serviceaccount.util.Token;
import com.skyflow.serviceaccount.util.BearerToken;

import java.io.File;
import java.util.List;

public class BearerTokenWithContextGeneration {
public class BearerTokenGenerationUsingThreadsExample {
public static void main(String args[]) {

String bearerToken = null;
Expand All @@ -19,7 +18,7 @@ public static void main(String args[]) {
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";
final BearerToken token = new BearerToken.BearerTokenBuilder()
.setCredentials(new File(filePath))
.setContext("abc")
.setCtx("abc")
.build();

Thread t = new Thread(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
package com.example;

import com.skyflow.entities.ResponseToken;
import com.skyflow.errors.SkyflowException;
import com.skyflow.serviceaccount.util.Token;
import com.skyflow.serviceaccount.util.BearerToken;

import java.io.File;

public class BearerTokenWithContextGenerationExample {
Expand All @@ -18,7 +18,7 @@ public static void main(String args[]) {
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";
BearerToken token = new BearerToken.BearerTokenBuilder()
.setCredentials(new File(filePath))
.setContext("abc")
.setCtx("abc")
.build();

bearerToken = token.getBearerToken();
Expand All @@ -32,7 +32,7 @@ public static void main(String args[]) {
String fileContents = "<YOUR_CREDENTIALS_FILE_CONTENTS_AS_STRING>";
BearerToken token = new BearerToken.BearerTokenBuilder()
.setCredentials(fileContents)
.setContext("abc")
.setCtx("abc")
.build();

bearerToken = token.getBearerToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package com.example;

import com.skyflow.serviceaccount.util.BearerToken;
import java.io.File;

public class ScopedTokenGenerationExample {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package com.example;

import com.skyflow.errors.SkyflowException;
import com.skyflow.serviceaccount.util.SignedDataTokenResponse;
import com.skyflow.serviceaccount.util.SignedDataTokens;

import java.io.File;
import java.util.List;

Expand All @@ -18,8 +21,8 @@ public static void main(String args[]) {
String context = "abc";
SignedDataTokens signedToken = new SignedDataTokens.SignedDataTokensBuilder()
.setCredentials(new File(filePath))
.setContext(context)
.setTimeToLive(30.0) // in seconds
.setCtx(context)
.setTimeToLive(30) // in seconds
.setDataTokens(new String[]{"dataToken1"}).build();

signedTokenValue = signedToken.getSignedDataTokens();
Expand All @@ -34,8 +37,8 @@ public static void main(String args[]) {
String context = "abc";
SignedDataTokens signedToken = new SignedDataTokens.SignedDataTokensBuilder()
.setCredentials(fileContents)
.setContext(context)
.setTimeToLive(30.0) // in seconds
.setCtx(context)
.setTimeToLive(30) // in seconds
.setDataTokens(new String[]{"dataToken1"}).build();

signedTokenValue = signedToken.getSignedDataTokens();
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/skyflow/serviceaccount/util/BearerToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class BearerToken {
private final File credentialsFile;
private final String credentialsString;
private final String context;
private final String ctx;

private final String[] roles;

Expand All @@ -37,7 +37,7 @@ public class BearerToken {
private BearerToken(BearerTokenBuilder builder) {
this.credentialsFile = builder.credentialsFile;
this.credentialsString = builder.credentialsString;
this.context = builder.context;
this.ctx = builder.ctx;
this.roles = builder.roles;
this.credentialsType = builder.credentialsType;
}
Expand All @@ -46,7 +46,7 @@ private BearerToken(BearerTokenBuilder builder) {
public static class BearerTokenBuilder {
private File credentialsFile;
private String credentialsString;
private String context;
private String ctx;
private String[] roles;

private String credentialsType;
Expand All @@ -67,8 +67,8 @@ public BearerTokenBuilder setCredentials(String credentialsString) {
return this;
}

public BearerTokenBuilder setContext(String context) {
this.context = context;
public BearerTokenBuilder setCtx(String ctx) {
this.ctx = ctx;
return this;
}

Expand All @@ -89,12 +89,11 @@ public synchronized String getBearerToken() throws SkyflowException {

try {
if (this.credentialsFile != null && Objects.equals(this.credentialsType, "FILE")) {
response = generateBearerTokenFromCredentials(this.credentialsFile, this.context, this.roles);
response = generateBearerTokenFromCredentials(this.credentialsFile, this.ctx, this.roles);
accessToken = response.getAccessToken();

} else if (this.credentialsString != null && Objects.equals(this.credentialsType, "STRING")) {

response = generateBearerTokenFromCredentialString(this.credentialsString, this.context, this.roles);
response = generateBearerTokenFromCredentialString(this.credentialsString, this.ctx, this.roles);
accessToken = response.getAccessToken();
}
} catch (SkyflowException e) {
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/skyflow/serviceaccount/util/SignedDataTokens.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
public class SignedDataTokens {
private final File credentialsFile;
private final String credentialsString;
private final String context;
private final String ctx;

private final String[] dataTokens;
private final Double timeToLive;
private final Integer timeToLive;

private final String credentialsType;

private SignedDataTokens(SignedDataTokensBuilder builder) {
this.credentialsFile = builder.credentialsFile;
this.credentialsString = builder.credentialsString;
this.context = builder.context;
this.ctx = builder.ctx;
this.timeToLive = builder.timeToLive;
this.dataTokens = builder.dataTokens;
this.credentialsType = builder.credentialsType;
Expand All @@ -47,10 +47,10 @@ private SignedDataTokens(SignedDataTokensBuilder builder) {
public static class SignedDataTokensBuilder {
private File credentialsFile;
private String credentialsString;
private String context;
private String ctx;

private String[] dataTokens;
private Double timeToLive;
private Integer timeToLive;

private String credentialsType;

Expand All @@ -70,8 +70,8 @@ public SignedDataTokensBuilder setCredentials(String credentialsString) {
return this;
}

public SignedDataTokensBuilder setContext(String context) {
this.context = context;
public SignedDataTokensBuilder setCtx(String ctx) {
this.ctx = ctx;
return this;
}

Expand All @@ -80,7 +80,7 @@ public SignedDataTokensBuilder setDataTokens(String[] dataTokens) {
return this;
}

public SignedDataTokensBuilder setTimeToLive(Double timeToLive) {
public SignedDataTokensBuilder setTimeToLive(Integer timeToLive) {
this.timeToLive = timeToLive;
return this;
}
Expand All @@ -96,10 +96,10 @@ public synchronized List<SignedDataTokenResponse> getSignedDataTokens() throws S
try {
if (this.credentialsFile != null && Objects.equals(this.credentialsType, "FILE")) {
signedToken = generateSignedTokens(this.credentialsFile, this.dataTokens, this.timeToLive,
this.context);
this.ctx);
} else if (this.credentialsString != null && Objects.equals(this.credentialsType, "STRING")) {
signedToken = generateSignedTokensFromCredentialsString(this.credentialsString, this.dataTokens,
this.timeToLive, this.context);
this.timeToLive, this.ctx);

}
} catch (SkyflowException e) {
Expand All @@ -109,7 +109,7 @@ public synchronized List<SignedDataTokenResponse> getSignedDataTokens() throws S
}

private static List<SignedDataTokenResponse> generateSignedTokens(File credentialsPath, String[] dataTokens,
Double timeToLive, String context) throws SkyflowException {
Integer timeToLive, String context) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.GenerateBearerTokenFromCredsCalled.getLog());
JSONParser parser = new JSONParser();
List<SignedDataTokenResponse> responseToken;
Expand All @@ -136,7 +136,7 @@ private static List<SignedDataTokenResponse> generateSignedTokens(File credentia
}

private static List<SignedDataTokenResponse> generateSignedTokensFromCredentialsString(String credentials,
String[] dataTokens, Double timeToLive, String context) throws SkyflowException {
String[] dataTokens, Integer timeToLive, String context) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.GenerateBearerTokenFromCredsCalled.getLog());
JSONParser parser = new JSONParser();
List<SignedDataTokenResponse> responseToken;
Expand All @@ -162,7 +162,7 @@ private static List<SignedDataTokenResponse> generateSignedTokensFromCredentials
}

private static List<SignedDataTokenResponse> getSignedTokenFromCredsFile(JSONObject creds, String[] dataTokens,
Double timeToLive, String context) throws SkyflowException {
Integer timeToLive, String context) throws SkyflowException {
List<SignedDataTokenResponse> responseToken;

try {
Expand All @@ -189,12 +189,12 @@ private static List<SignedDataTokenResponse> getSignedTokenFromCredsFile(JSONObj
}

private static List<SignedDataTokenResponse> getSignedToken(String clientID, String keyID, PrivateKey pvtKey,
String[] dataTokens, Double timeToLive, String context) {
String[] dataTokens, Integer timeToLive, String context) {
final Date createdDate = new Date();
final Date expirationDate;

if (timeToLive != null) {
expirationDate = new Date((long) (createdDate.getTime() + (timeToLive * 1000)));
expirationDate = new Date(createdDate.getTime() + (timeToLive * 1000));
} else {
expirationDate = new Date(createdDate.getTime() + 60000); // Valid for 60 seconds
}
Expand All @@ -205,7 +205,7 @@ private static List<SignedDataTokenResponse> getSignedToken(String clientID, Str

String eachSignedDataToken = Jwts.builder()
.claim("iss", "sdk")
.claim("iat", createdDate.getTime())
.claim("iat", (createdDate.getTime()/1000))
.claim("key", keyID)
.claim("sub", clientID)
.claim("ctx", context)
Expand Down
Loading

0 comments on commit 39300c9

Please sign in to comment.