TokenObject.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 == 'local':
  21. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiZW4iLCJleHAiOjE1NzQwNTg2Njh9.tzJLLh78EuW_FZ1oLJ85RSaryVntVY72FBfqZg_G-qc'
  22. if token == 'test':
  23. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsYW5nIjoiZW4iLCJleHAiOjE1NDUwOTQ0MDgsInVzZXJJRCI6IjE1MTU2NDI2MjMzNzkzOTUxMzgwMDEzODAwMSJ9.d7guVGZy7oFh5_Ri5kfeK-C7hNtKTqBlxMrWPR7nnaE'
  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 = 309
  44. except Exception as e:
  45. self.code = 309
  46. else:
  47. return res
  48. def generate(self, data={}):
  49. try:
  50. access_expire = int(OAUTH_ACCESS_TOKEN_TIME.total_seconds())
  51. refresh_expire = int(OAUTH_REFRESH_TOKEN_TIME.total_seconds())
  52. now_stamp = int(time.time())
  53. access_data = data
  54. refresh_data = data
  55. access_data['exp'] = access_expire + now_stamp
  56. refresh_data['exp'] = refresh_expire + now_stamp
  57. access_token = jwt.encode(access_data,
  58. OAUTH_ACCESS_TOKEN_SECRET,
  59. algorithm='HS256')
  60. refresh_token = jwt.encode(
  61. refresh_data,
  62. OAUTH_REFRESH_TOKEN_SECRET,
  63. algorithm='HS256')
  64. res = {
  65. 'access_token': access_token.decode('utf-8'),
  66. 'access_expire': access_expire,
  67. 'refresh_expire': refresh_expire,
  68. 'refresh_token': refresh_token.decode('utf-8'),
  69. }
  70. except Exception as e:
  71. self.code = 309
  72. print(repr(e))
  73. else:
  74. return res
  75. def refresh(self):
  76. try:
  77. res = jwt.decode(self.token, OAUTH_REFRESH_TOKEN_SECRET, algorithms='HS256')
  78. except jwt.ExpiredSignatureError as e:
  79. print('过期')
  80. print(repr(e))
  81. self.code = 309
  82. except Exception as e:
  83. self.code = 309
  84. print(repr(e))
  85. else:
  86. userID = res['userID']
  87. lang = res['lang']
  88. refreshRes = self.generate(data={'userID': userID, 'lang': lang})
  89. return refreshRes