Skip to content
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 flowvault/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>skyflow-flowvault-java</artifactId>
<version>3.0.0-beta.13-dev.9a074a90</version>
<version>3.0.0-beta.13-dev.3671a19a</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>Skyflow V3 SDK for the Java programming language</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.example.serviceaccount;

import com.skyflow.Skyflow;
import com.skyflow.config.Credentials;
import com.skyflow.config.VaultConfig;
import com.skyflow.enums.Env;
import com.skyflow.enums.LogLevel;
import com.skyflow.enums.RedactionType;
import com.skyflow.errors.SkyflowException;
import com.skyflow.vault.tokens.DetokenizeData;
import com.skyflow.vault.tokens.DetokenizeRequest;
import com.skyflow.vault.tokens.DetokenizeResponse;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.ArrayList;

/**
* This example demonstrates how to configure and use the Skyflow SDK
* to detokenize sensitive data stored in a Skyflow vault.
* It includes setting up credentials, configuring the vault, and
* making a detokenization request. The code also implements a retry
* mechanism to handle unauthorized access errors (HTTP 401).
*/
public class BearerTokenExpiryExample {
public static void main(String[] args) {
try {
// Setting up credentials for accessing the Skyflow vault
Credentials vaultCredentials = new Credentials();
vaultCredentials.setCredentialsString("<YOUR_CREDENTIALS_STRING>");

// Configuring the Skyflow vault with necessary details
VaultConfig vaultConfig = new VaultConfig();
vaultConfig.setVaultId("<YOUR_VAULT_ID>"); // Vault ID
vaultConfig.setClusterId("<YOUR_CLUSTER_ID>"); // Cluster ID
vaultConfig.setEnv(Env.PROD); // Environment (e.g., DEV, PROD)
vaultConfig.setCredentials(vaultCredentials); // Setting credentials

// Creating a Skyflow client instance with the configured vault
Skyflow skyflowClient = Skyflow.builder()
.setLogLevel(LogLevel.ERROR) // Setting log level to ERROR
.addVaultConfig(vaultConfig) // Adding vault configuration
.build();

// Attempting to detokenize data using the Skyflow client
try {
detokenizeData(skyflowClient);
} catch (SkyflowException e) {
// Retry detokenization if the error is due to unauthorized access (HTTP 401)
if (e.getHttpCode() == 401) {
detokenizeData(skyflowClient);
} else {
// Rethrow the exception for other error codes
throw e;
}
}
} catch (SkyflowException e) {
// Handling any exceptions that occur during the process
System.out.println("An error occurred: " + e.getMessage());
}
}

/**
* Method to detokenize data using the Skyflow client.
* It sends a detokenization request with a list of tokens and prints the response.
*
* @param skyflowClient The Skyflow client instance used for detokenization.
* @throws SkyflowException If an error occurs during the detokenization process.
*/
public static void detokenizeData(Skyflow skyflowClient) throws SkyflowException {
// Creating a list of tokens to be detokenized
DetokenizeData detokenizeDataToken1 = new DetokenizeData("<YOUR_TOKEN_VALUE_1>", RedactionType.PLAIN_TEXT);
DetokenizeData detokenizeDataToken2 = new DetokenizeData("<YOUR_TOKEN_VALUE_2>");
ArrayList<DetokenizeData> detokenizeDataList = new ArrayList<>();
detokenizeDataList.add(detokenizeDataToken1); // First token
detokenizeDataList.add(detokenizeDataToken2); // Second token

// Building a detokenization request with the token list and configuration
DetokenizeRequest detokenizeRequest = DetokenizeRequest.builder()
.detokenizeData(detokenizeDataList) // Adding tokens to the request
.continueOnError(false) // Stop on error
.build();

// Sending the detokenization request and receiving the response
DetokenizeResponse detokenizeResponse = skyflowClient.vault().detokenize(detokenizeRequest);

// Printing the detokenized response
System.out.println(detokenizeResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.serviceaccount;

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

import java.io.File;

/**
* Example program to generate a Bearer Token using Skyflow's BearerToken utility.
* The token can be generated in two ways:
* 1. Using the file path to a credentials.json file.
* 2. Using the JSON content of the credentials file as a string.
*/
public class BearerTokenGenerationExample {
public static void main(String[] args) {
// Variable to store the generated token
String token = null;

// Example 1: Generate Bearer Token using a credentials.json file
try {
// Specify the full file path to the credentials.json file
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";

// Check if the token is either not initialized or has expired
if (Token.isExpired(token)) {
// Create a BearerToken object using the credentials file
BearerToken bearerToken = BearerToken.builder()
.setCredentials(new File(filePath)) // Set credentials from the file path
.build();

// Generate a new Bearer Token
token = bearerToken.getBearerToken();
}

// Print the generated Bearer Token to the console
System.out.println("Generated Bearer Token (from file): " + token);
} catch (SkyflowException e) {
// Handle any exceptions encountered during the token generation process
e.printStackTrace();
}

// Example 2: Generate Bearer Token using the credentials JSON as a string
try {
// Provide the credentials JSON content as a string
String fileContents = "<YOUR_CREDENTIALS_FILE_CONTENTS_AS_STRING>";

// Check if the token is either not initialized or has expired
if (Token.isExpired(token)) {
// Create a BearerToken object using the credentials string
BearerToken bearerToken = BearerToken.builder()
.setCredentials(fileContents) // Set credentials from the string
.build();

// Generate a new Bearer Token
token = bearerToken.getBearerToken();
}

// Print the generated Bearer Token to the console
System.out.println("Generated Bearer Token (from string): " + token);
} catch (SkyflowException e) {
// Handle any exceptions encountered during the token generation process
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.serviceaccount;

import com.skyflow.errors.SkyflowException;
import com.skyflow.serviceaccount.util.BearerToken;

import java.io.File;

/**
* This example demonstrates how to generate Bearer tokens in two different ways:
* 1. Using a credentials file specified by its file path.
* 2. Using the credentials as a string.
* <p>
* The code also showcases multithreaded token generation with a shared context (`ctx`),
* where each thread generates and prints tokens repeatedly.
*/
public class BearerTokenGenerationUsingThreadsExample {
public static void main(String[] args) {
// Example 1: Generate Bearer token using a credentials file path
try {
// Step 1: Specify the path to the credentials file
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>"; // Replace with the actual file path

// Step 2: Create a BearerToken object using the file path
final BearerToken bearerToken = BearerToken.builder()
.setCredentials(new File(filePath)) // Provide the credentials file
.setCtx("abc") // Specify a context string ("abc" in this case)
.build();

// Step 3: Create and start a thread to repeatedly generate and print tokens
Thread t = new Thread(() -> {
for (int i = 0; i < 5; i++) { // Loop to generate tokens 5 times
try {
System.out.println(bearerToken.getBearerToken()); // Print the Bearer token
} catch (SkyflowException e) { // Handle exceptions during token generation
Thread.currentThread().interrupt(); // Interrupt the thread on error
throw new RuntimeException(e); // Wrap and propagate the exception
}
}
});
t.start(); // Start the thread
} catch (Exception e) { // Handle exceptions during BearerToken creation
e.printStackTrace();
}

// Example 2: Generate Bearer token using credentials as a string
try {
// Step 1: Specify the credentials as a string (file contents)
String fileContents = "<YOUR_CREDENTIALS_FILE_CONTENTS_AS_STRING>"; // Replace with actual file contents

// Step 2: Create a BearerToken object using the credentials string
final BearerToken bearerToken = BearerToken.builder()
.setCredentials(fileContents) // Provide the credentials as a string
.setCtx("abc") // Specify a context string ("abc" in this case)
.build();

// Step 3: Create and start a thread to repeatedly generate and print tokens
Thread t = new Thread(() -> {
for (int i = 0; i < 5; i++) { // Loop to generate tokens 5 times
try {
System.out.println(bearerToken.getBearerToken()); // Print the Bearer token
} catch (SkyflowException e) { // Handle exceptions during token generation
Thread.currentThread().interrupt(); // Interrupt the thread on error
throw new RuntimeException(e); // Wrap and propagate the exception
}
}
});
t.start(); // Start the thread
} catch (Exception e) { // Handle exceptions during BearerToken creation
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.example.serviceaccount;

import com.skyflow.errors.SkyflowException;
import com.skyflow.serviceaccount.util.BearerToken;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* Example program to generate a Bearer Token using Skyflow's BearerToken utility.
* The token is generated using three approaches:
* 1. By providing a string context.
* 2. By providing a JSON object context (Map) for conditional data access policies.
* 3. By providing the credentials as a string with context.
*/
public class BearerTokenGenerationWithContextExample {
public static void main(String[] args) {
String bearerToken = null;

// Approach 1: Bearer token with string context
// Use a simple string identifier when your policy references a single context value.
try {
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";
BearerToken token = BearerToken.builder()
.setCredentials(new File(filePath))
.setCtx("user_12345")
.build();

bearerToken = token.getBearerToken();
System.out.println("Bearer token (string context): " + bearerToken);
} catch (SkyflowException e) {
e.printStackTrace();
}

// Approach 2: Bearer token with JSON object context
// Use a structured Map when your policy needs multiple context values.
// Each key maps to a Skyflow CEL policy variable under request.context.*
// For example, the map below enables policies like:
// request.context.role == "admin" && request.context.department == "finance"
try {
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";
Map<String, Object> ctx = new HashMap<>();
ctx.put("role", "admin");
ctx.put("department", "finance");
ctx.put("user_id", "user_12345");

BearerToken token = BearerToken.builder()
.setCredentials(new File(filePath))
.setCtx(ctx)
.build();

bearerToken = token.getBearerToken();
System.out.println("Bearer token (object context): " + bearerToken);
} catch (SkyflowException e) {
e.printStackTrace();
}

// Approach 3: Bearer token with string context from credentials string
try {
String fileContents = "<YOUR_CREDENTIALS_FILE_CONTENTS_AS_STRING>";
BearerToken token = BearerToken.builder()
.setCredentials(fileContents)
.setCtx("user_12345")
.build();

bearerToken = token.getBearerToken();
System.out.println("Bearer token (creds string): " + bearerToken);
} catch (SkyflowException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.serviceaccount;

import com.skyflow.errors.SkyflowException;
import com.skyflow.serviceaccount.util.BearerToken;

import java.io.File;
import java.util.ArrayList;

/**
* This example demonstrates how to generate a Scoped Bearer Token in two ways:
* 1. Using a credentials file specified by its file path.
* 2. Using the credentials as a string.
* <p>
* Scoped tokens are generated by assigning specific roles for access control.
*/
public class ScopedTokenGenerationExample {
public static void main(String[] args) {
String scopedToken = null; // Variable to store the generated Scoped Bearer Token

// Example 1: Generate Scoped Token using a credentials file path
try {
// Step 1: Specify the roles required for the scoped token
ArrayList<String> roles = new ArrayList<>();
roles.add("YOUR_ROLE_ID"); // Replace with your actual role ID

// Step 2: Specify the path to the credentials file
String filePath = "<YOUR_CREDENTIALS_FILE_PATH>"; // Replace with the actual file path

// Step 3: Create a BearerToken object using the file path and roles
BearerToken bearerToken = BearerToken.builder()
.setCredentials(new File(filePath)) // Provide the credentials file
.setRoles(roles) // Set the roles for the scoped token
.build();

// Step 4: Generate and print the Scoped Bearer Token
scopedToken = bearerToken.getBearerToken();
System.out.println("Scoped Token (using file path): " + scopedToken);
} catch (SkyflowException e) { // Handle exceptions during token generation
System.out.println("Error occurred while generating Scoped Token using file path:");
e.printStackTrace();
}

// Example 2: Generate Scoped Token using credentials as a string
try {
// Step 1: Specify the roles required for the scoped token
ArrayList<String> roles = new ArrayList<>();
roles.add("YOUR_ROLE_ID"); // Replace with your actual role ID

// Step 2: Specify the credentials as a string (file contents)
String fileContents = "<YOUR_CREDENTIALS_FILE_CONTENTS_AS_STRING>"; // Replace with actual file contents

// Step 3: Create a BearerToken object using the credentials string and roles
BearerToken bearerToken = BearerToken.builder()
.setCredentials(fileContents) // Provide the credentials as a string
.setRoles(roles) // Set the roles for the scoped token
.build();

// Step 4: Generate and print the Scoped Bearer Token
scopedToken = bearerToken.getBearerToken();
System.out.println("Scoped Token (using credentials string): " + scopedToken);
} catch (SkyflowException e) { // Handle exceptions during token generation
System.out.println("Error occurred while generating Scoped Token using credentials string:");
e.printStackTrace();
}
}
}
Loading
Loading