Skip to content

Commit c767bca

Browse files
committed
refac
1 parent 2943955 commit c767bca

3 files changed

Lines changed: 39 additions & 15 deletions

File tree

backend/open_webui/main.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@
557557
get_oauth_client_info_with_static_credentials,
558558
encrypt_data,
559559
decrypt_data,
560+
resolve_oauth_client_info,
560561
OAuthManager,
561562
OAuthClientManager,
562563
OAuthClientInformationFull,
@@ -2304,10 +2305,8 @@ async def get_current_usage(user=Depends(get_verified_user)):
23042305
auth_type = tool_server_connection.get('auth_type', 'none')
23052306

23062307
if server_id and auth_type in ('oauth_2.1', 'oauth_2.1_static'):
2307-
oauth_client_info = tool_server_connection.get('info', {}).get('oauth_client_info', '')
2308-
23092308
try:
2310-
oauth_client_info = decrypt_data(oauth_client_info)
2309+
oauth_client_info = resolve_oauth_client_info(tool_server_connection)
23112310
app.state.oauth_client_manager.add_client(
23122311
f'mcp:{server_id}',
23132312
OAuthClientInformationFull(**oauth_client_info),
@@ -2368,18 +2367,25 @@ async def register_client(request, client_id: str) -> bool:
23682367

23692368
try:
23702369
if auth_type == 'oauth_2.1_static':
2371-
# Static credentials: rebuild from stored credentials + fresh metadata
2372-
existing_client_info = connection.get('info', {}).get('oauth_client_info', '')
2373-
if not existing_client_info:
2374-
log.error(f'No stored OAuth client info for static client {client_id}')
2375-
return False
2376-
existing_data = decrypt_data(existing_client_info)
2370+
# Static credentials: rebuild from admin-provided credentials + fresh metadata
2371+
info = connection.get('info', {})
2372+
oauth_client_id = info.get('oauth_client_id') or ''
2373+
oauth_client_secret = info.get('oauth_client_secret') or ''
2374+
if not oauth_client_id or not oauth_client_secret:
2375+
# Fall back to blob for backward compatibility
2376+
existing_client_info = info.get('oauth_client_info', '')
2377+
if not existing_client_info:
2378+
log.error(f'No stored OAuth client info for static client {client_id}')
2379+
return False
2380+
existing_data = decrypt_data(existing_client_info)
2381+
oauth_client_id = oauth_client_id or existing_data.get('client_id', '')
2382+
oauth_client_secret = oauth_client_secret or existing_data.get('client_secret', '')
23772383
oauth_client_info = await get_oauth_client_info_with_static_credentials(
23782384
request,
23792385
client_id,
23802386
server_url,
2381-
oauth_client_id=existing_data.get('client_id', ''),
2382-
oauth_client_secret=existing_data.get('client_secret', ''),
2387+
oauth_client_id=oauth_client_id,
2388+
oauth_client_secret=oauth_client_secret,
23832389
)
23842390
else:
23852391
oauth_client_info = await get_oauth_client_info_with_dynamic_client_registration(

backend/open_webui/routers/configs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
get_oauth_client_info_with_static_credentials,
2828
encrypt_data,
2929
decrypt_data,
30+
resolve_oauth_client_info,
3031
OAuthClientInformationFull,
3132
)
3233
from mcp.shared.auth import OAuthMetadata
@@ -203,9 +204,7 @@ async def set_tool_servers_config(
203204

204205
if auth_type in ('oauth_2.1', 'oauth_2.1_static') and server_id:
205206
try:
206-
oauth_client_info = connection.get('info', {}).get('oauth_client_info', '')
207-
oauth_client_info = decrypt_data(oauth_client_info)
208-
207+
oauth_client_info = resolve_oauth_client_info(connection)
209208
request.app.state.oauth_client_manager.add_client(
210209
f'{server_type}:{server_id}',
211210
OAuthClientInformationFull(**oauth_client_info),

backend/open_webui/utils/oauth.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,25 @@ async def get_oauth_client_info_with_static_credentials(
548548
raise e
549549

550550

551+
552+
def resolve_oauth_client_info(connection: dict) -> dict:
553+
"""
554+
Decrypt OAuth client info from a tool server connection config.
555+
556+
For oauth_2.1_static, overlays admin-provided credentials from
557+
info.oauth_client_id and info.oauth_client_secret onto the blob.
558+
"""
559+
info = connection.get('info', {})
560+
data = decrypt_data(info.get('oauth_client_info', ''))
561+
562+
if connection.get('auth_type') == 'oauth_2.1_static':
563+
if info.get('oauth_client_id') and info.get('oauth_client_secret'):
564+
data['client_id'] = info['oauth_client_id']
565+
data['client_secret'] = info['oauth_client_secret']
566+
567+
return data
568+
569+
551570
class OAuthClientManager:
552571
def __init__(self, app):
553572
self.oauth = OAuth()
@@ -624,7 +643,7 @@ def ensure_client_from_config(self, client_id):
624643
continue
625644

626645
try:
627-
oauth_client_info = decrypt_data(oauth_client_info)
646+
oauth_client_info = resolve_oauth_client_info(connection)
628647
return self.add_client(expected_client_id, OAuthClientInformationFull(**oauth_client_info))['client']
629648
except Exception as e:
630649
log.error(f'Failed to lazily add OAuth client {expected_client_id} from config: {e}')

0 commit comments

Comments
 (0)