Files
romm/backend/endpoints/auth.py
Stu Anderson e06e0e4010 chore(docs): better openapi descriptions (#5660)
* docs(api): describe the core endpoints and add a docs test

Give the parameters, operations and response fields of the core routers a
description in the OpenAPI document: roms, platforms, collections, users,
saves, states, screenshots, channels, snapshots, streaming, devices, client
tokens, play sessions, search, auth, music, memory cards, firmware, sync,
config, feeds, notifications and permissions.

Response models read attribute docstrings (`use_attribute_docstrings` on the
shared response `BaseModel`), so a string after a field is its description.
The update-rom form reads its descriptions from `RomUpdateForm`, so they
have one home. The paging parameters gain examples, such as 72 per page.

`tests/endpoints/test_openapi_docs.py` reads `app.openapi()` and fails when a
parameter or operation of a listed router has no description, or when a
schema with some described fields leaves others bare. Add a router to
`DOCUMENTED_ROUTERS` once its parameters and operations are described.

Descriptions only: no parameter, constraint or default changes. The frontend
types need regenerating (`npm run generate`).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs(api): describe the remaining routers and the roms list parameters

Cover the last routers (tasks, activity, heartbeat, audit events, stats, logs
and netplay) and add the routers to the docs test's allow-list, so every
router is now held to a description on each parameter and operation.

Describe the schemas that test found partly documented: the task status
responses and `TaskInfo` (as `Annotated[..., Field(...)]`, since a TypedDict
takes no attribute docstrings) and the audit page's `max_id`.

Rewrite the `GET /api/roms` parameters that did not say what they do:
`with_char_index`, `with_filter_values`, `with_rom_id_index`, `with_total`,
`with_provider_metadata`, `order_by`, `with_files`, `group_by_meta_id` and
`search_term`, each with an example or the values it accepts.

Replace a franchise name in an existing docstring example with a neutral one.

Descriptions only: no parameter, constraint or default changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* chore(api): regenerate the frontend types

Run `npm run generate` against the backend so the generated models carry the
descriptions added to the OpenAPI document. Only comments change: no type,
field or signature differs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs(api): fix alternative_names and shorten docstrings per review

Address the review feedback:

- Describe `alternative_names` as the hand-set titles when there are any,
  otherwise the titles from IGDB, MobyGames and ScreenScraper, as
  `resolve_alternative_names` returns them. It said every matched provider.
- Check HEAD routes in the docs test. Both existing HEAD routes already pass.
- Keep docstrings to one paragraph, as the repository asks. The test module
  docstring is one sentence and its two rules are short comments. Nineteen
  operation docstrings added in this change drop their second paragraph.

Descriptions and a test only: no parameter, constraint or default changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* chore(api): regenerate the frontend types

Pick up the corrected `alternative_names` description. Only comments change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs(api): correct descriptions that disagree with the code (pr-ready: code-review)

Fix 12 OpenAPI descriptions that contradicted handler behavior (empty
provider metadata, virtual collection types, streaming container lookup,
save device sync, user collections, search terms, file order, firmware
file name, audit max_id, play session offset, save target layouts).
Share the music filter descriptions through Annotated aliases and drop
comments that restated the new docstrings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(api): share parameter aliases and check every router (pr-ready: simplify)

Move the rom id and sort direction aliases to utils/router.py and reuse
them, add per-router id path aliases in place of repeated inline params,
let LimitOffsetPage inherit the shared BaseModel config, and drop Args
blocks that restated parameter descriptions. The docs test now covers
every /api path instead of an allowlist. The OpenAPI spec is unchanged
apart from description strings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(api): form model for rom updates, shared schema config and rom id alias

- Take RomUpdateForm as a FastAPI form model instead of 33 Form params
  and a duplicate field dict; only sent keys still count as provided.
- Document TypedDict responses with attribute docstrings through
  @with_config(API_SCHEMA_CONFIG) instead of per-field Annotated wrappers.
- Move request payload models onto the shared BaseModel and use
  attribute docstrings for their descriptions.
- Replace the 32 inline rom id path params with RomIdPath, which now
  carries ge=1 (adds minimum: 1 to the activity and streaming rom ids).

The update rom request body now references a RomUpdateForm schema; the
multipart fields are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(api): keep only the descriptions that explain something

Drop the field and parameter descriptions that restate the name, the
type, or what the schema already shows (enum and literal values,
defaults, bounds, nullability), along with the wrappers and aliases
that only carried them. Reword four that did not say what the field
is. The docs test now checks operations only, since requiring a
description on every parameter and field is what produced the filler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(api): plainer wording for three sync and rom descriptions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(api): rewrite clipped descriptions as full sentences

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(api): document error statuses in responses instead of docstrings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(api): document pairing code exchange errors

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(api): move error statuses from route docstrings into responses

Covers the routes whose docstrings stated an error in prose or in a
Raises: block, new in this PR or older.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-20 14:11:13 -05:00

509 lines
17 KiB
Python

import asyncio
from datetime import datetime, timedelta, timezone
from typing import Annotated, Final, Literal, Optional, cast
from urllib.parse import urlencode
from authlib.common.errors import AuthlibBaseError
from fastapi import BackgroundTasks, Body, Depends, HTTPException, Request, status
from fastapi.responses import RedirectResponse
from fastapi.security.http import HTTPBasic, HTTPBasicCredentials
from httpx2 import HTTPError
from joserfc.errors import JoseError
from config import (
OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS,
OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS,
OIDC_ENABLED,
OIDC_END_SESSION_ENDPOINT,
OIDC_REDIRECT_URI,
OIDC_RP_INITIATED_LOGOUT,
)
from decorators.auth import oauth
from endpoints.forms.identity import OAuth2RequestForm
from endpoints.responses.oauth import OIDCLogoutResponse, TokenResponse
from exceptions.auth_exceptions import (
AuthCredentialsException,
OIDCDisabledException,
OIDCNotConfiguredException,
UserDisabledException,
)
from handler.audit_handler import (
AuditActor,
AuditTarget,
claim_once,
client_ip,
record,
release_claim,
within_budget,
)
from handler.auth.base_handler import auth_handler, oauth_handler, oidc_handler
from handler.database import db_user_handler
from logger.formatter import CYAN
from logger.formatter import highlight as hl
from logger.logger import log
from models.audit_event import AuditAction
from models.user import User
from utils.auth import create_or_find_web_device
from utils.router import APIRouter
from utils.validation import ValidationError, validate_password
router = APIRouter(
tags=["auth"],
)
# Failed sign-ins from one address for one username within this window are one attempt.
LOGIN_FAILURE_WINDOW_SECONDS: Final = 60
# At most this many failed sign-ins are recorded per address each window, so a
# client cycling usernames can't flood the log.
LOGIN_FAILURES_PER_ADDRESS: Final = 20
LOGIN_FAILURE_ADDRESS_WINDOW_SECONDS: Final = 10 * 60
# Repeated reset requests for one user from one address are one request.
PASSWORD_RESET_REQUEST_WINDOW_SECONDS: Final = 10 * 60
LoginMethod = Literal["password", "token", "oidc"]
def _record_login(
request: Request, user: User, method: LoginMethod, device_id: str | None = None
) -> None:
record(
AuditAction.AUTH_LOGIN,
AuditActor.for_user(user, ip_address=client_ip(request), device_id=device_id),
data={"method": method},
)
def _record_login_failure(
request: Request,
username: str | None,
method: LoginMethod,
reason: Literal["credentials", "disabled"],
) -> None:
ip_address = client_ip(request)
key = f"login_failed:{ip_address}:{username}"
if not claim_once(key, LOGIN_FAILURE_WINDOW_SECONDS) or not within_budget(
f"login_failed:{ip_address}",
LOGIN_FAILURES_PER_ADDRESS,
LOGIN_FAILURE_ADDRESS_WINDOW_SECONDS,
):
return
user = db_user_handler.get_user_by_username(username) if username else None
# A name that matches no account may be a password typed in the wrong box,
# so only an account's own name is kept.
if not record(
AuditAction.AUTH_LOGIN_FAILED,
(
AuditActor.for_user(user, ip_address=ip_address)
if user
else AuditActor.anonymous(ip_address)
),
data={
"username": user.username if user else None,
"method": method,
"reason": reason,
},
):
release_claim(key)
def _record_password_reset_request(ip_address: str | None, user: User) -> None:
key = f"password_reset:{ip_address}:{user.id}"
if claim_once(key, PASSWORD_RESET_REQUEST_WINDOW_SECONDS) and not record(
AuditAction.AUTH_PASSWORD_RESET_REQUEST,
AuditActor.anonymous(ip_address),
AuditTarget.of_user(user),
):
release_claim(key)
# Session authentication endpoints
@router.post("/login")
def login(
request: Request,
credentials: HTTPBasicCredentials = Depends(HTTPBasic()), # noqa
) -> None:
"""Session login endpoint
Args:
request (Request): Fastapi Request object
credentials: Defaults to Depends(HTTPBasic()).
Raises:
CredentialsException: Invalid credentials
UserDisabledException: Auth is disabled
"""
user = auth_handler.authenticate_user(credentials.username, credentials.password)
if not user:
_record_login_failure(request, credentials.username, "password", "credentials")
raise AuthCredentialsException
if not user.enabled:
_record_login_failure(request, credentials.username, "password", "disabled")
raise UserDisabledException
request.session["iss"] = "romm:auth"
request.session["sub"] = user.username
# Auto-create or find the web browser device
device = create_or_find_web_device(request, user)
request.session["device_id"] = device.id
# Update last login and active times
now = datetime.now(timezone.utc)
db_user_handler.update_user(user.id, {"last_login": now, "last_active": now})
_record_login(request, user, "password", device.id)
@router.post("/logout", status_code=status.HTTP_200_OK)
async def logout(request: Request) -> Optional[OIDCLogoutResponse]:
"""Session logout endpoint
Args:
request (Request): Fastapi Request object
Returns:
Optional[dict]: When OIDC RP-Initiated Logout is enabled and the session
contains an OIDC id_token, returns a dict with the OIDC end-session URL
so the client can redirect the browser to log out of the OIDC provider.
"""
id_token = request.session.get("oidc_id_token")
request.session.clear()
if OIDC_RP_INITIATED_LOGOUT and id_token:
end_session_endpoint = OIDC_END_SESSION_ENDPOINT
if not end_session_endpoint and oauth.openid:
try:
metadata = await oauth.openid.load_server_metadata()
end_session_endpoint = metadata.get("end_session_endpoint", "")
except Exception:
log.warning(
"Failed to load OIDC server metadata for RP-Initiated Logout"
)
if end_session_endpoint:
params = urlencode({"id_token_hint": id_token})
return {"oidc_logout_url": f"{end_session_endpoint}?{params}"}
return None
@router.post(
"/token",
responses={
status.HTTP_400_BAD_REQUEST: {
"description": "A required field is missing, or the grant type is not supported."
},
status.HTTP_401_UNAUTHORIZED: {
"description": "The username and password, or the refresh token, are not valid."
},
status.HTTP_403_FORBIDDEN: {
"description": "The account is disabled, or the requested scopes are not granted to it."
},
},
)
async def token(
request: Request, form_data: Annotated[OAuth2RequestForm, Depends()]
) -> TokenResponse:
"""Issue an access token and a refresh token, from a username and password or from a refresh token."""
user: User | None
# Read before any credential check, so a revocation that lands during the
# check still covers the tokens issued below.
issued_at = datetime.now(timezone.utc)
# Support refreshing access tokens
if form_data.grant_type == "refresh_token":
token = form_data.refresh_token
if not token:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Missing refresh token"
)
user, claims = await oauth_handler.consume_refresh_token(token)
if not user or not claims:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token"
)
if not user.enabled:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="User account is disabled"
)
access_token = oauth_handler.create_access_token(
data={
"sub": user.username,
"iss": "romm:oauth",
"scopes": claims.get("scopes"),
},
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
issued_at=issued_at,
)
refresh_token = oauth_handler.create_refresh_token(
data={
"sub": user.username,
"iss": "romm:oauth",
"scopes": claims.get("scopes"),
},
expires_delta=timedelta(seconds=OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS),
issued_at=issued_at,
)
return {
"access_token": access_token,
"refresh_token": refresh_token,
"token_type": "bearer", # trunk-ignore(bandit/B105)
"expires": OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS,
"refresh_expires": OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS,
}
# Authentication via username/password
elif form_data.grant_type == "password":
if not form_data.username or not form_data.password:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Missing username or password",
)
user = await asyncio.to_thread(
auth_handler.authenticate_user, form_data.username, form_data.password
)
if not user:
_record_login_failure(request, form_data.username, "token", "credentials")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid username or password",
)
if not user.enabled:
_record_login_failure(request, form_data.username, "token", "disabled")
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="User account is disabled"
)
# TODO: Authentication via client_id/client_secret
elif form_data.grant_type == "client_credentials":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Client credentials are not yet supported",
)
else:
# All other grant types are unsupported
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid or unsupported grant type",
)
# Check if user has access to requested scopes
if not set(form_data.scopes).issubset(user.oauth_scopes):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Insufficient scope",
)
access_token = oauth_handler.create_access_token(
data={
"sub": user.username,
"iss": "romm:oauth",
"scopes": " ".join(form_data.scopes),
},
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
issued_at=issued_at,
)
refresh_token = oauth_handler.create_refresh_token(
data={
"sub": user.username,
"iss": "romm:oauth",
"scopes": " ".join(form_data.scopes),
},
expires_delta=timedelta(seconds=OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS),
issued_at=issued_at,
)
_record_login(request, user, "token")
return {
"access_token": access_token,
"refresh_token": refresh_token,
"token_type": "bearer", # trunk-ignore(bandit/B105)
"expires": OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS,
"refresh_expires": OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS,
}
# OIDC login and callback endpoints
@router.get("/login/openid")
async def login_via_openid(request: Request) -> RedirectResponse:
"""OIDC login endpoint
Args:
request (Request): Fastapi Request object
Raises:
OIDCDisabledException: OAuth is disabled
OIDCNotConfiguredException: OAuth not configured
Returns:
RedirectResponse: Redirect to OIDC provider
"""
if not OIDC_ENABLED:
raise OIDCDisabledException
if not oauth.openid:
raise OIDCNotConfiguredException
# Authlib fetches the provider's discovery document here, so a wrong
# OIDC_SERVER_APPLICATION_URL surfaces as an HTTP error
try:
return cast(
RedirectResponse,
await oauth.openid.authorize_redirect(request, OIDC_REDIRECT_URI),
)
except HTTPError as exc:
log.error(
f"OIDC provider discovery failed, check OIDC_SERVER_APPLICATION_URL: {exc!r}"
)
# Without the bypass, OIDC autologin would send the failure straight
# back here and loop
return RedirectResponse(url="/login?bypass_autologin=true")
@router.get(
"/oauth/openid",
responses={
status.HTTP_400_BAD_REQUEST: {
"description": "The provider's token lacks the user info, email or username, or the email is not verified."
},
status.HTTP_401_UNAUTHORIZED: {
"description": "No user matches the identity, or the user is disabled."
},
status.HTTP_403_FORBIDDEN: {
"description": "The user has no role for RomM, the account is linked to another identity, or registration is disabled."
},
status.HTTP_500_INTERNAL_SERVER_ERROR: {
"description": "OIDC is disabled or not configured."
},
},
)
async def auth_openid(request: Request) -> RedirectResponse:
"""Complete an OIDC sign-in, then redirect to the home page, or to the login page if the provider rejects it."""
if not OIDC_ENABLED:
raise OIDCDisabledException
if not oauth.openid:
raise OIDCNotConfiguredException
# Authlib verifies the ID token with joserfc, whose errors are not Authlib's.
try:
token = await oauth.openid.authorize_access_token(request)
except (AuthlibBaseError, JoseError) as exc:
# repr() because error and description can come from the query string
log.warning(f"OIDC callback rejected: {exc.error!r}: {exc.description!r}")
if request.user.is_authenticated and not request.user.is_kiosk_guest:
return RedirectResponse(url="/")
# Without the bypass, OIDC autologin would send a persistent failure
# straight back to the provider and loop
return RedirectResponse(url="/login?bypass_autologin=true")
potential_user, _userinfo = (
await oidc_handler.get_current_active_user_from_openid_token(token)
)
if not potential_user:
_record_login_failure(request, None, "oidc", "credentials")
raise AuthCredentialsException
if not potential_user.enabled:
_record_login_failure(request, potential_user.username, "oidc", "disabled")
raise UserDisabledException
request.session["iss"] = "romm:auth"
request.session["sub"] = potential_user.username
if OIDC_RP_INITIATED_LOGOUT:
request.session["oidc_id_token"] = token.get("id_token", "")
# Auto-create or find the web browser device
device = create_or_find_web_device(request, potential_user)
request.session["device_id"] = device.id
# Update last login and active times
now = datetime.now(timezone.utc)
db_user_handler.update_user(
potential_user.id, {"last_login": now, "last_active": now}
)
_record_login(request, potential_user, "oidc", device.id)
return RedirectResponse(url="/")
@router.post("/forgot-password", status_code=status.HTTP_200_OK)
def request_password_reset(
request: Request,
background_tasks: BackgroundTasks,
username: str = Body(..., embed=True),
) -> None:
"""Request a password reset link for the user.
Args:
username (str): Username of the user requesting the reset
Returns:
None: Returns 200 OK status
"""
user = db_user_handler.get_user_by_username(username)
if user:
# After the response, so its timing can't tell whether the user exists.
# The record goes first: a link that fails to send stops the tasks after it.
background_tasks.add_task(
_record_password_reset_request, client_ip(request), user
)
background_tasks.add_task(auth_handler.send_password_reset_link, user)
else:
log.warning(
f"Reset password link requested for a user {hl(username, color=CYAN)}, but that username does not exist."
)
@router.post("/reset-password", status_code=status.HTTP_200_OK)
async def reset_password(
request: Request,
token: str = Body(..., embed=True),
new_password: str = Body(..., embed=True),
) -> None:
"""Reset password using the token.
Args:
token (str): Reset token from the URL
new_password (str): New user password
Returns:
None: Returns 200 OK status
"""
# Ahead of the token check, which spends the link: a rejected password
# leaves it usable for another try.
try:
validate_password(new_password)
except ValidationError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=exc.message,
) from exc
user = auth_handler.verify_password_reset_token(token)
await auth_handler.set_user_new_password(user, new_password)
record(
AuditAction.AUTH_PASSWORD_RESET,
AuditActor.for_user(user, ip_address=client_ip(request)),
AuditTarget.of_user(user),
)
log.info(
f"Password was successfully reset for user {hl(user.username, color=CYAN)}."
)