-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
89 lines (66 loc) · 2.16 KB
/
helpers.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
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
import datetime
import json
import os
import jwt
from typing import Dict, Any
# SETTINGS
SECRET_KEY = os.environ['SECRET_KEY']
JWT_EXPIRATION_MINUTES = float(os.environ['JWT_EXPIRATION_MINUTES'])
JWT_DOMAIN = 'benjamin.dog'
# JWT HELPERS
class JSONDataEncoder(json.JSONEncoder):
""" setup a json encoder to convert datetime objects """
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
elif isinstance(obj, datetime.date):
return obj.isoformat()
elif isinstance(obj, datetime.timedelta):
return (datetime.datetime.min + obj).time().isoformat()
elif isinstance(obj, set):
return list(obj)
else:
return super().default(obj)
def build_jwt(payload: dict) -> str:
""" return a jwt token for the payload """
if 'sub' not in payload.keys():
raise ValueError('sub not in payload keys')
jwt_fields = {
'iss': JWT_DOMAIN,
'sub': None,
'iat': datetime.datetime.utcnow(),
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=JWT_EXPIRATION_MINUTES),
**payload
}
return jwt.encode(jwt_fields, key=SECRET_KEY, json_encoder=JSONDataEncoder).decode(encoding='UTF-8')
def verify_jwt(token):
""" verity the jwt token """
return jwt.decode(token.encode(), SECRET_KEY)
# AWS LAMBDA TYPE HELPERS
LambdaDict = Dict[str, Any]
class LambdaCognitoIdentity(object):
cognito_identity_id: str
cognito_identity_pool_id: str
class LambdaClientContextMobileClient(object):
installation_id: str
app_title: str
app_version_name: str
app_version_code: str
app_package_name: str
class LambdaClientContext(object):
client: LambdaClientContextMobileClient
custom: LambdaDict
env: LambdaDict
class LambdaContext(object):
function_name: str
function_version: str
invoked_function_arn: str
memory_limit_in_mb: int
aws_request_id: str
log_group_name: str
log_stream_name: str
identity: LambdaCognitoIdentity
client_context: LambdaClientContext
@staticmethod
def get_remaining_time_in_millis() -> int:
return 0