TokenObject1.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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
  15. import jwt
  16. from Model.models import oauth_user
  17. class TokenObject:
  18. def __init__(self, token=None):
  19. if token == 'debug':
  20. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvaWQiOiJhNDVmN2U3MC04NTAwLTRkOTItYWRkZS1mMmMxMDdkYzVkYzEiLCJsYW5nIjpudWxsLCJleHAiOjE1NzA5ODY4Mzl9.uDLfBjUQqWQB0pOMtXN17iikEbxztDhcus_UCH36XZw'
  21. if token == 'test':
  22. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvaWQiOiJhNDVmN2U3MC04NTAwLTRkOTItYWRkZS1mMmMxMDdkYzVkYzEiLCJsYW5nIjpudWxsLCJleHAiOjE1MzUwNzQ0Njl9.MW7iU_LMo_uqceYeoePVZiyJoYc1wZTJTxpxOa0aha8'
  23. self.token = token
  24. self.code = 0
  25. self.userID = None
  26. self.lang = None
  27. def valid(self):
  28. try:
  29. res = jwt.decode(self.token, OAUTH_ACCESS_TOKEN_SECRET, algorithms='HS256')
  30. self.userID = res.get('userID', None)
  31. self.lang = res.get('lang', None)
  32. except jwt.ExpiredSignatureError as e:
  33. print('过期')
  34. print(repr(e))
  35. self.code = 307
  36. except Exception as e:
  37. print(repr(e))
  38. self.code = 303
  39. def userID(self):
  40. oid = self.oid
  41. if oid is not None:
  42. oauth_user_qs = oauth_user.objects.filter(oid=oid)
  43. if oauth_user_qs.exists():
  44. return oauth_user_qs[0].userID.userID
  45. return None