13
13
from django .core import cache
14
14
from django .core import signing
15
15
from django .utils .translation import gettext_lazy as _
16
+ from drf_spectacular .extensions import OpenApiAuthenticationExtension
16
17
from rest_framework .authentication import TokenAuthentication
17
18
18
19
from common .exception .app_exception import AppAuthenticationFailed , AppEmbedIdentityFailed , AppChatNumOutOfBoundsFailed , \
@@ -26,6 +27,20 @@ def authenticate(self, request):
26
27
return None , None
27
28
28
29
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
+
29
44
def new_instance_by_class_path (class_path : str ):
30
45
parts = class_path .rpartition ('.' )
31
46
package_path = parts [0 ]
@@ -54,39 +69,23 @@ def get_token_details(self):
54
69
return self .token_details
55
70
56
71
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
-
78
72
class TokenAuth (TokenAuthentication ):
73
+ keyword = "Bearer"
74
+
79
75
# 重新 authenticate 方法,自定义认证规则
80
76
def authenticate (self , request ):
81
77
auth = request .META .get ('HTTP_AUTHORIZATION' )
82
78
# 未认证
83
79
if auth is None :
84
80
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' ))
85
83
try :
86
- token_details = TokenDetails (auth )
84
+ token = auth [7 :]
85
+ token_details = TokenDetails (token )
87
86
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 )
90
89
raise AppAuthenticationFailed (1002 , _ ('Authentication information is incorrect! illegal user' ))
91
90
except Exception as e :
92
91
traceback .format_exc ()
0 commit comments