88from apps .system .models .system_model import AssistantModel
99from common .core .db import engine
1010from apps .system .crud .assistant import get_assistant_info , get_assistant_user
11- from apps .system .crud .user import get_user_info
11+ from apps .system .crud .user import get_user_by_account , get_user_info
1212from apps .system .schemas .system_schema import AssistantHeader , UserInfoDTO
1313from common .core import security
1414from common .core .config import settings
@@ -34,7 +34,7 @@ async def dispatch(self, request, call_next):
3434 trans = await get_i18n (request )
3535 #if assistantToken and assistantToken.lower().startswith("assistant "):
3636 if assistantToken :
37- validator : tuple [any ] = await self .validateAssistant (assistantToken )
37+ validator : tuple [any ] = await self .validateAssistant (assistantToken , trans )
3838 if validator [0 ]:
3939 request .state .current_user = validator [1 ]
4040 request .state .assistant = validator [2 ]
@@ -87,14 +87,17 @@ async def validateToken(self, token: Optional[str], trans: I18n):
8787 return False , e
8888
8989
90- async def validateAssistant (self , assistantToken : Optional [str ]) -> tuple [any ]:
90+ async def validateAssistant (self , assistantToken : Optional [str ], trans : I18n ) -> tuple [any ]:
9191 if not assistantToken :
9292 return False , f"Miss Token[{ settings .TOKEN_KEY } ]!"
9393 schema , param = get_authorization_scheme_param (assistantToken )
94- if schema .lower () != "assistant" :
95- return False , f"Token schema error!"
9694
97- try :
95+
96+ try :
97+ if schema .lower () == 'embedded' :
98+ return await self .validateEmbedded (param , trans )
99+ if schema .lower () != "assistant" :
100+ return False , f"Token schema error!"
98101 payload = jwt .decode (
99102 param , settings .SECRET_KEY , algorithms = [security .ALGORITHM ]
100103 )
@@ -112,4 +115,45 @@ async def validateAssistant(self, assistantToken: Optional[str]) -> tuple[any]:
112115 except Exception as e :
113116 SQLBotLogUtil .exception (f"Assistant validation error: { str (e )} " )
114117 # Return False and the exception message
118+ return False , e
119+
120+ async def validateEmbedded (self , param : str , trans : I18n ) -> tuple [any ]:
121+ try :
122+ """ payload = jwt.decode(
123+ param, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
124+ ) """
125+ payload : dict = jwt .decode (
126+ param ,
127+ options = {"verify_signature" : False , "verify_exp" : False },
128+ algorithms = [security .ALGORITHM ]
129+ )
130+ if not payload ['embeddedId' ]:
131+ return False , f"Miss embeddedId payload error!"
132+ if not payload ['account' ]:
133+ return False , f"Miss account payload error!"
134+ embeddedId = payload ['embeddedId' ]
135+ account = payload ['account' ]
136+ with Session (engine ) as session :
137+ """ session_user = await get_user_info(session = session, user_id = token_data.id)
138+ session_user = UserInfoDTO.model_validate(session_user) """
139+ session_user = get_user_by_account (session = session , account = account )
140+ if not session_user :
141+ message = trans ('i18n_not_exist' , msg = trans ('i18n_user.account' ))
142+ raise Exception (message )
143+ session_user = await get_user_info (session = session , user_id = session_user .id )
144+
145+ session_user = UserInfoDTO .model_validate (session_user )
146+ if session_user .status != 1 :
147+ message = trans ('i18n_login.user_disable' , msg = trans ('i18n_concat_admin' ))
148+ raise Exception (message )
149+ if not session_user .oid or session_user .oid == 0 :
150+ message = trans ('i18n_login.no_associated_ws' , msg = trans ('i18n_concat_admin' ))
151+ raise Exception (message )
152+ assistant_info = await get_assistant_info (session = session , assistant_id = embeddedId )
153+ assistant_info = AssistantModel .model_validate (assistant_info )
154+ assistant_info = AssistantHeader .model_validate (assistant_info .model_dump (exclude_unset = True ))
155+ return True , session_user , assistant_info
156+ except Exception as e :
157+ SQLBotLogUtil .exception (f"Embedded validation error: { str (e )} " )
158+ # Return False and the exception message
115159 return False , e
0 commit comments