-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscoped_token_generation_example.py
More file actions
56 lines (44 loc) · 1.57 KB
/
scoped_token_generation_example.py
File metadata and controls
56 lines (44 loc) · 1.57 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
import json
from skyflow.service_account import (
generate_bearer_token,
generate_bearer_token_from_creds,
is_expired,
)
file_path = '<CREDENTIALS_FILE_PATH>'
bearer_token = ''
# To generate Bearer Token from credentials string.
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)
options = {'role_ids': ['ROLE_ID1', 'ROLE_ID2']}
def get_scoped_bearer_token_from_file_path():
# Generate scoped bearer token from credentials file path.
global bearer_token
try:
if not is_expired(bearer_token):
return bearer_token
else:
token, _ = generate_bearer_token(file_path, options)
bearer_token = token
return bearer_token
except Exception as e:
print(f'Error generating token from file path: {str(e)}')
def get_scoped_bearer_token_from_credentials_string():
# Generate scoped bearer token from credentials string.
global bearer_token
try:
if not is_expired(bearer_token):
return bearer_token
else:
token, _ = generate_bearer_token_from_creds(credentials_string, options)
bearer_token = token
return bearer_token
except Exception as e:
print(f"Error generating token from credentials string: {str(e)}")
print(get_scoped_bearer_token_from_file_path())
print(get_scoped_bearer_token_from_credentials_string())