forked from docusign/code-examples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (56 loc) · 2.33 KB
/
utils.py
File metadata and controls
71 lines (56 loc) · 2.33 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
from datetime import timedelta, datetime
from functools import wraps
from docusign_esign import ApiClient
from flask import session, flash, url_for, redirect
from .ds_client import DSClient
from ..consts import minimum_buffer_min
def ds_logout_internal():
# remove the keys and their values from the session
session.pop("ds_access_token", None)
session.pop("ds_refresh_token", None)
session.pop("ds_user_email", None)
session.pop("ds_user_name", None)
session.pop("ds_expiration", None)
session.pop("ds_account_id", None)
session.pop("ds_account_name", None)
session.pop("ds_base_path", None)
session.pop("envelope_id", None)
session.pop("eg", None)
session.pop("envelope_documents", None)
session.pop("template_id", None)
session.pop("auth_type", None)
DSClient.destroy()
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
def ds_token_ok(buffer_min=60):
"""
:param buffer_min: buffer time needed in minutes
:return: true iff the user has an access token that will be good for another buffer min
"""
ok = "ds_access_token" in session and "ds_expiration" in session
ok = ok and (session["ds_expiration"] - timedelta(minutes=buffer_min)) > datetime.utcnow()
return ok
def authenticate(eg):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if ds_token_ok(minimum_buffer_min):
return func(*args, **kwargs)
else:
# We could store the parameters of the requested operation
# so it could be restarted automatically.
# But since it should be rare to have a token issue here,
# we"ll make the user re-enter the form data after
# authentication.
session["eg"] = url_for(eg + ".get_view")
if session.get("auth_type"):
flash("Token has been updated")
return redirect(url_for("ds.ds_login"))
else:
return redirect(url_for("ds.ds_must_authenticate"))
return wrapper
return decorator