@@ -52,6 +52,7 @@ def new_instance_by_class_path(class_path: str):
5252
5353handles = [new_instance_by_class_path (class_path ) for class_path in settings .AUTH_HANDLES ]
5454chat_handles = [new_instance_by_class_path (class_path ) for class_path in settings .CHAT_AUTH_HANDLES ]
55+ all_handles = handles + chat_handles
5556
5657
5758class 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' ))
0 commit comments