TokenObject.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. from Object.RedisObject import RedisObject
  19. class TokenObject:
  20. def __init__(self, token=None):
  21. if token == 'local':
  22. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiZW4iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJleHAiOjE1NDgzMDA0MDV9.bsPkGbiHFjamv7JpYpUwRNi2QONfisvEByacA30abts'
  23. if token == 'test':
  24. token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJleHAiOjE1NDgzODM1ODQsImxhbmciOiJlbiIsInVzZXJJRCI6IjE1MTU2NDI2MjMzNzkzOTUxMzgwMDEzODAwMSJ9.0z1oCXI4xPb2YUV8bw7pS5l_NGotGTx9DBurGA9wojM'
  25. self.token = token
  26. self.lang = None
  27. self.userID = None
  28. self.user = ''
  29. self.valid()
  30. if token is None:
  31. self.code = 309
  32. else:
  33. self.code = 0
  34. def valid(self):
  35. try:
  36. res = jwt.decode(self.token, OAUTH_ACCESS_TOKEN_SECRET, algorithms='HS256')
  37. # print(res)
  38. self.userID = res.get('userID', None)
  39. self.lang = res.get('lang', None)
  40. self.user = res.get('user', '')
  41. # 刷新登录时间
  42. if self.userID:
  43. # device_user = Device_User.objects.get(userID=self.userID)
  44. # device_user.online = True
  45. # device_user.save()
  46. redisObj = RedisObject(db=3)
  47. redisObj.set_data(key=self.userID, val='user', expire=300)
  48. except jwt.ExpiredSignatureError as e:
  49. print('过期')
  50. print(repr(e))
  51. self.code = 309
  52. except Exception as e:
  53. self.code = 309
  54. else:
  55. self.code = 0
  56. if not self.userID:
  57. self.code = 309
  58. return res
  59. def generate(self, data={}):
  60. try:
  61. access_expire = int(OAUTH_ACCESS_TOKEN_TIME.total_seconds())
  62. refresh_expire = int(OAUTH_REFRESH_TOKEN_TIME.total_seconds())
  63. now_stamp = int(time.time())
  64. access_data = data
  65. refresh_data = data
  66. access_data['exp'] = access_expire + now_stamp
  67. refresh_data['exp'] = refresh_expire + now_stamp
  68. access_token = jwt.encode(access_data,
  69. OAUTH_ACCESS_TOKEN_SECRET,
  70. algorithm='HS256')
  71. refresh_token = jwt.encode(
  72. refresh_data,
  73. OAUTH_REFRESH_TOKEN_SECRET,
  74. algorithm='HS256')
  75. res = {
  76. 'access_token': access_token.decode('utf-8'),
  77. 'access_expire': access_expire,
  78. 'refresh_expire': refresh_expire,
  79. 'refresh_token': refresh_token.decode('utf-8'),
  80. }
  81. except Exception as e:
  82. self.code = 309
  83. print(repr(e))
  84. else:
  85. self.code = 0
  86. return res
  87. def refresh(self):
  88. try:
  89. res = jwt.decode(self.token, OAUTH_REFRESH_TOKEN_SECRET, algorithms='HS256')
  90. except jwt.ExpiredSignatureError as e:
  91. print('过期')
  92. print(repr(e))
  93. self.code = 309
  94. except Exception as e:
  95. self.code = 309
  96. print(repr(e))
  97. else:
  98. self.code = 0
  99. userID = res.get('userID', '')
  100. lang = res.get('lang', '')
  101. user = res.get('user', '')
  102. refreshRes = self.generate(data={'userID': userID, 'lang': lang, 'user': user})
  103. return refreshRes