UnisKB/apps/common/auth/common.py

88 lines
2.9 KiB
Python
Raw Normal View History

2025-06-06 14:28:21 +00:00
# coding=utf-8
"""
@project: MaxKB
@Author虎虎
@file common.py
@date2025/6/6 19:55
@desc:
"""
2025-08-29 03:54:16 +00:00
import hashlib
2025-06-06 14:28:21 +00:00
import json
2025-08-29 03:54:16 +00:00
import threading
2025-06-06 14:28:21 +00:00
2025-08-29 03:54:16 +00:00
from django.core import signing, cache
2025-06-06 14:28:21 +00:00
2025-08-29 03:54:16 +00:00
from common.constants.cache_version import Cache_Version
2025-06-06 14:28:21 +00:00
from common.utils.rsa_util import encrypt, decrypt
2025-08-29 03:54:16 +00:00
authentication_cache = cache.cache
lock = threading.Lock()
def _decrypt(authentication: str):
cache_key = hashlib.sha256(authentication.encode()).hexdigest()
result = authentication_cache.get(key=cache_key, version=Cache_Version.CHAT.value)
if result is None:
with lock:
result = authentication_cache.get(cache_key, version=Cache_Version.CHAT.value)
if result is None:
result = decrypt(authentication)
authentication_cache.set(cache_key, result, version=Cache_Version.CHAT.value, timeout=60 * 60 * 2)
return result
2025-06-06 14:28:21 +00:00
class ChatAuthentication:
2025-08-29 03:54:16 +00:00
def __init__(self, auth_type: str | None, **kwargs):
2025-06-06 14:28:21 +00:00
self.auth_type = auth_type
2025-08-29 03:54:16 +00:00
for k, v in kwargs.items():
self.__setattr__(k, v)
2025-06-06 14:28:21 +00:00
def to_dict(self):
2025-08-29 03:54:16 +00:00
return self.__dict__
2025-06-06 14:28:21 +00:00
def to_string(self):
2025-08-29 03:54:16 +00:00
value = json.dumps(self.to_dict())
authentication = encrypt(value)
cache_key = hashlib.sha256(authentication.encode()).hexdigest()
2025-11-14 10:40:05 +00:00
authentication_cache.set(cache_key, value, version=Cache_Version.CHAT.get_version(), timeout=60 * 60 * 2)
2025-08-29 03:54:16 +00:00
return authentication
2025-06-06 14:28:21 +00:00
@staticmethod
def new_instance(authentication: str):
2025-08-29 03:54:16 +00:00
auth = json.loads(_decrypt(authentication))
return ChatAuthentication(**auth)
2025-06-06 14:28:21 +00:00
class ChatUserToken:
2025-06-09 08:18:43 +00:00
def __init__(self, application_id, user_id, access_token, _type, chat_user_type, chat_user_id,
2025-06-06 14:28:21 +00:00
authentication: ChatAuthentication):
self.application_id = application_id
2025-06-17 12:08:39 +00:00
self.user_id = user_id
2025-06-06 14:28:21 +00:00
self.access_token = access_token
self.type = _type
2025-06-09 08:18:43 +00:00
self.chat_user_type = chat_user_type
self.chat_user_id = chat_user_id
2025-06-06 14:28:21 +00:00
self.authentication = authentication
def to_dict(self):
return {
'application_id': str(self.application_id),
'user_id': str(self.user_id),
'access_token': self.access_token,
'type': str(self.type.value),
2025-06-09 08:18:43 +00:00
'chat_user_type': str(self.chat_user_type),
'chat_user_id': str(self.chat_user_id),
2025-06-06 14:28:21 +00:00
'authentication': self.authentication.to_string()
}
def to_token(self):
return signing.dumps(self.to_dict())
@staticmethod
def new_instance(token_dict):
return ChatUserToken(token_dict.get('application_id'), token_dict.get('user_id'),
2025-06-09 08:18:43 +00:00
token_dict.get('access_token'), token_dict.get('type'), token_dict.get('chat_user_type'),
token_dict.get('chat_user_id'),
2025-06-06 14:28:21 +00:00
ChatAuthentication.new_instance(token_dict.get('authentication')))