Guests sign in to the ai-agent and Jellyfin with one account at an OpenID Connect provider (Pocket ID in the homelab, login.gabvdl.xyz). - users/oidc.py: code flow + PKCE on the guest host; state kept server-side and bound to the browser (aia_oidc cookie); RS256 ID token checked against the JWKS with cryptography (no new dependency); sub → guest by idpId, else once by email; refusals → /login?error=… and `oidc_refused`. /api/auth/methods tells the login page; with OIDC configured the mailed link / code login answers 410 (old sessions stay valid). - Invitations are Pocket ID one-time login codes: the hub opens the authorize request server-side and sends the browser through /lc/<code> to that request's /interaction step (the code page won't redirect to /authorize). - users/idp.py: every guest mirrored into Pocket ID on create / save / disable / delete (username = hub id, the ai-agent group + one group per app switched on in the new Apps panel; foreign groups kept); POST /api/users/idp-sync for all; the result shown, never failing a save. - users/session.py: one session-opening path for both logins. - Login page: "Continue with …" + error messages (French included); Users page: Apps switches, identity-provider state, "Mail new sign-in" (mail: "sso", EN + FR mail). Mock: ?mockSso=1. - test_oidc.py against a fake Pocket ID (22 tests); docs: CLAUDE.md, docs/multi-user.md round 9. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.9 KiB
Python
40 lines
1.9 KiB
Python
"""Opening a guest's browser session — one path for every way they sign in
|
|
(the built-in mailed link / code, users/routes.py; Pocket ID, users/oidc.py)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import Request, Response
|
|
|
|
from core.i18n import primary_lang
|
|
from security import service as security
|
|
from users.caller import SESSION_COOKIE
|
|
from users.store import SESSION_TTL_S
|
|
|
|
|
|
def secure(request: Request) -> bool:
|
|
return (request.headers.get("x-forwarded-proto") or request.url.scheme) == "https"
|
|
|
|
|
|
def record_login(deps, uid: str, request: Request) -> None:
|
|
"""The sign-in in the security log, and a ``new_device`` event (a push,
|
|
by default) when the IP or the browser is one this guest never used."""
|
|
ip, ua = security.client_ip(request), request.headers.get("user-agent", "")
|
|
seen = deps.security_state.see_device(uid, ip, ua)
|
|
security.record(deps, "guest_login", who=uid, request=request)
|
|
if not seen["first"] and (seen["ip"] or seen["ua"]):
|
|
new = " and ".join(w for w, n in (("IP", seen["ip"]), ("browser", seen["ua"])) if n)
|
|
security.record(deps, "new_device", who=uid, request=request,
|
|
detail=f"new {new}: {ip} · {ua[:80]}")
|
|
|
|
|
|
def open_session(deps, request: Request, response: Response, uid: str) -> None:
|
|
"""Sign ``uid`` in on this browser: the session it already had (if any)
|
|
gives way to the new one, the login is recorded, the cookie set."""
|
|
deps.auth.close_session(request.cookies.get(SESSION_COOKIE, ""))
|
|
token = deps.auth.open_session(uid, request.headers.get("user-agent", ""))
|
|
deps.users.touch_login(uid)
|
|
deps.users.set_browser_lang(uid, primary_lang(request.headers.get("accept-language", "")))
|
|
record_login(deps, uid, request)
|
|
response.set_cookie(SESSION_COOKIE, token, max_age=SESSION_TTL_S,
|
|
httponly=True, secure=secure(request), samesite="lax", path="/")
|