Skip to content

Commit 23ad188

Browse files
authored
fix: The conversation page cannot upload files (#4611)
1 parent 1da8315 commit 23ad188

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

apps/common/auth/authenticate.py

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

5353
handles = [new_instance_by_class_path(class_path) for class_path in settings.AUTH_HANDLES]
5454
chat_handles = [new_instance_by_class_path(class_path) for class_path in settings.CHAT_AUTH_HANDLES]
55+
all_handles = handles + chat_handles
5556

5657

5758
class TokenDetails:
@@ -120,3 +121,29 @@ def authenticate(self, request):
120121
AppApiException):
121122
raise e
122123
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
124+
125+
126+
class AllTokenAuth(TokenAuthentication):
127+
keyword = "Bearer"
128+
129+
# 重新 authenticate 方法,自定义认证规则
130+
def authenticate(self, request):
131+
auth = request.META.get('HTTP_AUTHORIZATION')
132+
# 未认证
133+
if auth is None:
134+
raise AppAuthenticationFailed(1003, _('Not logged in, please log in first'))
135+
if not auth.startswith("Bearer "):
136+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
137+
try:
138+
token = auth[7:]
139+
token_details = TokenDetails(token)
140+
for handle in all_handles:
141+
if handle.support(request, token, token_details.get_token_details):
142+
return handle.handle(request, token, token_details.get_token_details)
143+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
144+
except Exception as e:
145+
maxkb_logger.error(f'Exception: {e}', exc_info=True)
146+
if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e,
147+
AppApiException):
148+
raise e
149+
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))

apps/oss/views/file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from rest_framework.parsers import MultiPartParser
55
from rest_framework.views import APIView
66
from rest_framework.views import Request
7-
from common.auth import TokenAuth
7+
from common.auth import TokenAuth, AllTokenAuth
88
from common.log.log import log
99
from common.result import result
1010
from knowledge.api.file import FileUploadAPI, FileGetAPI
@@ -29,7 +29,7 @@ def get(self, request: Request, file_id: str):
2929

3030

3131
class FileView(APIView):
32-
authentication_classes = [TokenAuth]
32+
authentication_classes = [AllTokenAuth]
3333
parser_classes = [MultiPartParser]
3434

3535
@extend_schema(
@@ -80,4 +80,4 @@ class GetUrlView(APIView):
8080
def get(self, request: Request, application_id: str):
8181
url = request.query_params.get('url')
8282
result_data = get_url_content(url, application_id)
83-
return result.success(result_data)
83+
return result.success(result_data)

0 commit comments

Comments
 (0)