From 01472c071b610187b932274eaf5b061848dc45fb Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 8 May 2024 08:40:18 -0700 Subject: [PATCH] feat: support webui_auth false #929 --- backend/apps/web/routers/auths.py | 15 ++++++++++++++- backend/config.py | 2 +- backend/main.py | 2 ++ backend/utils/misc.py | 7 ++++--- src/routes/auth/+page.svelte | 4 ++-- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/backend/apps/web/routers/auths.py b/backend/apps/web/routers/auths.py index 01fddb73a..bb5975f19 100644 --- a/backend/apps/web/routers/auths.py +++ b/backend/apps/web/routers/auths.py @@ -33,7 +33,7 @@ from utils.utils import ( from utils.misc import parse_duration, validate_email_format from utils.webhook import post_webhook from constants import ERROR_MESSAGES, WEBHOOK_MESSAGES -from config import WEBUI_AUTH_TRUSTED_EMAIL_HEADER +from config import WEBUI_AUTH, WEBUI_AUTH_TRUSTED_EMAIL_HEADER router = APIRouter() @@ -118,6 +118,19 @@ async def signin(request: Request, form_data: SigninForm): ), ) user = Auths.authenticate_user_by_trusted_header(trusted_email) + + if WEBUI_AUTH == False: + admin_email = "admin@localhost" + admin_password = "admin" + + if Users.get_num_users() == 0 and not Users.get_user_by_email( + admin_email.lower() + ): + await signup( + request, + SignupForm(email=admin_email, password=admin_password, name="User"), + ) + user = Auths.authenticate_user(admin_email.lower(), admin_password) else: user = Auths.authenticate_user(form_data.email.lower(), form_data.password) diff --git a/backend/config.py b/backend/config.py index d027670fd..e2749b465 100644 --- a/backend/config.py +++ b/backend/config.py @@ -413,7 +413,7 @@ WEBUI_VERSION = os.environ.get("WEBUI_VERSION", "v1.0.0-alpha.100") # WEBUI_AUTH (Required for security) #################################### -WEBUI_AUTH = True +WEBUI_AUTH = os.environ.get("WEBUI_AUTH", "True").lower() == "true" WEBUI_AUTH_TRUSTED_EMAIL_HEADER = os.environ.get( "WEBUI_AUTH_TRUSTED_EMAIL_HEADER", None ) diff --git a/backend/main.py b/backend/main.py index 330644191..139819f7c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -44,6 +44,7 @@ from config import ( CONFIG_DATA, WEBUI_NAME, WEBUI_URL, + WEBUI_AUTH, ENV, VERSION, CHANGELOG, @@ -240,6 +241,7 @@ async def get_app_config(): "status": True, "name": WEBUI_NAME, "version": VERSION, + "auth": WEBUI_AUTH, "default_locale": default_locale, "images": images_app.state.ENABLED, "default_models": webui_app.state.DEFAULT_MODELS, diff --git a/backend/utils/misc.py b/backend/utils/misc.py index 98528c400..5efff4a35 100644 --- a/backend/utils/misc.py +++ b/backend/utils/misc.py @@ -38,9 +38,10 @@ def calculate_sha256_string(string): def validate_email_format(email: str) -> bool: - if not re.match(r"[^@]+@[^@]+\.[^@]+", email): - return False - return True + if email.endswith("@localhost"): + return True + + return bool(re.match(r"[^@]+@[^@]+\.[^@]+", email)) def sanitize_filename(file_name): diff --git a/src/routes/auth/+page.svelte b/src/routes/auth/+page.svelte index 6b2f3a99a..3bfe6ed14 100644 --- a/src/routes/auth/+page.svelte +++ b/src/routes/auth/+page.svelte @@ -60,7 +60,7 @@ await goto('/'); } loaded = true; - if ($config?.trusted_header_auth ?? false) { + if (($config?.trusted_header_auth ?? false) || $config?.auth === false) { await signInHandler(); } }); @@ -97,7 +97,7 @@ -->
- {#if $config?.trusted_header_auth ?? false} + {#if ($config?.trusted_header_auth ?? false) || $config?.auth === false}