-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBearerTokenGenerationWithContextExample.java
More file actions
73 lines (63 loc) · 2.81 KB
/
BearerTokenGenerationWithContextExample.java
File metadata and controls
73 lines (63 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();
}
}
}