From 207c7735f4d10a34d643860da08cfafc5e37194b Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 17 Nov 2026 22:24:21 +0900 Subject: [PATCH] refac --- backend/open_webui/routers/tools.py | 20 ++++++++++++++++++++ src/lib/components/chat/Chat.svelte | 21 ++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/routers/tools.py b/backend/open_webui/routers/tools.py index e22fae187b..0144802a5d 100644 --- a/backend/open_webui/routers/tools.py +++ b/backend/open_webui/routers/tools.py @@ -43,6 +43,7 @@ from open_webui.utils.auth import get_admin_user, get_verified_user from open_webui.utils.mcp.apps import app_csp, ui_resource_uri, verify_app_binding from open_webui.utils.mcp.client import tool_visible_to from open_webui.utils.misc import is_string_allowed +from open_webui.utils.oauth import get_system_oauth_token from open_webui.utils.plugin import ( get_tool_contents_cache, get_tool_module_from_cache, @@ -116,6 +117,23 @@ async def get_tools( ) ) + # Tool servers that forward the user's SSO token fail once that SSO session is gone. + # Only flag them for browsers signed in through SSO, where signing in again fixes it. + oauth_session_expired = ( + ENABLE_TOOL_SERVERS + and 'oauth_session_id' in request.cookies + and any( + connection.get('auth_type') == 'system_oauth' and (connection.get('config') or {}).get('enable') + for connection in await Config.get('tool_server.connections', []) + ) + and await get_system_oauth_token(request, user) is None + ) + + def get_system_oauth_status(auth_type: str) -> dict: + if auth_type == 'system_oauth' and oauth_session_expired: + return {'authenticated': False, 'auth_type': 'system_oauth'} + return {} + # OpenAPI Tool Servers server_connections = {} for server in await get_tool_servers(request): @@ -151,6 +169,7 @@ async def get_tools( ], 'updated_at': int(time.time()), 'created_at': int(time.time()), + **get_system_oauth_status(connection.get('auth_type')), } ) ) @@ -192,6 +211,7 @@ async def get_tools( if auth_type in ('oauth_2.1', 'oauth_2.1_static') else {} ), + **get_system_oauth_status(auth_type), } ) ) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index f8050e18e6..5cacdf7082 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -1167,6 +1167,11 @@ console.log('saveSessionSelectedModels', selectedModels, sessionStorage.selectedModels); }; + // The backend only marks a tool as system_oauth when the user's SSO session is gone, so + // signing in again is the fix. These tools never block sending or redirect on their own. + const isSystemOAuthTool = (toolId: string) => + ($tools ?? []).find((tool) => tool.id === toolId)?.auth_type === 'system_oauth'; + const continueOAuthRedirect = async () => { if (tabbed) { pendingOAuthTools = []; @@ -1181,7 +1186,11 @@ return; } - const nextTool = pendingOAuthTools[0]; + const nextTool = pendingOAuthTools.find((tool) => !isSystemOAuthTool(tool.id)); + if (!nextTool) { + sessionStorage.removeItem('oauthRedirectInProgressToolId'); + return; + } if (sessionStorage.getItem('oauthRedirectInProgressToolId') === nextTool.id) { sessionStorage.removeItem('oauthRedirectInProgressToolId'); return; @@ -3195,7 +3204,7 @@ const pendingToolId = sessionStorage.getItem('pendingOAuthToolId'); if (pendingToolId) { sessionStorage.removeItem('pendingOAuthToolId'); - if (!selectedToolIds.includes(pendingToolId)) { + if (!selectedToolIds.includes(pendingToolId) && !isSystemOAuthTool(pendingToolId)) { selectedToolIds = [...selectedToolIds, pendingToolId]; } } @@ -4612,7 +4621,7 @@ return { status: 'rejected' }; } - if (pendingOAuthTools.length > 0) { + if (pendingOAuthTools.some((tool) => !isSystemOAuthTool(tool.id))) { toast.warning($i18n.t('Please connect all required integrations before sending a message')); return { status: 'rejected' }; } @@ -5705,6 +5714,12 @@ await tick(); saveSessionSelectedModels(); await saveDraft(draft, null, false); + if (isSystemOAuthTool(tool.id)) { + sessionStorage.setItem('pendingOAuthToolId', tool.id); + localStorage.removeItem('token'); + window.location.href = '/auth?state=logout&form=signin'; + return; + } initiateOAuthRedirect(tool); };