UidTokenObject.py 995 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import jwt
  2. from Ansjer.config import UID_TOKEN_KEY
  3. class UidTokenObject:
  4. def __init__(self, token=None):
  5. self.token = token
  6. self.UID = ''
  7. self.channel = ''
  8. self.flag = self.valid()
  9. def valid(self):
  10. token = self.token
  11. if self.token is None:
  12. return False
  13. try:
  14. res = jwt.decode(token, UID_TOKEN_KEY, algorithms='HS256')
  15. print(res)
  16. UID = res.get('uid', None)
  17. channel = res.get('channel', None)
  18. if UID is None:
  19. return False
  20. self.UID = UID
  21. self.channel = channel
  22. except jwt.ExpiredSignatureError as e:
  23. print('过期')
  24. print(repr(e))
  25. except Exception as e:
  26. print(repr(e))
  27. def generate(self, data=None):
  28. if data is None:
  29. data = {}
  30. token = jwt.encode(data, UID_TOKEN_KEY, algorithm='HS256')
  31. self.token = token
  32. return token