1313from django .core import cache
1414from django .core import signing
1515from django .utils .translation import gettext_lazy as _
16+ from drf_spectacular .extensions import OpenApiAuthenticationExtension
1617from rest_framework .authentication import TokenAuthentication
1718
1819from common .exception .app_exception import AppAuthenticationFailed , AppEmbedIdentityFailed , AppChatNumOutOfBoundsFailed , \
@@ -26,6 +27,20 @@ def authenticate(self, request):
2627 return None , None
2728
2829
30+ class AnonymousAuthenticationScheme (OpenApiAuthenticationExtension ):
31+ target_class = AnonymousAuthentication # 绑定到你的自定义认证类
32+ name = "AnonymousAuth" # 自定义认证名称(显示在 Swagger UI 中)
33+
34+ def get_security_definition (self , auto_schema ):
35+ # 定义认证方式,这里假设匿名认证不需要凭证
36+ return {
37+ }
38+
39+ def get_security_requirement (self , auto_schema ):
40+ # 返回安全要求(空字典表示无需认证)
41+ return {}
42+
43+
2944def new_instance_by_class_path (class_path : str ):
3045 parts = class_path .rpartition ('.' )
3146 package_path = parts [0 ]
@@ -54,39 +69,23 @@ def get_token_details(self):
5469 return self .token_details
5570
5671
57- class OpenAIKeyAuth (TokenAuthentication ):
58- def authenticate (self , request ):
59- auth = request .META .get ('HTTP_AUTHORIZATION' )
60- auth = auth .replace ('Bearer ' , '' )
61- # 未认证
62- if auth is None :
63- raise AppAuthenticationFailed (1003 , _ ('Not logged in, please log in first' ))
64- try :
65- token_details = TokenDetails (auth )
66- for handle in handles :
67- if handle .support (request , auth , token_details .get_token_details ):
68- return handle .handle (request , auth , token_details .get_token_details )
69- raise AppAuthenticationFailed (1002 , _ ('Authentication information is incorrect! illegal user' ))
70- except Exception as e :
71- traceback .format_exc ()
72- if isinstance (e , AppEmbedIdentityFailed ) or isinstance (e , AppChatNumOutOfBoundsFailed ) or isinstance (e ,
73- AppApiException ):
74- raise e
75- raise AppAuthenticationFailed (1002 , _ ('Authentication information is incorrect! illegal user' ))
76-
77-
7872class TokenAuth (TokenAuthentication ):
73+ keyword = "Bearer"
74+
7975 # 重新 authenticate 方法,自定义认证规则
8076 def authenticate (self , request ):
8177 auth = request .META .get ('HTTP_AUTHORIZATION' )
8278 # 未认证
8379 if auth is None :
8480 raise AppAuthenticationFailed (1003 , _ ('Not logged in, please log in first' ))
81+ if not auth .startswith ("Bearer " ):
82+ raise AppAuthenticationFailed (1002 , _ ('Authentication information is incorrect! illegal user' ))
8583 try :
86- token_details = TokenDetails (auth )
84+ token = auth [7 :]
85+ token_details = TokenDetails (token )
8786 for handle in handles :
88- if handle .support (request , auth , token_details .get_token_details ):
89- return handle .handle (request , auth , token_details .get_token_details )
87+ if handle .support (request , token , token_details .get_token_details ):
88+ return handle .handle (request , token , token_details .get_token_details )
9089 raise AppAuthenticationFailed (1002 , _ ('Authentication information is incorrect! illegal user' ))
9190 except Exception as e :
9291 traceback .format_exc ()
0 commit comments