· 6 years ago · Feb 17, 2019, 04:34 AM
1import jwt
2import time
3import hashlib
4
5
6class JWTGenerator:
7
8 def __init__(self, account_id, access_key, secret_key, canonical_path):
9 self.account_id = account_id
10 self.access_key = access_key
11 self.secret_key = secret_key
12 self.expire = 3600
13 self.canonical_path = canonical_path
14
15 @property
16 def jwt(self):
17 payload = {
18 'sub': self.account_id,
19 'qsh': hashlib.sha256(self.canonical_path.encode('utf-8')).hexdigest(),
20 'iss': self.access_key,
21 'exp': time.time()+self.expire,
22 'iat': time.time()
23 }
24 token = jwt.encode(payload, self.secret_key, algorithm='HS256')
25 return token
26
27 @property
28 def headers(self):
29 headers = {
30 'Authorization': 'JWT '+self.jwt(),
31 'Content-Type': 'application/json',
32 'zapiAccessKey': self.access_key
33 }
34 return headers