Repository navigation
Expand file tree
/
Copy pathtoken-generation-with-context-example.ts
More file actions
110 lines (103 loc) · 3.01 KB
/
Copy pathtoken-generation-with-context-example.ts
File metadata and controls
110 lines (103 loc) · 3.01 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright (c) 2022 Skyflow, Inc.
*/
import {
generateBearerToken,
generateBearerTokenFromCreds,
isExpired,
} from 'skyflow-node';
const filepath: string = 'CREDENTIALS_FILE_PATH';
let bearerToken: string = '';
// To generate Bearer Token from credentials string.
const cred = {
clientID: '<YOUR_CLIENT_ID>',
clientName: '<YOUR_CLIENT_NAME>',
keyID: '<YOUR_KEY_ID>',
tokenURI: '<YOUR_TOKEN_URI>',
privateKey: '<YOUR_PEM_PRIVATE_KEY>',
};
// Approach 1: Bearer token with string context
// Use a simple string identifier when your policy references a single context value.
// In your Skyflow policy, reference this as: request.context
function getSkyflowBearerTokenWithStringContext() {
return new Promise((resolve, reject) => {
try {
const options = {
ctx: 'user_12345',
};
if (!isExpired(bearerToken)) resolve(bearerToken);
else {
generateBearerToken(filepath, options)
.then(response => {
bearerToken = response.accessToken;
resolve(bearerToken);
})
.catch(error => {
reject(error);
});
}
} catch (e) {
reject(e);
}
});
}
// Approach 2: Bearer token with JSON object context
// Use a structured object when your policy needs multiple context values.
// Each key maps to a Skyflow CEL policy variable under request.context.*
// For example, the object below enables policies like:
// request.context.role == "admin" && request.context.department == "finance"
function getSkyflowBearerTokenWithObjectContext() {
return new Promise((resolve, reject) => {
try {
const options = {
ctx: {
role: 'admin',
department: 'finance',
user_id: 'user_12345',
},
};
if (!isExpired(bearerToken)) resolve(bearerToken);
else {
generateBearerToken(filepath, options)
.then(response => {
bearerToken = response.accessToken;
resolve(bearerToken);
})
.catch(error => {
reject(error);
});
}
} catch (e) {
reject(e);
}
});
}
// Approach 3: Bearer token with string context from credentials string
function getSkyflowBearerTokenWithContextFromCreds() {
return new Promise((resolve, reject) => {
try {
const options = {
ctx: 'context_id',
};
if (!isExpired(bearerToken)) resolve(bearerToken);
else {
generateBearerTokenFromCreds(JSON.stringify(cred), options)
.then(response => {
bearerToken = response.accessToken;
resolve(bearerToken);
})
.catch(error => {
reject(error);
});
}
} catch (e) {
reject(e);
}
});
}
const tokens = async () => {
console.log('String context:', await getSkyflowBearerTokenWithStringContext());
console.log('Object context:', await getSkyflowBearerTokenWithObjectContext());
console.log('Creds string context:', await getSkyflowBearerTokenWithContextFromCreds());
};
tokens();