diff --git a/backend/open_webui/routers/auths.py b/backend/open_webui/routers/auths.py index 8967d2a40..b1ac1ea13 100644 --- a/backend/open_webui/routers/auths.py +++ b/backend/open_webui/routers/auths.py @@ -34,14 +34,17 @@ from fastapi import APIRouter, Depends, HTTPException, Request, status from fastapi.responses import RedirectResponse, Response from open_webui.config import OPENID_PROVIDER_URL, ENABLE_OAUTH_SIGNUP, ENABLE_LDAP from pydantic import BaseModel + from open_webui.utils.misc import parse_duration, validate_email_format from open_webui.utils.auth import ( + decode_token, create_api_key, create_token, get_admin_user, get_verified_user, get_current_user, get_password_hash, + get_http_authorization_cred, ) from open_webui.utils.webhook import post_webhook from open_webui.utils.access_control import get_permissions @@ -73,31 +76,13 @@ class SessionUserResponse(Token, UserResponse): async def get_session_user( request: Request, response: Response, user=Depends(get_current_user) ): - expires_delta = parse_duration(request.app.state.config.JWT_EXPIRES_IN) - expires_at = None - if expires_delta: - expires_at = int(time.time()) + int(expires_delta.total_seconds()) - token = create_token( - data={"id": user.id}, - expires_delta=expires_delta, - ) + auth_header = request.headers.get("Authorization") + auth_token = get_http_authorization_cred(auth_header) + token = auth_token.credentials - datetime_expires_at = ( - datetime.datetime.fromtimestamp(expires_at, datetime.timezone.utc) - if expires_at - else None - ) - - # Set the cookie token - response.set_cookie( - key="token", - value=token, - expires=datetime_expires_at, - httponly=True, # Ensures the cookie is not accessible via JavaScript - samesite=WEBUI_AUTH_COOKIE_SAME_SITE, - secure=WEBUI_AUTH_COOKIE_SECURE, - ) + data = decode_token(token) + expires_at = data.get("exp") user_permissions = get_permissions( user.id, request.app.state.config.USER_PERMISSIONS @@ -289,11 +274,14 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm): user = Auths.authenticate_user_by_trusted_header(email) if user: + expires_delta = parse_duration(request.app.state.config.JWT_EXPIRES_IN) + expires_at = None + if expires_delta: + expires_at = int(time.time()) + int(expires_delta.total_seconds()) + token = create_token( data={"id": user.id}, - expires_delta=parse_duration( - request.app.state.config.JWT_EXPIRES_IN - ), + expires_delta=expires_delta, ) # Set the cookie token @@ -301,6 +289,8 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm): key="token", value=token, httponly=True, # Ensures the cookie is not accessible via JavaScript + samesite=WEBUI_AUTH_COOKIE_SAME_SITE, + secure=WEBUI_AUTH_COOKIE_SECURE, ) user_permissions = get_permissions( @@ -310,6 +300,7 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm): return { "token": token, "token_type": "Bearer", + "expires_at": expires_at, "id": user.id, "email": user.email, "name": user.name, diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index fd9a33111..104f92e27 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -251,77 +251,79 @@ {/if} -
-
- {#if !['user', 'admin'].includes($user?.role)} - - {:else if localDBChats.length > 0} -
-
-
-
-
- Important Update
Action Required for Chat Log Storage -
+{#if $user} +
+
+ {#if !['user', 'admin'].includes($user?.role)} + + {:else if localDBChats.length > 0} +
+
+
+
+
+ Important Update
Action Required for Chat Log Storage +
-
- {$i18n.t( - "Saving chat logs directly to your browser's storage is no longer supported. Please take a moment to download and delete your chat logs by clicking the button below. Don't worry, you can easily re-import your chat logs to the backend through" - )} - {$i18n.t('Settings')} > {$i18n.t('Chats')} > {$i18n.t('Import Chats')}. {$i18n.t( - 'This ensures that your valuable conversations are securely saved to your backend database. Thank you!' - )} -
+
+ {$i18n.t( + "Saving chat logs directly to your browser's storage is no longer supported. Please take a moment to download and delete your chat logs by clicking the button below. Don't worry, you can easily re-import your chat logs to the backend through" + )} + {$i18n.t('Settings')} > {$i18n.t('Chats')} > {$i18n.t('Import Chats')}. {$i18n.t( + 'This ensures that your valuable conversations are securely saved to your backend database. Thank you!' + )} +
-
- + localDBChats = []; + }} + > + Download & Delete + - + +
-
- {/if} + {/if} - + - {#if loaded} - - {:else} -
- -
- {/if} + {#if loaded} + + {:else} +
+ +
+ {/if} +
-
+{/if}