mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
The OAuth2 password flow in the OpenAPI schema declared its tokenUrl as /token, but the token endpoint is served at /api/token; a POST to /token is rejected with a 403 before any token code runs. Clients that read tokenUrl from the schema, such as Postman's OpenAPI import, request tokens from the wrong place. The test compares the schema's tokenUrl with the token route's own path, so a future prefix change can't drift them apart again. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
5.2 KiB
Python
162 lines
5.2 KiB
Python
import functools
|
|
import inspect
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
from authlib.integrations.starlette_client import OAuth
|
|
from authlib.oidc.discovery import get_well_known_url
|
|
from fastapi import HTTPException, Security, status
|
|
from fastapi.security.http import HTTPBasic
|
|
from fastapi.security.oauth2 import OAuth2PasswordBearer
|
|
from fastapi.types import DecoratedCallable
|
|
from starlette._utils import is_async_callable
|
|
from starlette.authentication import has_required_scope
|
|
from starlette.config import Config
|
|
from starlette.requests import Request
|
|
|
|
from config import (
|
|
OIDC_CLAIM_ROLES,
|
|
OIDC_CLIENT_ID,
|
|
OIDC_CLIENT_SECRET,
|
|
OIDC_ENABLED,
|
|
OIDC_PROVIDER,
|
|
OIDC_REDIRECT_URI,
|
|
OIDC_SERVER_APPLICATION_URL,
|
|
OIDC_SERVER_METADATA_URL,
|
|
OIDC_TLS_CACERTFILE,
|
|
)
|
|
from handler.auth.constants import (
|
|
EDIT_SCOPES_MAP,
|
|
FULL_SCOPES_MAP,
|
|
READ_SCOPES_MAP,
|
|
WRITE_SCOPES_MAP,
|
|
Scope,
|
|
)
|
|
from utils.tls import build_ca_ssl_context
|
|
|
|
# Using the internal password flow
|
|
oauth2_password_bearer = OAuth2PasswordBearer(
|
|
tokenUrl="/api/token",
|
|
auto_error=False,
|
|
# The maps are keyed by Scope, which is a StrEnum; FastAPI wants plain str.
|
|
scopes={
|
|
str(scope): description
|
|
for scope_map in (
|
|
READ_SCOPES_MAP,
|
|
WRITE_SCOPES_MAP,
|
|
EDIT_SCOPES_MAP,
|
|
FULL_SCOPES_MAP,
|
|
)
|
|
for scope, description in scope_map.items()
|
|
},
|
|
)
|
|
|
|
# Using an OIDC authorization code flow
|
|
config = Config(
|
|
environ={
|
|
"OIDC_ENABLED": str(OIDC_ENABLED),
|
|
"OIDC_PROVIDER": OIDC_PROVIDER,
|
|
"OIDC_CLIENT_ID": OIDC_CLIENT_ID,
|
|
"OIDC_CLIENT_SECRET": OIDC_CLIENT_SECRET,
|
|
"OIDC_REDIRECT_URI": OIDC_REDIRECT_URI,
|
|
"OIDC_SERVER_APPLICATION_URL": OIDC_SERVER_APPLICATION_URL,
|
|
}
|
|
)
|
|
oauth = OAuth(config=config)
|
|
oauth.register(
|
|
name="openid",
|
|
client_id=config.get("OIDC_CLIENT_ID"),
|
|
client_secret=config.get("OIDC_CLIENT_SECRET"),
|
|
server_metadata_url=OIDC_SERVER_METADATA_URL
|
|
or get_well_known_url(config.get("OIDC_SERVER_APPLICATION_URL"), external=True),
|
|
client_kwargs={
|
|
"scope": f"openid profile email {OIDC_CLAIM_ROLES}".strip(),
|
|
"verify": build_ca_ssl_context(OIDC_TLS_CACERTFILE),
|
|
# Authlib only derives a code_verifier when code_challenge_method is set,
|
|
# so providers that mandate PKCE refuse the code without it.
|
|
"code_challenge_method": "S256",
|
|
},
|
|
)
|
|
|
|
|
|
def _raise_auth_error(request: Request) -> None:
|
|
"""Raise the status matching the caller's auth state.
|
|
|
|
401 tells the client it has no valid credentials (so it can redirect to
|
|
login), while 403 means the authenticated user lacks the required scope
|
|
(an inline permission error, not a session problem).
|
|
"""
|
|
if not request.user.is_authenticated:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")
|
|
|
|
|
|
def _requires_scopes(
|
|
scopes: list[Scope],
|
|
) -> Callable[[DecoratedCallable], DecoratedCallable]:
|
|
"""Scope guard mirroring starlette's `requires`, with a 401/403 split."""
|
|
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable:
|
|
sig = inspect.signature(func)
|
|
idx = next(
|
|
(
|
|
i
|
|
for i, parameter in enumerate(sig.parameters.values())
|
|
if parameter.name == "request"
|
|
),
|
|
None,
|
|
)
|
|
if idx is None:
|
|
raise Exception(f'No "request" argument on function "{func}"')
|
|
|
|
if is_async_callable(func):
|
|
|
|
@functools.wraps(func)
|
|
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
request = kwargs.get("request", args[idx] if idx < len(args) else None)
|
|
assert isinstance(request, Request)
|
|
if not has_required_scope(request, scopes):
|
|
_raise_auth_error(request)
|
|
return await func(*args, **kwargs)
|
|
|
|
return async_wrapper # type: ignore[return-value]
|
|
|
|
@functools.wraps(func)
|
|
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
request = kwargs.get("request", args[idx] if idx < len(args) else None)
|
|
assert isinstance(request, Request)
|
|
if not has_required_scope(request, scopes):
|
|
_raise_auth_error(request)
|
|
return func(*args, **kwargs)
|
|
|
|
return sync_wrapper # type: ignore[return-value]
|
|
|
|
return decorator
|
|
|
|
|
|
def protected_route(
|
|
method: Callable[..., Callable[[DecoratedCallable], DecoratedCallable]],
|
|
path: str,
|
|
scopes: list[Scope] | None = None,
|
|
**kwargs: Any,
|
|
) -> Callable[[DecoratedCallable], DecoratedCallable]:
|
|
def decorator(func: DecoratedCallable) -> DecoratedCallable:
|
|
fn = _requires_scopes(scopes or [])(func)
|
|
return method(
|
|
path,
|
|
dependencies=[
|
|
Security(
|
|
dependency=oauth2_password_bearer,
|
|
scopes=scopes or [],
|
|
),
|
|
Security(dependency=HTTPBasic(auto_error=False)),
|
|
],
|
|
**kwargs,
|
|
)(fn)
|
|
|
|
return decorator
|