Cleanup of origins handling. Added site_url and site_path such that these can be determined reliably, and we print it in the startbox.

This commit is contained in:
2025-12-06 03:39:05 +00:00
parent df5c176bcd
commit a1b73711e6
6 changed files with 157 additions and 89 deletions
+27 -36
View File
@@ -3,38 +3,31 @@
import json
import os
from functools import lru_cache
from urllib.parse import urlparse, urlsplit
from paskia.globals import passkey as global_passkey
from urllib.parse import urlsplit
@lru_cache(maxsize=1)
def _load_config() -> tuple[str, str] | None:
"""Load auth_host from PASKIA_CONFIG JSON.
Returns (scheme, netloc) tuple if configured, None otherwise.
"""
def _load_config() -> dict:
"""Load PASKIA_CONFIG JSON."""
config_json = os.getenv("PASKIA_CONFIG")
if not config_json:
return None
config = json.loads(config_json)
raw = config["auth_host"] # Always present, may be None
if not raw:
return None
parsed = urlparse(raw if "://" in raw else f"//{raw}")
netloc = parsed.netloc or parsed.path
if not netloc:
return None
return (parsed.scheme or "https", netloc.strip("/"))
def configured_auth_host() -> str | None:
cfg = _load_config()
return cfg[1] if cfg else None
return {}
return json.loads(config_json)
def is_root_mode() -> bool:
return _load_config() is not None
return _load_config().get("auth_host") is not None
def configured_auth_host() -> str | None:
"""Return configured auth_host netloc, or None."""
auth_host = _load_config().get("auth_host")
if not auth_host:
return None
from urllib.parse import urlparse
parsed = urlparse(auth_host if "://" in auth_host else f"//{auth_host}")
return parsed.netloc or parsed.path or None
def ui_base_path() -> str:
@@ -42,25 +35,23 @@ def ui_base_path() -> str:
def auth_site_base_url() -> str:
"""Return the base URL for the auth site UI.
If auth_host is configured (root mode), returns its URL.
Otherwise, constructs URL from rp_id with /auth/ path.
"""
"""Return the base URL for the auth site UI (computed at startup)."""
cfg = _load_config()
if cfg:
scheme, netloc = cfg
return f"{scheme}://{netloc}/"
# Not in root mode: use rp_id with /auth/ path
rp_id = global_passkey.instance.rp_id
return f"https://{rp_id}/auth/"
return cfg.get("site_url", "https://localhost") + cfg.get("site_path", "/auth/")
def reset_link_url(token: str) -> str:
"""Generate a reset link URL for the given token."""
return f"{auth_site_base_url()}{token}"
def normalize_origin(origin: str) -> str:
"""Normalize an origin URL by adding https:// if no scheme is present."""
if "://" not in origin:
return f"https://{origin}"
return origin
def reload_config() -> None:
_load_config.cache_clear()
+19 -17
View File
@@ -1,6 +1,13 @@
"""Startup configuration box formatting utilities."""
import os
from sys import stderr
from typing import TYPE_CHECKING
from paskia._version import __version__
if TYPE_CHECKING:
from paskia.config import PaskiaConfig
BOX_WIDTH = 60 # Inner width (excluding box chars)
@@ -20,23 +27,18 @@ def bottom() -> str:
return "" + "" * (BOX_WIDTH + 2) + "\n"
def print_startup_config(passkey_instance, args, host, port, uds) -> None:
def print_startup_config(config: "PaskiaConfig") -> None:
"""Print server configuration on startup."""
from sys import stderr
from paskia._version import __version__
lines = [top()]
lines.append(line(" ▄▄▄▄▄"))
lines.append(line("█ █ Paskia " + __version__))
lines.append(line("█ █▄▄▄▄▄▄▄▄▄▄▄▄"))
lines.append(line("█ █▀▀▀▀█▀▀█▀▀█"))
lines.append(line("█ █▀▀▀▀█▀▀█▀▀█ " + config.site_url + config.site_path))
lines.append(line(" ▀▀▀▀▀"))
# Format auth host section
auth_host = getattr(args, "auth_host", None)
if auth_host:
lines.append(line(f"Auth Host: {auth_host}"))
if config.auth_host:
lines.append(line(f"Auth Host: {config.auth_host}"))
# Show frontend URL if in dev mode
devmode = os.environ.get("PASKIA_DEVMODE")
@@ -44,24 +46,24 @@ def print_startup_config(passkey_instance, args, host, port, uds) -> None:
lines.append(line(f"Dev Frontend: {devmode}"))
# Format listen address with scheme
if uds:
listen = f"unix:{uds}"
elif host:
listen = f"http://{host}:{port}"
if config.uds:
listen = f"unix:{config.uds}"
elif config.host:
listen = f"http://{config.host}:{config.port}"
else:
listen = f"http://0.0.0.0:{port} + [::]:{port}"
listen = f"http://0.0.0.0:{config.port} + [::]:{config.port}"
lines.append(line(f"Backend: {listen}"))
# Relying Party line (omit name if same as id)
rp_id = passkey_instance.rp_id
rp_name = passkey_instance.rp_name
rp_id = config.rp_id
rp_name = config.rp_name
if rp_name and rp_name != rp_id:
lines.append(line(f"Relying Party: {rp_id} ({rp_name})"))
else:
lines.append(line(f"Relying Party: {rp_id}"))
# Format origins section
allowed = passkey_instance.allowed_origins
allowed = config.origins
if allowed:
lines.append(line("Permitted Origins:"))
for origin in sorted(allowed):