oidc.py: single-sourced shared module; guest trail consumes the portal's state
- app/oidc.py is now a byte-identical copy of the canonical module (ops-docs: scripts/shared/oidc_client.py); this app's identity (logger name, session cookie, session salt) is set via oidc.configure() in main.py with the app's historical values, so existing sessions survive. tests/test_oidc_sync.py pins the copy to the canonical revision — a hand edit goes red; changes go through the canonical + sync-oidc-module.sh. - The guest trail pill consumes the portal-served `state` (the single invited-vs-active derivation, tested in the portal) with the old status-only payload fallback kept; dashboard-guests-harness.js asserts both shapes through the real page JS.
This commit is contained in:
1 parent
1e6e9e481c
commit
622982a371
4 files changed
+184
-13
No files matched your search
+52
-10
@@ -1,7 +1,12 @@
|
||||
"""OIDC client for Cloudron's OpenID Connect addon.
|
||||
"""OIDC client for Cloudron's OpenID Connect addon — ONE shared module.
|
||||
|
||||
Implements the authorization-code flow against Cloudron's built-in OIDC
|
||||
provider, so the app no longer depends on the proxyAuth header wall.
|
||||
This file is the SINGLE SOURCE for the two thin Cloudron apps that implement
|
||||
the authorization-code flow themselves (admin-panel, member-dashboard). The
|
||||
copies in those repos' app/oidc.py are byte-identical to this file — verified
|
||||
by ops-docs/scripts/check-oidc-sync.py and enforced by each repo's
|
||||
tests/test_oidc_sync.py — so a fix lands here, is synced to both repos, and
|
||||
can never drift. Do not edit a repo copy directly: sync-oidc-module.sh
|
||||
overwrites it, and the pin test goes red on any hand edit.
|
||||
|
||||
Cloudron exports these env vars (they change on every restart — read per-call,
|
||||
never cache at import):
|
||||
@@ -16,8 +21,16 @@ never cache at import):
|
||||
CLOUDRON_APP_DOMAIN the app's public domain
|
||||
|
||||
Identity: Cloudron's `sub` claim is the username (the unique user identifier).
|
||||
We use `sub` (username) as the identity — the portal's broker accepts it as
|
||||
`X-Member-User` and resolves username → email.
|
||||
We use `sub` as the identity; each host app decides what it authorizes — the
|
||||
panel matches it against its ADMIN_USERNAMES allowlist, the dashboard
|
||||
forwards it to the portal's broker as `X-Member-User` (which resolves
|
||||
username → email).
|
||||
|
||||
Per-app identity: the ONLY differences the two copies used to carry were the
|
||||
logger name, the session cookie name and the session salt. The host app now
|
||||
supplies those via configure() at import time (see each app's main.py), which
|
||||
keeps this file byte-identical everywhere while preserving each app's
|
||||
existing cookie names and salts (so existing sessions survive a redeploy).
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -29,15 +42,44 @@ import httpx
|
||||
from authlib.jose import JsonWebToken, JsonWebKey
|
||||
from itsdangerous import URLSafeTimedSerializer, BadSignature, SignatureExpired
|
||||
|
||||
logger = logging.getLogger("member-dashboard")
|
||||
# Per-app identity — set by the host app at import time via configure().
|
||||
# The unset sentinels below are loud on purpose: a copy that reaches a
|
||||
# request without its host app having configured itself is a sync/setup bug.
|
||||
APP_NAME = "unset-app"
|
||||
SESSION_COOKIE = "unset_oidc_session"
|
||||
_SESSION_SALT = "unset-oidc-session"
|
||||
|
||||
# Session cookie name + max age (8 hours, roughly a working day).
|
||||
SESSION_COOKIE = "member_oidc_session"
|
||||
SESSION_MAX_AGE = 60 * 60 * 8
|
||||
SESSION_MAX_AGE = 60 * 60 * 8 # 8 hours, roughly a working day
|
||||
|
||||
SCOPES = "openid profile email"
|
||||
|
||||
|
||||
def configure(app_name: str, session_cookie: str, session_salt: str) -> None:
|
||||
"""Set this app's identity. The host app MUST call this at import time."""
|
||||
global APP_NAME, SESSION_COOKIE, _SESSION_SALT
|
||||
APP_NAME = app_name
|
||||
SESSION_COOKIE = session_cookie
|
||||
_SESSION_SALT = session_salt
|
||||
|
||||
|
||||
class _LazyLogger:
|
||||
"""Resolves at CALL time, not import time.
|
||||
|
||||
The host app calls configure() AFTER this module is imported (from its
|
||||
main.py), so an eagerly-created module-level logger would bind the
|
||||
'unset-app' sentinel name and every OIDC log line would carry the wrong
|
||||
attribution. Logging through this proxy reads the configured APP_NAME
|
||||
when the line is actually emitted; logging.getLogger caches by name, so
|
||||
the per-call lookup is free.
|
||||
"""
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(logging.getLogger(APP_NAME), name)
|
||||
|
||||
|
||||
logger = _LazyLogger()
|
||||
|
||||
|
||||
def _oidc_env(name: str) -> str:
|
||||
return os.environ.get(name, "").strip()
|
||||
|
||||
@@ -55,7 +97,7 @@ def _session_serializer() -> URLSafeTimedSerializer:
|
||||
# Derive a stable, secret signing key from the OIDC client secret (already
|
||||
# a secret the app holds, and stable across restarts).
|
||||
secret = _oidc_env("CLOUDRON_OIDC_CLIENT_SECRET") or _secrets.token_urlsafe(32)
|
||||
return URLSafeTimedSerializer(secret, salt="member-dashboard-oidc-session")
|
||||
return URLSafeTimedSerializer(secret, salt=_SESSION_SALT)
|
||||
|
||||
|
||||
def _redirect_uri(request_host: str) -> str:
|
||||
|
||||
Reference in new issue
Block a user