common.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎虎
  5. @file: common.py
  6. @date:2025/6/6 19:55
  7. @desc:
  8. """
  9. import hashlib
  10. import json
  11. import threading
  12. from django.core import signing, cache
  13. from common.constants.cache_version import Cache_Version
  14. from common.utils.rsa_util import encrypt, decrypt
  15. authentication_cache = cache.cache
  16. lock = threading.Lock()
  17. def _decrypt(authentication: str):
  18. cache_key = hashlib.sha256(authentication.encode()).hexdigest()
  19. result = authentication_cache.get(key=cache_key, version=Cache_Version.CHAT.value)
  20. if result is None:
  21. with lock:
  22. result = authentication_cache.get(cache_key, version=Cache_Version.CHAT.value)
  23. if result is None:
  24. result = decrypt(authentication)
  25. authentication_cache.set(cache_key, result, version=Cache_Version.CHAT.value, timeout=60 * 60 * 2)
  26. return result
  27. class ChatAuthentication:
  28. def __init__(self, auth_type: str | None, **kwargs):
  29. self.auth_type = auth_type
  30. for k, v in kwargs.items():
  31. self.__setattr__(k, v)
  32. def to_dict(self):
  33. return self.__dict__
  34. def to_string(self):
  35. value = json.dumps(self.to_dict())
  36. authentication = encrypt(value)
  37. cache_key = hashlib.sha256(authentication.encode()).hexdigest()
  38. authentication_cache.set(cache_key, value, version=Cache_Version.CHAT.get_version(), timeout=60 * 60 * 2)
  39. return authentication
  40. @staticmethod
  41. def new_instance(authentication: str):
  42. auth = json.loads(_decrypt(authentication))
  43. return ChatAuthentication(**auth)
  44. class ChatUserToken:
  45. def __init__(self, application_id, user_id, access_token, _type, chat_user_type, chat_user_id,
  46. authentication: ChatAuthentication):
  47. self.application_id = application_id
  48. self.user_id = user_id
  49. self.access_token = access_token
  50. self.type = _type
  51. self.chat_user_type = chat_user_type
  52. self.chat_user_id = chat_user_id
  53. self.authentication = authentication
  54. def to_dict(self):
  55. return {
  56. 'application_id': str(self.application_id),
  57. 'user_id': str(self.user_id),
  58. 'access_token': self.access_token,
  59. 'type': str(self.type.value),
  60. 'chat_user_type': str(self.chat_user_type),
  61. 'chat_user_id': str(self.chat_user_id),
  62. 'authentication': self.authentication.to_string()
  63. }
  64. def to_token(self):
  65. return signing.dumps(self.to_dict())
  66. @staticmethod
  67. def new_instance(token_dict):
  68. return ChatUserToken(token_dict.get('application_id'), token_dict.get('user_id'),
  69. token_dict.get('access_token'), token_dict.get('type'), token_dict.get('chat_user_type'),
  70. token_dict.get('chat_user_id'),
  71. ChatAuthentication.new_instance(token_dict.get('authentication')))