Skip to content

Commit b39ecf6

Browse files
authored
feat: Separate dialogue authentication processor and system authentication processor (#4552)
1 parent 283b6a9 commit b39ecf6

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

apps/chat/views/chat.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
TextToSpeechSerializers, OpenAIChatSerializer
2222
from chat.serializers.chat_authentication import AnonymousAuthenticationSerializer, ApplicationProfileSerializer, \
2323
AuthProfileSerializer
24-
from common.auth import TokenAuth
24+
from common.auth import ChatTokenAuth
2525
from common.constants.permission_constants import ChatAuth
2626
from common.exception.app_exception import AppAuthenticationFailed
2727
from common.result import result
@@ -65,7 +65,7 @@ def get(self, request: Request):
6565

6666

6767
class OpenAIView(APIView):
68-
authentication_classes = [TokenAuth]
68+
authentication_classes = [ChatTokenAuth]
6969

7070
@extend_schema(
7171
methods=['POST'],
@@ -108,7 +108,7 @@ def post(self, request: Request):
108108

109109

110110
class ApplicationProfile(APIView):
111-
authentication_classes = [TokenAuth]
111+
authentication_classes = [ChatTokenAuth]
112112

113113
@extend_schema(
114114
methods=['GET'],
@@ -142,7 +142,7 @@ def get(self, request: Request):
142142

143143

144144
class ChatView(APIView):
145-
authentication_classes = [TokenAuth]
145+
authentication_classes = [ChatTokenAuth]
146146

147147
@extend_schema(
148148
methods=['POST'],
@@ -164,7 +164,7 @@ def post(self, request: Request, chat_id: str):
164164

165165

166166
class OpenView(APIView):
167-
authentication_classes = [TokenAuth]
167+
authentication_classes = [ChatTokenAuth]
168168

169169
@extend_schema(
170170
methods=['GET'],
@@ -196,7 +196,7 @@ def get(self, request: Request):
196196

197197

198198
class SpeechToText(APIView):
199-
authentication_classes = [TokenAuth]
199+
authentication_classes = [ChatTokenAuth]
200200

201201
@extend_schema(
202202
methods=['POST'],
@@ -215,7 +215,7 @@ def post(self, request: Request):
215215

216216

217217
class TextToSpeech(APIView):
218-
authentication_classes = [TokenAuth]
218+
authentication_classes = [ChatTokenAuth]
219219

220220
@extend_schema(
221221
methods=['POST'],
@@ -234,7 +234,7 @@ def post(self, request: Request):
234234

235235

236236
class UploadFile(APIView):
237-
authentication_classes = [TokenAuth]
237+
authentication_classes = [ChatTokenAuth]
238238
parser_classes = [MultiPartParser]
239239

240240
@extend_schema(

apps/chat/views/chat_record.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
from chat.serializers.chat_record import VoteSerializer, HistoricalConversationSerializer, \
1919
HistoricalConversationRecordSerializer, HistoricalConversationOperateSerializer
2020
from common import result
21-
from common.auth import TokenAuth
21+
from common.auth import ChatTokenAuth
2222

2323

2424
class VoteView(APIView):
25-
authentication_classes = [TokenAuth]
25+
authentication_classes = [ChatTokenAuth]
2626

2727
@extend_schema(
2828
methods=['PUT'],
@@ -42,7 +42,7 @@ def put(self, request: Request, chat_id: str, chat_record_id: str):
4242

4343

4444
class HistoricalConversationView(APIView):
45-
authentication_classes = [TokenAuth]
45+
authentication_classes = [ChatTokenAuth]
4646

4747
@extend_schema(
4848
methods=['GET'],
@@ -61,7 +61,7 @@ def get(self, request: Request):
6161
}).list())
6262

6363
class Operate(APIView):
64-
authentication_classes = [TokenAuth]
64+
authentication_classes = [ChatTokenAuth]
6565

6666
@extend_schema(
6767
methods=['PUT'],
@@ -100,7 +100,7 @@ def delete(self, request: Request, chat_id: str):
100100
}).logic_delete())
101101

102102
class BatchDelete(APIView):
103-
authentication_classes = [TokenAuth]
103+
authentication_classes = [ChatTokenAuth]
104104

105105
@extend_schema(
106106
methods=['DELETE'],
@@ -118,7 +118,7 @@ def delete(self, request: Request):
118118
}).batch_logic_delete())
119119

120120
class PageView(APIView):
121-
authentication_classes = [TokenAuth]
121+
authentication_classes = [ChatTokenAuth]
122122

123123
@extend_schema(
124124
methods=['GET'],
@@ -138,7 +138,7 @@ def get(self, request: Request, current_page: int, page_size: int):
138138

139139

140140
class HistoricalConversationRecordView(APIView):
141-
authentication_classes = [TokenAuth]
141+
authentication_classes = [ChatTokenAuth]
142142

143143
@extend_schema(
144144
methods=['GET'],
@@ -158,7 +158,7 @@ def get(self, request: Request, chat_id: str):
158158
}).list())
159159

160160
class PageView(APIView):
161-
authentication_classes = [TokenAuth]
161+
authentication_classes = [ChatTokenAuth]
162162

163163
@extend_schema(
164164
methods=['GET'],
@@ -179,7 +179,7 @@ def get(self, request: Request, chat_id: str, current_page: int, page_size: int)
179179

180180

181181
class ChatRecordView(APIView):
182-
authentication_classes = [TokenAuth]
182+
authentication_classes = [ChatTokenAuth]
183183

184184
@extend_schema(
185185
methods=['GET'],

apps/common/auth/authenticate.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def new_instance_by_class_path(class_path: str):
5151

5252

5353
handles = [new_instance_by_class_path(class_path) for class_path in settings.AUTH_HANDLES]
54+
chat_handles = [new_instance_by_class_path(class_path) for class_path in settings.CHAT_AUTH_HANDLES]
5455

5556

5657
class TokenDetails:
@@ -93,3 +94,29 @@ def authenticate(self, request):
9394
AppApiException):
9495
raise e
9596
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
97+
98+
99+
class ChatTokenAuth(TokenAuthentication):
100+
keyword = "Bearer"
101+
102+
# 重新 authenticate 方法,自定义认证规则
103+
def authenticate(self, request):
104+
auth = request.META.get('HTTP_AUTHORIZATION')
105+
# 未认证
106+
if auth is None:
107+
raise AppAuthenticationFailed(1003, _('Not logged in, please log in first'))
108+
if not auth.startswith("Bearer "):
109+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
110+
try:
111+
token = auth[7:]
112+
token_details = TokenDetails(token)
113+
for handle in chat_handles:
114+
if handle.support(request, token, token_details.get_token_details):
115+
return handle.handle(request, token, token_details.get_token_details)
116+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
117+
except Exception as e:
118+
maxkb_logger.error(f'Exception: {e}', exc_info=True)
119+
if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e,
120+
AppApiException):
121+
raise e
122+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))

apps/maxkb/settings/auth/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99

1010
AUTH_HANDLES = [
1111
]
12+
CHAT_AUTH_HANDLES = [
13+
]

apps/maxkb/settings/auth/web.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
CHAT_ANONYMOUS_USER_AURH = 'common.auth.handle.impl.chat_anonymous_user_token.ChatAnonymousUserToken'
1111
APPLICATION_KEY_AUTH = 'common.auth.handle.impl.application_key.ApplicationKey'
1212
AUTH_HANDLES = [
13-
USER_TOKEN_AUTH,
13+
USER_TOKEN_AUTH
14+
]
15+
16+
CHAT_AUTH_HANDLES = [
1417
CHAT_ANONYMOUS_USER_AURH,
1518
APPLICATION_KEY_AUTH
1619
]

0 commit comments

Comments
 (0)