-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
41 lines (33 loc) · 1.21 KB
/
auth.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
import base64
import copy
import hmac
import json
import os
from time import time
TEN_MINUTES = 600 # seconds
def generate_jwt(api_key, secret_key, payload, expiration_time=TEN_MINUTES):
header = {
"alg": "HS256",
"typ": "JWT",
"kid": api_key
}
payload_copy = copy.deepcopy(payload)
current_timestamp = int(time())
payload_copy["exp"] = current_timestamp + expiration_time
payload_bytes = json.dumps(payload_copy, separators=(',', ':')).encode("utf-8")
header_bytes = json.dumps(header, separators=(',', ':')).encode("utf-8")
data = (base64.urlsafe_b64encode(header_bytes).strip(b'=') + b"." +
base64.urlsafe_b64encode(payload_bytes).strip(b'='))
signature = hmac.new(base64.urlsafe_b64decode(secret_key), msg=data, digestmod="sha256")
jwt = data + b"." + base64.urlsafe_b64encode(signature.digest()).strip(b'=')
return jwt.decode("utf-8")
def authorization_metadata(api_key, secret_key, scope, type=list):
auth_payload = {
"iss": "test_issuer",
"sub": "test_user",
"aud": scope
}
metadata = [
("authorization", "Bearer " + generate_jwt(api_key, secret_key, auth_payload))
]
return type(metadata)