|
7 | 7 | @desc: |
8 | 8 | """ |
9 | 9 |
|
10 | | -import hashlib |
11 | | -import json |
12 | | -import threading |
| 10 | +from django.core import signing |
13 | 11 |
|
14 | | -from django.core import signing, cache |
15 | | - |
16 | | -from application.models import ChatUserType |
17 | 12 | from common.constants.authentication_type import AuthenticationType |
18 | | -from common.constants.cache_version import Cache_Version |
19 | | -from common.utils.rsa_util import encrypt, decrypt |
20 | | - |
21 | | -authentication_cache = cache.cache |
22 | | -lock = threading.Lock() |
23 | | - |
24 | | - |
25 | | -def _decrypt(authentication: str): |
26 | | - cache_key = hashlib.sha256(authentication.encode()).hexdigest() |
27 | | - result = authentication_cache.get(key=cache_key, version=Cache_Version.CHAT.value) |
28 | | - if result is None: |
29 | | - with lock: |
30 | | - result = authentication_cache.get(cache_key, version=Cache_Version.CHAT.value) |
31 | | - if result is None: |
32 | | - result = decrypt(authentication) |
33 | | - authentication_cache.set(cache_key, result, version=Cache_Version.CHAT.value, timeout=60 * 60 * 2) |
34 | | - |
35 | | - return result |
36 | | - |
37 | | - |
38 | | -class ChatAuthentication: |
39 | | - def __init__(self, auth_type: str | None, **kwargs): |
40 | | - self.auth_type = auth_type |
41 | | - for k, v in kwargs.items(): |
42 | | - self.__setattr__(k, v) |
43 | | - |
44 | | - def to_dict(self): |
45 | | - return self.__dict__ |
46 | | - |
47 | | - def to_string(self): |
48 | | - value = json.dumps(self.to_dict()) |
49 | | - authentication = encrypt(value) |
50 | | - cache_key = hashlib.sha256(authentication.encode()).hexdigest() |
51 | | - authentication_cache.set(cache_key, value, version=Cache_Version.CHAT.get_version(), timeout=60 * 60 * 2) |
52 | | - return authentication |
53 | | - |
54 | | - @staticmethod |
55 | | - def new_instance(authentication: str): |
56 | | - auth = json.loads(_decrypt(authentication)) |
57 | | - return ChatAuthentication(**auth) |
| 13 | +from common.exception.app_exception import AppAuthenticationFailed |
58 | 14 |
|
59 | 15 |
|
60 | | -class FileToken: |
61 | | - def __init__(self, user_id, _type, application_id: str = None): |
62 | | - self.user_id = user_id |
| 16 | +class SystemToken: |
| 17 | + def __init__(self, user_id, _type: AuthenticationType, **kwargs): |
| 18 | + self.id = user_id |
63 | 19 | self.type = _type |
64 | | - self.application_id = application_id |
65 | | - |
66 | | - def to_dict(self): |
67 | | - return ( |
68 | | - {"user_id": self.user_id, "type": str(self.type), "application_id": self.application_id} |
69 | | - if self.application_id |
70 | | - else {"user_id": self.user_id, "type": str(self.type)} |
71 | | - ) |
72 | | - |
73 | | - def to_token(self): |
74 | | - return signing.dumps(self.to_dict()) |
75 | | - |
76 | | - @staticmethod |
77 | | - def new_instance(token): |
78 | | - token_dict = signing.loads(token) |
79 | | - return FileToken(token_dict.get("user_id"), token_dict.get("type"), token_dict.get("application_id")) |
80 | | - |
81 | | - |
82 | | -class ChatUserToken: |
83 | | - def __init__( |
84 | | - self, |
85 | | - application_id, |
86 | | - user_id, |
87 | | - access_token, |
88 | | - _type, |
89 | | - chat_user_type, |
90 | | - chat_user_id, |
91 | | - authentication: ChatAuthentication, |
92 | | - ): |
93 | | - self.application_id = application_id |
94 | | - self.user_id = user_id |
95 | | - self.access_token = access_token |
96 | | - self.type = _type |
97 | | - self.chat_user_type = chat_user_type |
98 | | - self.chat_user_id = chat_user_id |
99 | | - self.authentication = authentication |
| 20 | + self.kwargs = kwargs |
100 | 21 |
|
101 | 22 | def to_dict(self): |
102 | | - return { |
103 | | - "application_id": str(self.application_id), |
104 | | - "user_id": str(self.user_id), |
105 | | - "access_token": self.access_token, |
106 | | - "type": str(self.type.value), |
107 | | - "chat_user_type": str(self.chat_user_type), |
108 | | - "chat_user_id": str(self.chat_user_id), |
109 | | - "authentication": self.authentication.to_string(), |
110 | | - } |
| 23 | + if self.kwargs: |
| 24 | + return {"user_id": self.id, "type": str(self.type.value), "kwargs": self.kwargs} |
| 25 | + return {"id": str(self.id), "type": str(self.type.value)} |
111 | 26 |
|
112 | 27 | def to_token(self): |
113 | 28 | return signing.dumps(self.to_dict()) |
114 | 29 |
|
115 | | - @staticmethod |
116 | | - def new_instance(token_dict): |
117 | | - return ChatUserToken( |
118 | | - token_dict.get("application_id"), |
119 | | - token_dict.get("user_id"), |
120 | | - token_dict.get("access_token"), |
121 | | - token_dict.get("type"), |
122 | | - token_dict.get("chat_user_type"), |
123 | | - token_dict.get("chat_user_id"), |
124 | | - ChatAuthentication.new_instance(token_dict.get("authentication")), |
125 | | - ) |
126 | | - |
127 | 30 |
|
128 | 31 | class ChatToken: |
129 | 32 | def __init__(self, user_id, _type: AuthenticationType, login_type: str, **kwargs): |
130 | | - self.user_id = user_id |
| 33 | + self.id = user_id |
131 | 34 | self.type = _type |
132 | 35 | self.login_type = login_type |
133 | 36 | self.kwargs = kwargs |
134 | 37 |
|
135 | 38 | def to_dict(self): |
| 39 | + if self.kwargs: |
| 40 | + return { |
| 41 | + "id": str(self.id), |
| 42 | + "type": str(self.type.value), |
| 43 | + "login_type": str(self.login_type), |
| 44 | + "kwargs": self.kwargs, |
| 45 | + } |
136 | 46 | return { |
137 | | - "user_id": str(self.user_id), |
| 47 | + "id": str(self.id), |
138 | 48 | "type": str(self.type.value), |
139 | 49 | "login_type": str(self.login_type), |
140 | | - "kwargs": self.kwargs, |
141 | 50 | } |
142 | 51 |
|
143 | 52 | def to_token(self): |
144 | 53 | return signing.dumps(self.to_dict()) |
| 54 | + |
| 55 | + |
| 56 | +def parse_token(token): |
| 57 | + details = signing.loads(token) |
| 58 | + _type = details.get("type") |
| 59 | + if _type: |
| 60 | + if _type == AuthenticationType.SYSTEM_USER.value: |
| 61 | + return SystemToken(details.get("id"), details.get("type"), **details.get("kwargs", {})) |
| 62 | + return ChatToken(details.get("id"), details.get("type"), details.get("login_type"), **details.get("kwargs", {})) |
| 63 | + raise AppAuthenticationFailed(1001, "") |
0 commit comments