feat: refactor signout functionality to use aiohttp for OpenID configuration retrieval

This commit is contained in:
Zaiban Ali 2024-12-08 04:57:57 +01:00
parent 48d604a525
commit 899424b371

View File

@ -3,7 +3,7 @@ import uuid
import time import time
import datetime import datetime
import logging import logging
import httpx from aiohttp import ClientSession
from open_webui.apps.webui.models.auths import ( from open_webui.apps.webui.models.auths import (
AddUserForm, AddUserForm,
@ -507,24 +507,25 @@ async def signout(request: Request, response: Response):
response.delete_cookie("token") response.delete_cookie("token")
if ENABLE_OAUTH_SIGNUP.value: if ENABLE_OAUTH_SIGNUP.value:
id_token = request.cookies.get("id_token", None) id_token = request.cookies.get("id_token")
if id_token: if id_token:
async with httpx.AsyncClient() as client:
try: try:
openid_config = await client.get(OPENID_PROVIDER_URL.value) async with ClientSession() as session:
openid_config.raise_for_status() async with session.get(OPENID_PROVIDER_URL.value) as resp:
openid_data = openid_config.json() if resp.status == 200:
end_session_endpoint = openid_data.get("end_session_endpoint") openid_data = await resp.json()
if end_session_endpoint: logout_url = openid_data.get("end_session_endpoint")
logout_url = f"{end_session_endpoint}?id_token_hint={id_token}" if logout_url:
response.delete_cookie("id_token") response.delete_cookie("id_token")
return RedirectResponse(url=logout_url) return RedirectResponse(url=f"{logout_url}?id_token_hint={id_token}")
except httpx.HTTPStatusError as e: else:
raise HTTPException(status_code=e.response.status_code, detail="Failed to fetch OpenID configuration") raise HTTPException(
status_code=resp.status,
detail="Failed to fetch OpenID configuration"
)
except Exception as e: except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))
# Fall back to the default signout
return {"status": True} return {"status": True}