TokenObject.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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: AnsjerOA
  7. @software: PyCharm
  8. @DATE: 2018/8/13 15:36
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: TokenObject.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from Ansjer.config import OAUTH_ACCESS_TOKEN_SECRET, OAUTH_REFRESH_TOKEN_SECRET, OAUTH_ACCESS_TOKEN_TIME, \
  15. OAUTH_REFRESH_TOKEN_TIME
  16. import jwt, time
  17. from Model.models import Device_User
  18. class TokenObject:
  19. def __init__(self, token=None):
  20. if token == 'debug':
  21. token = 'x'
  22. if token == 'test':
  23. token = 'x'
  24. self.token = token
  25. self.code = 0
  26. self.lang = None
  27. self.userID = None
  28. def valid(self):
  29. if self.token is None:
  30. self.code = 309
  31. return
  32. try:
  33. res = jwt.decode(self.token, OAUTH_ACCESS_TOKEN_SECRET, algorithms='HS256')
  34. self.userID = res.get('userID', None)
  35. self.lang = res.get('lang', None)
  36. # 刷新登录时间
  37. device_user = Device_User.objects.get(userID=self.userID)
  38. device_user.online = True
  39. device_user.save()
  40. except jwt.ExpiredSignatureError as e:
  41. print('过期')
  42. print(repr(e))
  43. # self.code = 307
  44. self.code = 309
  45. except Exception as e:
  46. # self.code = 305
  47. self.code = 309
  48. else:
  49. return res
  50. def generate(self, data={}):
  51. try:
  52. access_expire = int(OAUTH_ACCESS_TOKEN_TIME.total_seconds())
  53. refresh_expire = int(OAUTH_REFRESH_TOKEN_TIME.total_seconds())
  54. now_stamp = int(time.time())
  55. access_data = data
  56. refresh_data = data
  57. access_data['exp'] = access_expire+now_stamp
  58. refresh_data['exp'] = refresh_expire+now_stamp
  59. access_token = jwt.encode(access_data,
  60. OAUTH_ACCESS_TOKEN_SECRET,
  61. algorithm='HS256')
  62. refresh_token = jwt.encode(
  63. refresh_data,
  64. OAUTH_REFRESH_TOKEN_SECRET,
  65. algorithm='HS256')
  66. res = {
  67. 'access_token': access_token.decode('utf-8'),
  68. 'access_expire': access_expire,
  69. 'refresh_expire': refresh_expire,
  70. 'refresh_token': refresh_token.decode('utf-8'),
  71. }
  72. except Exception as e:
  73. self.code = 309
  74. print(repr(e))
  75. else:
  76. return res
  77. def refresh(self):
  78. try:
  79. res = jwt.decode(self.token, OAUTH_REFRESH_TOKEN_SECRET, algorithms='HS256')
  80. except jwt.ExpiredSignatureError as e:
  81. print('过期')
  82. print(repr(e))
  83. self.code = 309
  84. except Exception as e:
  85. self.code = 309
  86. print(repr(e))
  87. else:
  88. userID = res['userID']
  89. lang = res['lang']
  90. refreshRes = self.generate(data={'userID': userID, 'lang': lang})
  91. return refreshRes