oidc.py: single-sourced shared module; guest pill 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. - Guests list: the pill CONSUMES the portal-served `state` (the single invited-vs-active derivation now lives in the portal and is tested there). The local active-flag re-derivation is deleted — the shim is first-served-field-wins (state → guest_status), so the previous payload shape renders identically during a portal rollback. Guests harness extended: invited-wins-over-ledger-active, served expired, and a payload with neither field renders NO derived pill text.
This commit is contained in:
1 parent
267a573eda
commit
80acfaed3b
4 files changed
+160
-16
No files matched your search
+52
-12
@@ -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,9 +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).
|
||||
The `email` scope adds `email`/`email_verified`; `profile` adds `name`,
|
||||
`preferred_username`, etc. We use `sub` (username) as the identity, matching
|
||||
the app's ADMIN_USERNAMES allowlist.
|
||||
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
|
||||
@@ -30,15 +42,44 @@ import httpx
|
||||
from authlib.jose import JsonWebToken, JsonWebKey
|
||||
from itsdangerous import URLSafeTimedSerializer, BadSignature, SignatureExpired
|
||||
|
||||
logger = logging.getLogger("admin-panel")
|
||||
# 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 = "admin_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()
|
||||
|
||||
@@ -56,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="admin-panel-oidc-session")
|
||||
return URLSafeTimedSerializer(secret, salt=_SESSION_SALT)
|
||||
|
||||
|
||||
def _redirect_uri(request_host: str) -> str:
|
||||
@@ -95,7 +136,6 @@ async def exchange_code(code: str, request_host: str, nonce: str) -> str:
|
||||
token_endpoint = _oidc_env("CLOUDRON_OIDC_TOKEN_ENDPOINT")
|
||||
keys_endpoint = _oidc_env("CLOUDRON_OIDC_KEYS_ENDPOINT")
|
||||
|
||||
token_resp = None
|
||||
id_token = None
|
||||
async with httpx.AsyncClient(timeout=20.0) as client:
|
||||
# Token exchange (client_secret_post).
|
||||
|
||||
Reference in new issue
Block a user