-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsigned_token_generation_example.py
More file actions
71 lines (61 loc) · 2.25 KB
/
signed_token_generation_example.py
File metadata and controls
71 lines (61 loc) · 2.25 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
import json
from skyflow.service_account import (
is_expired,
generate_signed_data_tokens,
generate_signed_data_tokens_from_creds,
)
file_path = 'CREDENTIALS_FILE_PATH'
bearer_token = ''
skyflow_credentials = {
'clientID': '<YOUR_CLIENT_ID>',
'clientName': '<YOUR_CLIENT_NAME>',
'tokenURI': '<YOUR_TOKEN_URI>',
'keyID': '<YOUR_KEY_ID>',
'privateKey': '<YOUR_PRIVATE_KEY>',
}
credentials_string = json.dumps(skyflow_credentials)
# Approach 1: Signed data tokens with string context
def get_signed_tokens_with_string_context():
options = {
'ctx': 'user_12345',
'data_tokens': ['DATA_TOKEN1', 'DATA_TOKEN2'],
'time_to_live': 90, # in seconds
}
try:
data_token, signed_data_token = generate_signed_data_tokens(file_path, options)
return data_token, signed_data_token
except Exception as e:
print(f'Error: {str(e)}')
# Approach 2: Signed data tokens with JSON object context (dict)
# Each key maps to a Skyflow CEL policy variable under request.context.*
# For example: request.context.role == "analyst" and request.context.department == "research"
def get_signed_tokens_with_object_context():
options = {
'ctx': {
'role': 'analyst',
'department': 'research',
'user_id': 'user_67890',
},
'data_tokens': ['DATA_TOKEN1', 'DATA_TOKEN2'],
'time_to_live': 90,
}
try:
data_token, signed_data_token = generate_signed_data_tokens(file_path, options)
return data_token, signed_data_token
except Exception as e:
print(f'Error: {str(e)}')
# Approach 3: Signed data tokens from credentials string
def get_signed_tokens_from_credentials_string():
options = {
'ctx': 'user_12345',
'data_tokens': ['DATA_TOKEN1', 'DATA_TOKEN2'],
'time_to_live': 90,
}
try:
data_token, signed_data_token = generate_signed_data_tokens_from_creds(credentials_string, options)
return data_token, signed_data_token
except Exception as e:
print(f'Error: {str(e)}')
print("String context:", get_signed_tokens_with_string_context())
print("Object context:", get_signed_tokens_with_object_context())
print("Creds string:", get_signed_tokens_from_credentials_string())