1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386"""``/api/v1/auth/*`` router.
Always exposes ``GET /auth/config`` so the SPA can decide which login UI to
render (or whether to render one at all). The remaining routes are gated on
``settings.auth_mode != "none"``: when auth is off they 404 like any unknown
path, and the SPA's ``AuthProvider`` never calls them anyway.
Mode-conditional routes:
- ``POST /auth/login`` โ basic mode only.
- ``GET /auth/login/{provider_id}`` โ OIDC kickoff.
- ``GET /auth/callback/{provider_id}`` โ OIDC redirect-back.
- ``POST /auth/logout`` and ``GET /auth/me`` โ both basic and OIDC.
"""
from __future__ import annotations
import logging
from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
from fastapi.responses import RedirectResponse
from itsdangerous import BadSignature, SignatureExpired
from pydantic import BaseModel, Field
from ..config import settings
from . import audit, csrf
from .base import AuthMode, Identity
from .cookies import (
OAUTH_TX_COOKIE_NAME,
OAUTH_TX_TTL_SECONDS,
clear_oauth_tx_cookie,
clear_session_cookies,
set_oauth_tx_cookie,
set_session_cookies,
)
from .dependencies import require_identity
from .factory import get_auth_provider, get_auth_providers
from .providers.basic_provider import BasicAuthProvider, InvalidCredentialsError
from .providers.oidc_provider import OIDCAuthProvider, OIDCError
from .session import make_tx_serializer
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
# --- Schemas -----------------------------------------------------------------
class _ProviderInfo(BaseModel):
id: str
display_name: str
login_url: str
class AuthConfigResponse(BaseModel):
"""What the SPA fetches on mount to decide its login UI."""
mode: AuthMode
providers: list[_ProviderInfo] = Field(default_factory=list)
class BasicLoginInput(BaseModel):
username: str = Field(min_length=1, max_length=200)
password: str = Field(min_length=1, max_length=200)
class IdentityResponse(BaseModel):
"""Trimmed projection of :class:`Identity` for the SPA."""
sub: str
email: str | None = None
name: str | None = None
provider_id: str
expires_at: str
@classmethod
def from_identity(cls, identity: Identity) -> IdentityResponse:
return cls(
sub=identity.sub,
email=identity.email,
name=identity.name,
provider_id=identity.provider_id,
expires_at=identity.expires_at.isoformat(),
)
class LogoutResponse(BaseModel):
logout_url: str | None = None
# --- Helpers -----------------------------------------------------------------
def _client_ip(request: Request) -> str | None:
# Match APIKeyAuthMiddleware's client-IP shape so log fields are uniform.
forwarded = request.headers.get("X-Forwarded-For")
if forwarded:
return forwarded.split(",", 1)[0].strip()
return request.headers.get("X-Real-IP") or (request.client.host if request.client else None)
def _enforce_csrf(request: Request) -> None:
"""For state-changing /auth/* endpoints under basic+oidc.
OIDC's callback uses the OAuth ``state`` parameter for CSRF (handled in
PR2 inside the OIDC provider); this guard covers ``POST /auth/login`` and
``POST /auth/logout``, which set/clear cookies on a same-origin POST.
Login is special: the user has no session cookie *yet*, so they can't
have a CSRF cookie to echo. We therefore require an ``Origin`` header
that matches our own scheme+host instead โ browsers send Origin on
cross-site POSTs reliably and a foreign site's Origin would fail this
check.
"""
origin = request.headers.get("Origin")
if origin is None:
# Same-origin form posts from many browsers omit Origin on
# cross-site form-submissions; treat absence as suspicious unless
# the request comes with Sec-Fetch-Site: same-origin (browser-controlled,
# unforgeable from page scripts).
if request.headers.get("Sec-Fetch-Site") != "same-origin":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="missing or non-same-origin request"
)
return
expected = f"{request.url.scheme}://{request.url.netloc}"
if origin != expected:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail="cross-origin request rejected"
)
# --- Routes ------------------------------------------------------------------
@router.get("/config", response_model=AuthConfigResponse)
async def get_config(request: Request) -> AuthConfigResponse:
"""Always public. SPA polls on mount to decide whether to render login."""
mode = AuthMode(settings.auth_mode)
if mode is AuthMode.NONE:
return AuthConfigResponse(mode=mode, providers=[])
providers = get_auth_providers()
return AuthConfigResponse(
mode=mode,
providers=[
_ProviderInfo(
id=p.id,
display_name=p.display_name,
login_url=await p.login_url(request=request, next_path="/"),
)
for p in providers.values()
],
)
@router.post("/login", response_model=IdentityResponse)
async def basic_login(
payload: BasicLoginInput,
request: Request,
response: Response,
) -> IdentityResponse:
"""Basic-mode login. JSON body, sets session + CSRF cookies on success."""
if AuthMode(settings.auth_mode) is not AuthMode.BASIC:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="basic login disabled")
_enforce_csrf(request)
provider = get_auth_provider()
if not isinstance(provider, BasicAuthProvider):
# Shouldn't happen โ factory + AuthMode check above guarantee this โ
# but raise loudly rather than silently.
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="provider mismatch")
try:
identity = provider.authenticate(username=payload.username, password=payload.password)
except InvalidCredentialsError:
audit.emit(
"login_failure",
provider_id="basic",
reason="invalid_password",
client_ip=_client_ip(request),
)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid credentials"
) from None
set_session_cookies(response=response, identity=identity)
audit.emit("login_success", provider_id="basic", sub=identity.sub, client_ip=_client_ip(request))
return IdentityResponse.from_identity(identity)
@router.post("/logout", response_model=LogoutResponse)
async def logout(
request: Request,
response: Response,
identity: Identity = Depends(require_identity),
) -> LogoutResponse:
"""Clear cookies and return any IdP-side logout URL.
CSRF is enforced via the cookie companion (``X-CSRF-Token`` header).
The ``X-Forwarded-Proto``/host check in :func:`_enforce_csrf` belt-and-
braces against missing-header browsers.
"""
_enforce_csrf(request)
secret = settings.auth_secret_key
if secret is None:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="not configured")
# CSRF: the cookie value travels in the request; the header echoes the
# HMAC computed from it. Recompute and compare.
session_value = request.cookies.get(settings.auth_session_cookie_name) or ""
header = request.headers.get("X-CSRF-Token")
if not csrf.verify(session_value=session_value, csrf_header=header, secret_key=secret.get_secret_value()):
audit.emit(
"login_failure",
provider_id=identity.provider_id,
sub=identity.sub,
reason="csrf_mismatch",
client_ip=_client_ip(request),
)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="csrf mismatch")
provider = get_auth_provider()
logout_url = await provider.logout_url(identity)
clear_session_cookies(response)
audit.emit("logout", provider_id=identity.provider_id, sub=identity.sub, client_ip=_client_ip(request))
return LogoutResponse(logout_url=logout_url)
@router.get("/me", response_model=IdentityResponse)
async def get_me(
identity: Identity = Depends(require_identity),
) -> IdentityResponse:
"""Return the current identity, 401 if anonymous. SPA hits this on mount."""
return IdentityResponse.from_identity(identity)
# --- OIDC kickoff + callback -------------------------------------------------
def _redirect_uri_for(request: Request, provider_id: str) -> str:
"""Build the absolute redirect_uri the IdP calls back to.
Derived from the request's scheme + host so the same code works in
local dev (``http://localhost:8080``) and behind an HTTPS-terminating
ALB. Operators must register *exactly* this URL with the IdP โ there
is no wildcard support in OAuth.
"""
return f"{request.url.scheme}://{request.url.netloc}/api/v1/auth/callback/{provider_id}"
def _safe_next_path(raw: str | None) -> str:
"""Sanitise the ``next`` query param so we only redirect to in-app
paths. Anything starting with a scheme or ``//`` is rejected to prevent
open-redirect abuse.
"""
if not raw:
return settings.auth_post_login_redirect
if raw.startswith("//") or "://" in raw:
return settings.auth_post_login_redirect
if not raw.startswith("/"):
return settings.auth_post_login_redirect
return raw
def _get_oidc_provider(provider_id: str) -> OIDCAuthProvider:
"""Look up an OIDC provider by id, 404 if not configured for this mode."""
if AuthMode(settings.auth_mode) is not AuthMode.OIDC:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="oidc not enabled")
provider = get_auth_providers().get(provider_id)
if not isinstance(provider, OIDCAuthProvider):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="unknown provider")
return provider
@router.get("/login/{provider_id}")
async def oidc_login(provider_id: str, request: Request, next: str = "") -> RedirectResponse:
"""OIDC kickoff. Mints state/nonce/PKCE, sets the tx cookie, redirects to IdP."""
provider = _get_oidc_provider(provider_id)
secret = settings.auth_secret_key
if secret is None:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="not configured")
next_path = _safe_next_path(next)
redirect_uri = _redirect_uri_for(request, provider_id)
auth_url, tx_payload = await provider.begin_authorization(
redirect_uri=redirect_uri, next_path=next_path
)
serializer = make_tx_serializer(secret.get_secret_value())
signed = serializer.dumps(tx_payload)
response = RedirectResponse(url=auth_url, status_code=status.HTTP_302_FOUND)
set_oauth_tx_cookie(response, signed)
return response
@router.get("/callback/{provider_id}")
async def oidc_callback(
provider_id: str,
request: Request,
response: Response,
) -> RedirectResponse:
"""OIDC redirect-back. Validates state, exchanges code, mints session."""
provider = _get_oidc_provider(provider_id)
secret = settings.auth_secret_key
if secret is None:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="not configured")
code = request.query_params.get("code")
state_from_query = request.query_params.get("state")
error_from_idp = request.query_params.get("error")
if error_from_idp:
# IdP refused the user (consent declined, account disabled, โฆ).
# Don't tell the SPA more than the category; the IdP already
# showed the user the specifics.
audit.emit(
"login_failure",
provider_id=provider_id,
reason="provider_error",
client_ip=_client_ip(request),
)
redirect = RedirectResponse(url="/login", status_code=status.HTTP_302_FOUND)
clear_oauth_tx_cookie(redirect)
return redirect
if not code or not state_from_query:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="callback missing code or state",
)
tx_cookie = request.cookies.get(OAUTH_TX_COOKIE_NAME)
if not tx_cookie:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="missing oauth tx cookie",
)
serializer = make_tx_serializer(secret.get_secret_value())
try:
tx_payload = serializer.loads(tx_cookie, max_age=OAUTH_TX_TTL_SECONDS)
except SignatureExpired as exc:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="oauth tx expired") from exc
except BadSignature as exc:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="oauth tx invalid") from exc
if not isinstance(tx_payload, dict):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="oauth tx malformed")
redirect_uri = _redirect_uri_for(request, provider_id)
try:
result = await provider.complete_authorization(
code=code,
state_from_query=state_from_query,
tx_payload={k: str(v) for k, v in tx_payload.items()},
redirect_uri=redirect_uri,
)
except OIDCError as exc:
audit.emit(
"login_failure",
provider_id=provider_id,
reason="provider_error",
client_ip=_client_ip(request),
)
logger.warning("OIDC callback failed for provider %s: %s", provider_id, exc)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="authentication failed"
) from exc
redirect = RedirectResponse(url=result.next_path, status_code=status.HTTP_302_FOUND)
set_session_cookies(response=redirect, identity=result.identity)
clear_oauth_tx_cookie(redirect)
audit.emit(
"login_success",
provider_id=provider_id,
sub=result.identity.sub,
client_ip=_client_ip(request),
)
return redirect