TokenObject.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. redisObj = RedisObject(db=3)
  44. redisObj.set_data(key=self.userID, val=self.user, expire=300)
  45. except jwt.ExpiredSignatureError as e:
  46. print('过期')
  47. print(repr(e))
  48. self.code = 309
  49. except Exception as e:
  50. self.code = 309
  51. else:
  52. self.code = 0
  53. if not self.userID:
  54. self.code = 309
  55. return res
  56. def generate(self, data={}):
  57. try:
  58. access_expire = int(OAUTH_ACCESS_TOKEN_TIME.total_seconds())
  59. refresh_expire = int(OAUTH_REFRESH_TOKEN_TIME.total_seconds())
  60. now_stamp = int(time.time())
  61. access_data = data
  62. refresh_data = data
  63. access_data['exp'] = access_expire + now_stamp
  64. refresh_data['exp'] = refresh_expire + now_stamp
  65. access_token = jwt.encode(access_data,
  66. OAUTH_ACCESS_TOKEN_SECRET,
  67. algorithm='HS256')
  68. refresh_token = jwt.encode(
  69. refresh_data,
  70. OAUTH_REFRESH_TOKEN_SECRET,
  71. algorithm='HS256')
  72. res = {
  73. 'access_token': access_token.decode('utf-8'),
  74. 'access_expire': access_expire,
  75. 'refresh_expire': refresh_expire,
  76. 'refresh_token': refresh_token.decode('utf-8'),
  77. }
  78. except Exception as e:
  79. self.code = 309
  80. print(repr(e))
  81. else:
  82. self.code = 0
  83. return res
  84. def refresh(self):
  85. try:
  86. res = jwt.decode(self.token, OAUTH_REFRESH_TOKEN_SECRET, algorithms='HS256')
  87. except jwt.ExpiredSignatureError as e:
  88. print('过期')
  89. print(repr(e))
  90. self.code = 309
  91. except Exception as e:
  92. self.code = 309
  93. print(repr(e))
  94. else:
  95. self.code = 0
  96. userID = res.get('userID', '')
  97. lang = res.get('lang', '')
  98. user = res.get('user', '')
  99. refreshRes = self.generate(data={'userID': userID, 'lang': lang, 'user': user})
  100. return refreshRes