-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
39 lines (34 loc) · 1.19 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
import base64
import json
def get_payload_from_token(token) -> dict:
if not token:
return {}
payload = token.split('.')[1]
payload += "=" * ((4 - len(payload) % 4) % 4) # solve the padding issue of the base64 python lib
return json.loads(base64.urlsafe_b64decode(payload).decode())
def get_header_from_token(token) -> dict:
if not token:
return {}
header = token.split('.')[0]
header += "=" * ((4 - len(header) % 4) % 4) # solve the padding issue of the base64 python lib
return json.loads(base64.urlsafe_b64decode(header).decode())
def alg(key):
key = json.loads(key) if isinstance(key, str) else key
if key['kty'] == 'EC':
if key['crv'] in ['secp256k1', 'P-256K']:
key['crv'] = 'secp256k1'
return 'ES256K'
elif key['crv'] == 'P-256':
return 'ES256'
elif key['crv'] == 'P-384':
return 'ES384'
elif key['crv'] == 'P-521':
return 'ES512'
else:
raise Exception("Curve not supported")
elif key['kty'] == 'RSA':
return 'RS256'
elif key['kty'] == 'OKP':
return 'EdDSA'
else:
raise Exception("Key type not supported")