UidTokenObject.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2018/12/5 11:52
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: UidTokenObject.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from AnsjerPush.config import UID_TOKEN_KEY
  15. import jwt
  16. # UID_TOKEN_KEY = 'c+565*j@%^'
  17. class UidTokenObject:
  18. def __init__(self, token=None):
  19. self.token = token
  20. self.UID = ''
  21. self.channel = ''
  22. self.flag = self.valid()
  23. def valid(self):
  24. token = self.token
  25. if self.token is None:
  26. return False
  27. res = jwt.decode(token, UID_TOKEN_KEY, algorithms='HS256')
  28. print(res)
  29. UID = res.get('uid', None)
  30. channel = res.get('channel', None)
  31. if UID is None:
  32. return False
  33. self.UID = UID
  34. self.channel = channel
  35. def generate(self, data={}):
  36. token = jwt.encode(data, UID_TOKEN_KEY, algorithm='HS256').decode('utf-8')
  37. self.token=token
  38. return token
  39. #
  40. # uto = UidTokenObject()
  41. # res = uto.generate(data={'uid':'VVDHCVBYDKFMJRWA111A','channel':'1'})
  42. # print('---')
  43. # print(res)