-
Notifications
You must be signed in to change notification settings - Fork 51
/
jwt_helper.py
39 lines (33 loc) · 1.24 KB
/
jwt_helper.py
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
from docusign_esign import ApiClient
from os import path
def get_jwt_token(private_key, scopes, auth_server, client_id, impersonated_user_id):
"""Get the jwt token"""
api_client = ApiClient()
api_client.set_base_path(auth_server)
response = api_client.request_jwt_user_token(
client_id=client_id,
user_id=impersonated_user_id,
oauth_host_name=auth_server,
private_key_bytes=private_key,
expires_in=4000,
scopes=scopes
)
return response
def get_private_key(private_key_path):
"""
Check that the private key present in the file and if it is, get it from the file.
In the opposite way get it from config variable.
"""
private_key_file = path.abspath(private_key_path)
if path.isfile(private_key_file):
with open(private_key_file) as private_key_file:
private_key = private_key_file.read()
else:
private_key = private_key_path
return private_key
def create_api_client(base_path, access_token):
"""Create api client and construct API headers"""
api_client = ApiClient()
api_client.host = base_path
api_client.set_default_header(header_name="Authorization", header_value=f"Bearer {access_token}")
return api_client