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