From 82189066e8bff8a37ff09f264f683022ad58ecda Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 16 Feb 2025 18:35:09 -0800 Subject: [PATCH] refac --- backend/open_webui/config.py | 14 +++++--- backend/open_webui/main.py | 33 +++---------------- backend/open_webui/utils/auth.py | 33 +++++++++++++++++++ .../components/admin/Settings/General.svelte | 4 +-- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 08d719162..d62ac3114 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -609,8 +609,6 @@ if frontend_favicon.exists(): shutil.copyfile(frontend_favicon, STATIC_DIR / "favicon.png") except Exception as e: logging.error(f"An error occurred: {e}") -else: - logging.warning(f"Frontend favicon not found at {frontend_favicon}") frontend_splash = FRONTEND_BUILD_DIR / "static" / "splash.png" @@ -619,12 +617,18 @@ if frontend_splash.exists(): shutil.copyfile(frontend_splash, STATIC_DIR / "splash.png") except Exception as e: logging.error(f"An error occurred: {e}") -else: - logging.warning(f"Frontend splash not found at {frontend_splash}") + +frontend_loader = FRONTEND_BUILD_DIR / "static" / "loader.js" + +if frontend_loader.exists(): + try: + shutil.copyfile(frontend_loader, STATIC_DIR / "loader.js") + except Exception as e: + logging.error(f"An error occurred: {e}") #################################### -# CUSTOM_NAME +# CUSTOM_NAME (Legacy) #################################### CUSTOM_NAME = os.environ.get("CUSTOM_NAME", "") diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index b45f192cc..a6fa6bd8c 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -88,7 +88,6 @@ from open_webui.models.models import Models from open_webui.models.users import UserModel, Users from open_webui.config import ( - override_static, LICENSE_KEY, # Ollama ENABLE_OLLAMA_API, @@ -316,7 +315,7 @@ from open_webui.utils.middleware import process_chat_payload, process_chat_respo from open_webui.utils.access_control import has_access from open_webui.utils.auth import ( - verify_signature, + get_license_data, decode_token, get_admin_user, get_verified_user, @@ -373,31 +372,8 @@ async def lifespan(app: FastAPI): if RESET_CONFIG_ON_START: reset_config() - key = app.state.config.LICENSE_KEY - if key: - try: - res = requests.post( - "https://api.openwebui.com/api/v1/license", - json={"key": key, "version": "1"}, - timeout=5, - ) - - if getattr(res, "ok", False): - payload = getattr(res, "json", lambda: {})() - for k, v in payload.items(): - if k == "resources": - for p, c in v.items(): - globals().get("override_static", lambda a, b: None)(p, c) - elif k == "user_count": - setattr(app.state, "USER_COUNT", v) - elif k == "webui_name": - setattr(app.state, "WEBUI_NAME", v) - else: - log.error( - f"License retrieval issue: {getattr(res, 'text', 'unknown error')}" - ) - except Exception as ex: - log.error(f"Uncaught Exception: {ex}") + if app.state.config.LICENSE_KEY: + get_license_data(app, app.state.config.LICENSE_KEY) asyncio.create_task(periodic_usage_pool_cleanup()) yield @@ -414,9 +390,8 @@ oauth_manager = OAuthManager(app) app.state.config = AppConfig() -app.state.config.LICENSE_KEY = LICENSE_KEY - app.state.WEBUI_NAME = WEBUI_NAME +app.state.config.LICENSE_KEY = LICENSE_KEY ######################################## # diff --git a/backend/open_webui/utils/auth.py b/backend/open_webui/utils/auth.py index 0715aaf18..e4ad27c84 100644 --- a/backend/open_webui/utils/auth.py +++ b/backend/open_webui/utils/auth.py @@ -4,6 +4,8 @@ import jwt import base64 import hmac import hashlib +import requests + from datetime import UTC, datetime, timedelta from typing import Optional, Union, List, Dict @@ -11,6 +13,7 @@ from typing import Optional, Union, List, Dict from open_webui.models.users import Users from open_webui.constants import ERROR_MESSAGES +from open_webui.config import override_static from open_webui.env import WEBUI_SECRET_KEY, TRUSTED_SIGNATURE_KEY from fastapi import Depends, HTTPException, Request, Response, status @@ -44,6 +47,36 @@ def verify_signature(payload: str, signature: str) -> bool: return False +def get_license_data(app, key): + if key: + try: + res = requests.post( + "https://api.openwebui.com/api/v1/license", + json={"key": key, "version": "1"}, + timeout=5, + ) + + if getattr(res, "ok", False): + payload = getattr(res, "json", lambda: {})() + for k, v in payload.items(): + if k == "resources": + for p, c in v.items(): + globals().get("override_static", lambda a, b: None)(p, c) + elif k == "user_count": + setattr(app.state, "USER_COUNT", v) + elif k == "webui_name": + setattr(app.state, "WEBUI_NAME", v) + + return True + else: + print( + f"License: retrieval issue: {getattr(res, 'text', 'unknown error')}" + ) + except Exception as ex: + print(f"License: Uncaught Exception: {ex}") + return False + + bearer_security = HTTPBearer(auto_error=False) pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") diff --git a/src/lib/components/admin/Settings/General.svelte b/src/lib/components/admin/Settings/General.svelte index 0876cdba5..218e5374e 100644 --- a/src/lib/components/admin/Settings/General.svelte +++ b/src/lib/components/admin/Settings/General.svelte @@ -170,7 +170,7 @@
-
+
{$i18n.t('Help')}
@@ -180,7 +180,7 @@