Guest User

Untitled

a guest
Feb 16th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import jwt
  2. import time
  3. import hashlib
  4.  
  5.  
  6. class 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
Add Comment
Please, Sign In to add comment