mfa.py 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. """2FA (TOTP + Email OTP) and OIDC authentication routes.
  2. Security model
  3. --------------
  4. * Pre-auth tokens : secrets.token_urlsafe(32) stored in-memory with a 5-minute TTL.
  5. They are single-use and do NOT grant access to any protected resource.
  6. * TOTP codes : verified with pyotp (30-second window, ±1 step tolerance).
  7. * Email OTP codes : 6-digit numeric, hashed with pbkdf2_sha256, 10-minute TTL,
  8. max 5 failed attempts per code before invalidation.
  9. * Backup codes : 10 × 8-char alphanumeric codes, each stored as pbkdf2_sha256 hash,
  10. single-use.
  11. * OIDC state : secrets.token_urlsafe(32) bound to provider_id + nonce, 10-minute TTL.
  12. * OIDC exchange : secrets.token_urlsafe(32), 2-minute TTL, single-use.
  13. * Rate limiting : max 5 failed 2FA verification attempts per user within 15 minutes.
  14. """
  15. from __future__ import annotations
  16. import base64
  17. import hashlib
  18. import io
  19. import logging
  20. import os
  21. import re
  22. import secrets
  23. import string
  24. import urllib.parse
  25. from datetime import datetime, timedelta, timezone
  26. import httpx
  27. import jwt
  28. import pyotp
  29. from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Request, Response, status
  30. from fastapi.responses import RedirectResponse
  31. from jwt import PyJWKClient
  32. from passlib.context import CryptContext
  33. from sqlalchemy import delete, select
  34. from sqlalchemy.ext.asyncio import AsyncSession
  35. from sqlalchemy.orm import selectinload, undefer
  36. from backend.app.api.routes._oidc_helpers import assert_safe_public_https_url
  37. from backend.app.api.routes.settings import get_setting, set_setting
  38. from backend.app.core.auth import (
  39. ACCESS_TOKEN_EXPIRE_MINUTES,
  40. RequirePermissionIfAuthEnabled,
  41. create_access_token,
  42. get_current_active_user,
  43. get_user_by_email,
  44. get_user_by_username,
  45. is_auth_enabled,
  46. verify_password,
  47. )
  48. from backend.app.core.database import get_db
  49. from backend.app.core.permissions import Permission
  50. from backend.app.models.auth_ephemeral import AuthEphemeralToken, AuthRateLimitEvent, EventType, TokenType
  51. from backend.app.models.group import Group
  52. from backend.app.models.oidc_provider import OIDCProvider, UserOIDCLink
  53. from backend.app.models.user import User
  54. from backend.app.models.user_otp_code import UserOTPCode
  55. from backend.app.models.user_totp import UserTOTP
  56. from backend.app.schemas.auth import (
  57. AUTO_LINK_REQUIREMENTS_ERROR,
  58. AdminDisable2FARequest,
  59. BackupCodesResponse,
  60. EmailOTPDisableRequest,
  61. EmailOTPEnableConfirmRequest,
  62. EmailOTPSendRequest,
  63. GroupBrief,
  64. LoginResponse,
  65. OIDCAuthorizeResponse,
  66. OIDCExchangeRequest,
  67. OIDCLinkResponse,
  68. OIDCProviderCreate,
  69. OIDCProviderResponse,
  70. OIDCProviderUpdate,
  71. TOTPDisableRequest,
  72. TOTPEnableRequest,
  73. TOTPEnableResponse,
  74. TOTPSetupRequest,
  75. TOTPSetupResponse,
  76. TwoFAStatusResponse,
  77. TwoFAVerifyRequest,
  78. TwoFAVerifyResponse,
  79. UserResponse,
  80. )
  81. from backend.app.services.email_service import get_smtp_settings, send_email
  82. from backend.app.services.oidc_icon import OIDCIconError, fetch_icon
  83. logger = logging.getLogger(__name__)
  84. def _redact_url_for_log(url: str) -> str:
  85. """Return ``scheme://host/path`` with query string and fragment stripped.
  86. Admin-supplied icon URLs are usually CDN paths, but nothing stops an
  87. admin from pasting a presigned URL whose query string carries an
  88. ``X-Amz-Signature`` / OAuth token / etc. Operators need a forensic
  89. trail without those secrets ending up in log files.
  90. """
  91. try:
  92. parsed = urllib.parse.urlparse(url)
  93. except ValueError:
  94. return "<unparseable>"
  95. netloc = parsed.netloc or "<no-host>"
  96. return f"{parsed.scheme}://{netloc}{parsed.path}"
  97. async def _fetch_icon_or_400(icon_url: str) -> tuple[bytes, str, str]:
  98. """Validate URL + fetch icon, mapping any failure to HTTPException(400).
  99. Centralises the SSRF guard + fetcher invocation so create/update/refresh
  100. all behave identically — admin always gets a 400 with a precise reason,
  101. never a 500 / opaque server error.
  102. Both failure paths log at WARNING so operators have a forensic trail
  103. later — without these log lines the admin's UI toast was the only
  104. record of the failure (#1333 review).
  105. """
  106. try:
  107. assert_safe_public_https_url(icon_url)
  108. except ValueError as exc:
  109. logger.warning("OIDC icon URL rejected by SSRF guard: url=%s reason=%s", _redact_url_for_log(icon_url), exc)
  110. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
  111. try:
  112. return await fetch_icon(icon_url)
  113. except OIDCIconError as exc:
  114. logger.warning("OIDC icon fetch failed: url=%s reason=%s", _redact_url_for_log(icon_url), exc)
  115. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
  116. def _build_provider_response(provider: OIDCProvider) -> OIDCProviderResponse:
  117. """Build OIDCProviderResponse via ``from_attributes``. The required
  118. ``has_icon`` field is supplied by ``OIDCProvider.has_icon`` (a property
  119. reading the non-deferred ``icon_content_type`` column)."""
  120. return OIDCProviderResponse.model_validate(provider)
  121. def _etag_matches(if_none_match: str | None, etag_raw: str | None) -> bool:
  122. """RFC 7232 §3.2 If-None-Match comparison.
  123. Supports:
  124. * ``*`` wildcard — matches any current representation when the resource
  125. exists (and it does here; we wouldn't have an etag otherwise).
  126. * Multiple comma-separated tokens.
  127. * Weak-validator prefix ``W/`` (RFC 7232 §2.3) — accepted on GET since
  128. cached representations of a static byte-blob are byte-identical.
  129. Returns False on missing header or missing stored etag.
  130. """
  131. if not if_none_match or not etag_raw:
  132. return False
  133. quoted = f'"{etag_raw}"'
  134. tokens = [t.strip() for t in if_none_match.split(",")]
  135. if "*" in tokens:
  136. return True
  137. return any(tok.removeprefix("W/") == quoted for tok in tokens)
  138. def _as_utc(dt: datetime) -> datetime:
  139. """Return *dt* with UTC timezone attached.
  140. SQLite/aiosqlite strips timezone info when reading DateTime(timezone=True)
  141. columns back – the stored value is always UTC, so we just re-attach the
  142. info when doing Python-level comparisons.
  143. """
  144. return dt if dt.tzinfo is not None else dt.replace(tzinfo=timezone.utc)
  145. # ---------------------------------------------------------------------------
  146. # Passlib context (same scheme as auth.py)
  147. # ---------------------------------------------------------------------------
  148. pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
  149. # ---------------------------------------------------------------------------
  150. # TTL / rate-limit constants
  151. # ---------------------------------------------------------------------------
  152. MAX_2FA_ATTEMPTS = 5
  153. MAX_LOGIN_ATTEMPTS = 10
  154. LOCKOUT_WINDOW = timedelta(minutes=15)
  155. MAX_EMAIL_OTP_SENDS = 3
  156. EMAIL_OTP_SEND_WINDOW = timedelta(minutes=10)
  157. PRE_AUTH_TOKEN_TTL = timedelta(minutes=5)
  158. OIDC_STATE_TTL = timedelta(minutes=10)
  159. OIDC_EXCHANGE_TTL = timedelta(minutes=2)
  160. # ---------------------------------------------------------------------------
  161. # Router
  162. # ---------------------------------------------------------------------------
  163. router = APIRouter(prefix="/auth", tags=["2fa", "oidc"])
  164. # ---------------------------------------------------------------------------
  165. # Helper: user response
  166. # ---------------------------------------------------------------------------
  167. def _user_to_response(user: User) -> UserResponse:
  168. return UserResponse(
  169. id=user.id,
  170. username=user.username,
  171. email=user.email,
  172. role=user.role,
  173. is_active=user.is_active,
  174. is_admin=user.is_admin,
  175. groups=[GroupBrief(id=g.id, name=g.name) for g in user.groups],
  176. permissions=sorted(user.get_permissions()),
  177. created_at=user.created_at.isoformat(),
  178. )
  179. # ---------------------------------------------------------------------------
  180. # Helper: QR code generation
  181. # ---------------------------------------------------------------------------
  182. def _generate_totp_qr_b64(provisioning_uri: str) -> str:
  183. """Generate a base64-encoded PNG QR code for the given TOTP provisioning URI."""
  184. import qrcode # type: ignore
  185. qr = qrcode.QRCode(box_size=6, border=2)
  186. qr.add_data(provisioning_uri)
  187. qr.make(fit=True)
  188. img = qr.make_image(fill_color="black", back_color="white")
  189. buf = io.BytesIO()
  190. img.save(buf, format="PNG")
  191. return base64.b64encode(buf.getvalue()).decode()
  192. # ---------------------------------------------------------------------------
  193. # Helper: backup code generation
  194. # ---------------------------------------------------------------------------
  195. def _generate_backup_codes() -> tuple[list[str], list[str]]:
  196. """Return (plain_codes, hashed_codes) — 10 codes of 8 alphanumeric chars each."""
  197. alphabet = string.ascii_uppercase + string.digits
  198. plain = ["".join(secrets.choice(alphabet) for _ in range(8)) for _ in range(10)]
  199. hashed = [pwd_context.hash(c) for c in plain]
  200. return plain, hashed
  201. # ---------------------------------------------------------------------------
  202. # DB-backed pre-auth token helpers
  203. # ---------------------------------------------------------------------------
  204. async def create_pre_auth_token(db: AsyncSession, username: str, challenge_id: str | None = None) -> str:
  205. """Create a single-use pre-auth token stored in the DB.
  206. Pass ``challenge_id`` (from the HttpOnly 2fa_challenge cookie) to bind the
  207. token to the originating browser session. The same value must be present as
  208. a cookie on every subsequent call that consumes this token.
  209. """
  210. now = datetime.now(timezone.utc)
  211. # Prune expired tokens opportunistically (keep table small)
  212. await db.execute(
  213. delete(AuthEphemeralToken).where(
  214. AuthEphemeralToken.token_type == TokenType.PRE_AUTH,
  215. AuthEphemeralToken.expires_at < now,
  216. )
  217. )
  218. token = secrets.token_urlsafe(32)
  219. db.add(
  220. AuthEphemeralToken(
  221. token=token,
  222. token_type=TokenType.PRE_AUTH,
  223. username=username,
  224. challenge_id=challenge_id,
  225. expires_at=now + PRE_AUTH_TOKEN_TTL,
  226. )
  227. )
  228. await db.commit()
  229. return token
  230. async def consume_pre_auth_token(db: AsyncSession, token: str, challenge_id: str | None = None) -> str | None:
  231. """Atomically validate and consume a pre-auth token. Returns username or None.
  232. Uses DELETE...RETURNING so two concurrent requests with the same token cannot
  233. both succeed — only the first DELETE finds the row.
  234. M5: When challenge_id is provided, also enforces the cookie-binding constraint
  235. so a stolen token cannot be replayed from a different browser session.
  236. """
  237. now = datetime.now(timezone.utc)
  238. result = await db.execute(
  239. delete(AuthEphemeralToken)
  240. .where(
  241. AuthEphemeralToken.token == token,
  242. AuthEphemeralToken.token_type == TokenType.PRE_AUTH,
  243. AuthEphemeralToken.expires_at > now,
  244. )
  245. .returning(AuthEphemeralToken.username, AuthEphemeralToken.challenge_id)
  246. )
  247. row = result.one_or_none()
  248. if row is None:
  249. return None
  250. username, stored_challenge_id = row
  251. # Enforce client binding: if the token was issued with a challenge_id,
  252. # the caller must supply the matching value.
  253. if stored_challenge_id is not None and stored_challenge_id != challenge_id:
  254. await db.rollback()
  255. return None
  256. await db.commit()
  257. return username
  258. async def peek_pre_auth_token(db: AsyncSession, token: str, challenge_id: str | None = None) -> str | None:
  259. """Validate a pre-auth token and return the username WITHOUT consuming it.
  260. When the stored token has a ``challenge_id`` (client-binding cookie), the
  261. caller must supply the matching value. A mismatch is treated as an invalid
  262. token — no information leakage about whether the token itself exists.
  263. """
  264. now = datetime.now(timezone.utc)
  265. result = await db.execute(
  266. select(AuthEphemeralToken).where(
  267. AuthEphemeralToken.token == token,
  268. AuthEphemeralToken.token_type == TokenType.PRE_AUTH,
  269. AuthEphemeralToken.expires_at > now,
  270. )
  271. )
  272. eph = result.scalar_one_or_none()
  273. if eph is None:
  274. return None
  275. # Enforce client binding: if the token was issued with a challenge_id the
  276. # cookie must match. Treat a mismatch as if the token doesn't exist.
  277. if eph.challenge_id is not None and eph.challenge_id != challenge_id:
  278. return None
  279. return eph.username
  280. # ---------------------------------------------------------------------------
  281. # DB-backed rate-limiting helpers
  282. # ---------------------------------------------------------------------------
  283. async def check_rate_limit(
  284. db: AsyncSession,
  285. username: str,
  286. event_type: str = EventType.TWO_FA_ATTEMPT,
  287. max_attempts: int = MAX_2FA_ATTEMPTS,
  288. ) -> None:
  289. """Raise HTTP 429 if the user has exceeded the failed attempt limit.
  290. The username is normalised to lower-case so case-variant attempts
  291. (which all resolve to the same user) share the same rate-limit bucket.
  292. L-2: Known TOCTOU — the SELECT (count) and the subsequent INSERT
  293. (record_failed_attempt) are not atomic. Two concurrent requests can both
  294. read a count below the threshold and both proceed. This is an inherent
  295. trade-off of the event-log rate-limit pattern: fixing it would require
  296. a serialising lock (SELECT FOR UPDATE on a dedicated counter row), which
  297. adds contention and is not worth it for a soft rate-limit whose window is
  298. already measured in minutes. In practice the race window is microseconds
  299. and the limit can be slightly exceeded only under precise concurrent timing.
  300. """
  301. username_key = username.lower()
  302. now = datetime.now(timezone.utc)
  303. cutoff = now - LOCKOUT_WINDOW
  304. result = await db.execute(
  305. select(AuthRateLimitEvent).where(
  306. AuthRateLimitEvent.username == username_key,
  307. AuthRateLimitEvent.event_type == event_type,
  308. AuthRateLimitEvent.occurred_at > cutoff,
  309. )
  310. )
  311. recent_count = len(result.scalars().all())
  312. if recent_count >= max_attempts:
  313. raise HTTPException(
  314. status_code=status.HTTP_429_TOO_MANY_REQUESTS,
  315. detail="Too many failed attempts. Please try again later.",
  316. )
  317. async def record_failed_attempt(db: AsyncSession, username: str, event_type: str = EventType.TWO_FA_ATTEMPT) -> None:
  318. """Record a failed attempt for rate-limiting purposes."""
  319. db.add(AuthRateLimitEvent(username=username.lower(), event_type=event_type))
  320. await db.commit()
  321. async def clear_failed_attempts(db: AsyncSession, username: str, event_type: str = EventType.TWO_FA_ATTEMPT) -> None:
  322. """Delete all recorded failed attempts for a user on successful verification."""
  323. await db.execute(
  324. delete(AuthRateLimitEvent).where(
  325. AuthRateLimitEvent.username == username.lower(),
  326. AuthRateLimitEvent.event_type == event_type,
  327. )
  328. )
  329. await db.commit()
  330. async def check_email_otp_send_rate(db: AsyncSession, username: str) -> None:
  331. """Raise HTTP 429 if the user has requested too many OTP emails recently.
  332. I1: This function only *checks* the limit. The caller is responsible for
  333. recording the slot via ``record_email_otp_send`` **after** the email has
  334. been sent successfully. This prevents failed sends from consuming a slot
  335. (wasting the user's quota) and makes it impossible to farm rate-limit events
  336. without actually triggering a send.
  337. """
  338. username_key = username.lower()
  339. now = datetime.now(timezone.utc)
  340. cutoff = now - EMAIL_OTP_SEND_WINDOW
  341. result = await db.execute(
  342. select(AuthRateLimitEvent).where(
  343. AuthRateLimitEvent.username == username_key,
  344. AuthRateLimitEvent.event_type == EventType.EMAIL_SEND,
  345. AuthRateLimitEvent.occurred_at > cutoff,
  346. )
  347. )
  348. recent_count = len(result.scalars().all())
  349. if recent_count >= MAX_EMAIL_OTP_SENDS:
  350. raise HTTPException(
  351. status_code=status.HTTP_429_TOO_MANY_REQUESTS,
  352. detail=f"Too many OTP email requests. Please wait {EMAIL_OTP_SEND_WINDOW.seconds // 60} minutes.",
  353. )
  354. async def record_email_otp_send(db: AsyncSession, username: str) -> None:
  355. """Record a successful OTP email send for rate-limiting purposes (I1).
  356. Must be called *after* the email has been sent successfully so that failed
  357. sends do not consume a slot from the user's quota.
  358. """
  359. db.add(AuthRateLimitEvent(username=username.lower(), event_type=EventType.EMAIL_SEND))
  360. await db.commit()
  361. # ---------------------------------------------------------------------------
  362. # TOTP replay-protection helper
  363. # ---------------------------------------------------------------------------
  364. def _assert_totp_not_replayed(totp_obj: pyotp.TOTP, totp_record: UserTOTP, code: str) -> None:
  365. """Raise HTTP 400 if this TOTP code was already accepted in its time window.
  366. M3 fix: store the counter of the *accepted* code rather than the current
  367. wall-clock counter. With valid_window=1, pyotp accepts codes from the
  368. previous 30-second step. Using timecode(now) would store the wrong counter
  369. when the previous-window code is accepted, allowing immediate replay.
  370. """
  371. # Determine which time-step the accepted code belongs to.
  372. now = datetime.now(timezone.utc)
  373. accepted_counter: int | None = None
  374. for offset in (0, -1): # current window first, then previous
  375. candidate_time = now.timestamp() + offset * totp_obj.interval
  376. candidate_counter = totp_obj.timecode(datetime.fromtimestamp(candidate_time, tz=timezone.utc))
  377. if totp_obj.at(candidate_counter) == code:
  378. accepted_counter = candidate_counter
  379. break
  380. if accepted_counter is None:
  381. accepted_counter = totp_obj.timecode(now) # fallback (should not happen after verify())
  382. totp_record.accept_counter(accepted_counter)
  383. # ---------------------------------------------------------------------------
  384. # OIDC helpers
  385. # ---------------------------------------------------------------------------
  386. _EMAIL_SHAPE_RE = re.compile(r"[^\s@]+@[^\s@]+\.[^\s@]+")
  387. def _is_valid_email_shaped(value: str | None) -> bool:
  388. # SEC-2: shape check for non-standard claims (upn, preferred_username).
  389. # Requires local@domain.tld — rejects "@", "x@", "@domain", "x@nodot".
  390. if not value or len(value) > 255:
  391. return False
  392. return _EMAIL_SHAPE_RE.fullmatch(value) is not None
  393. def _enforce_auto_link_safety(provider: OIDCProvider) -> None:
  394. """Raise HTTP 422 if auto_link_existing_accounts is on with an unsafe combined state.
  395. SEC-1: only Fall B (email_claim='email' + require_email_verified=False) is unsafe —
  396. an attacker-controlled IdP could present an unverified email that matches a local account.
  397. Fall C (custom claim) never performs an email_verified check, so auto_link is safe there.
  398. Called after ORM construction (create) and after the setattr loop (update).
  399. """
  400. if provider.auto_link_existing_accounts and provider.email_claim == "email" and not provider.require_email_verified:
  401. raise HTTPException(
  402. status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
  403. detail=AUTO_LINK_REQUIREMENTS_ERROR,
  404. )
  405. def _resolve_provider_email(provider: OIDCProvider, claims: dict, provider_sub: str) -> str | None:
  406. """Extract and normalise the email address from OIDC ID-token claims.
  407. Implements three resolution paths (Fall A/B/C):
  408. Fall C — custom email_claim (!= "email"): shape-check only, no email_verified gate.
  409. Recommended for Azure Entra ID (preferred_username or upn).
  410. Fall A — email_claim="email" + require_email_verified=True: strict, email_verified must be True.
  411. Fall B — email_claim="email" + require_email_verified=False: permissive, explicit False drops email.
  412. Returns a lowercase-stripped email string, or None when the claim is absent/invalid.
  413. """
  414. provider_id = provider.id
  415. raw_claim_value = claims.get(provider.email_claim)
  416. if raw_claim_value is not None and not isinstance(raw_claim_value, str):
  417. # TYPE-GUARD: non-string claim (e.g. list, int) would raise AttributeError on .lower().
  418. logger.warning(
  419. "OIDC provider %d: email_claim %r has unexpected type %s for sub=%r, ignoring",
  420. provider_id,
  421. provider.email_claim,
  422. type(raw_claim_value).__name__,
  423. provider_sub,
  424. )
  425. raw_claim_value = None
  426. raw_email: str | None = raw_claim_value.lower().strip() if raw_claim_value else None
  427. if provider.email_claim != "email":
  428. # Fall C: custom claim (preferred_username, upn, …) — no email_verified check.
  429. # SEC-2: _is_valid_email_shaped instead of bare '"@" in value'.
  430. # Recommended for Azure Entra ID: set email_claim="preferred_username" or "upn".
  431. if raw_email and _is_valid_email_shaped(raw_email):
  432. return raw_email
  433. if raw_email:
  434. logger.warning(
  435. "OIDC provider %d: email_claim %r value failed shape check for sub=%r, ignoring",
  436. provider_id,
  437. provider.email_claim,
  438. provider_sub,
  439. )
  440. return None
  441. email_verified = claims.get("email_verified")
  442. if provider.require_email_verified:
  443. # Fall A: standard C1-Guard — fail closed unless email_verified is True.
  444. # SEC-2: apply shape check to standard email claim — providers may set
  445. # email_verified=True on non-email values (e.g. numeric user IDs).
  446. # SEC-3 normalisation applies; existing mixed-case provider_email records
  447. # were normalised to lowercase by run_migrations at startup.
  448. if raw_email and not _is_valid_email_shaped(raw_email):
  449. logger.warning(
  450. "OIDC provider %d: email claim failed shape check for sub=%r, ignoring",
  451. provider_id,
  452. provider_sub,
  453. )
  454. return None
  455. if email_verified is True:
  456. return raw_email
  457. if raw_email:
  458. logger.info(
  459. "OIDC provider %d: ignoring email for sub=%r because email_verified=%r",
  460. provider_id,
  461. provider_sub,
  462. email_verified,
  463. )
  464. return None
  465. # Fall B: permissive — explicit False drops email, absent/None keeps it.
  466. # Required for Azure Entra ID which never sends email_verified.
  467. # SEC-2: apply shape check before the email_verified=False drop so malformed
  468. # values are rejected regardless of the email_verified claim.
  469. if raw_email and not _is_valid_email_shaped(raw_email):
  470. logger.warning(
  471. "OIDC provider %d: email claim failed shape check for sub=%r, ignoring",
  472. provider_id,
  473. provider_sub,
  474. )
  475. return None
  476. if email_verified is False:
  477. return None
  478. if email_verified is not True:
  479. # SEC-5: log only when the permissive path actually fires (ev absent/None),
  480. # not on every successful login.
  481. logger.info(
  482. "OIDC provider %r (%d): accepting email for sub=%r without email_verified claim (permissive mode)",
  483. provider.name,
  484. provider.id,
  485. provider_sub,
  486. )
  487. return raw_email
  488. # ---------------------------------------------------------------------------
  489. # Settings helpers (email 2FA flag)
  490. # ---------------------------------------------------------------------------
  491. async def _get_email_2fa_enabled(db: AsyncSession, user_id: int) -> bool:
  492. val = await get_setting(db, f"user_{user_id}_email_2fa_enabled")
  493. return val == "true"
  494. async def _set_email_2fa_enabled(db: AsyncSession, user_id: int, enabled: bool) -> None:
  495. await set_setting(db, f"user_{user_id}_email_2fa_enabled", "true" if enabled else "false")
  496. # ===========================================================================
  497. # 2FA Endpoints
  498. # ===========================================================================
  499. @router.get("/2fa/status", response_model=TwoFAStatusResponse)
  500. async def get_2fa_status(
  501. current_user: User = Depends(get_current_active_user),
  502. db: AsyncSession = Depends(get_db),
  503. ) -> TwoFAStatusResponse:
  504. """Return the current 2FA configuration for the authenticated user."""
  505. result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == current_user.id))
  506. totp_record = result.scalar_one_or_none()
  507. totp_enabled = totp_record is not None and totp_record.is_enabled
  508. backup_codes_remaining = len(totp_record.backup_code_hashes) if totp_record else 0
  509. email_otp_enabled = await _get_email_2fa_enabled(db, current_user.id)
  510. return TwoFAStatusResponse(
  511. totp_enabled=totp_enabled,
  512. email_otp_enabled=email_otp_enabled,
  513. backup_codes_remaining=backup_codes_remaining,
  514. )
  515. @router.post("/2fa/totp/setup", response_model=TOTPSetupResponse)
  516. async def setup_totp(
  517. body: TOTPSetupRequest | None = Body(default=None),
  518. current_user: User = Depends(get_current_active_user),
  519. db: AsyncSession = Depends(get_db),
  520. ) -> TOTPSetupResponse:
  521. """Initiate TOTP setup: generates a new secret and QR code.
  522. Creates (or replaces) a pending UserTOTP record with is_enabled=False.
  523. The caller must confirm with POST /auth/2fa/totp/enable.
  524. M-R7-A: If an *active* TOTP is already configured, the caller must supply
  525. the current TOTP code in the request body to confirm intent before the
  526. secret is overwritten (prevents silently locking out the real user).
  527. """
  528. if not await is_auth_enabled(db):
  529. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Authentication is not enabled")
  530. # Upsert a pending TOTP record (is_enabled=False)
  531. existing = (await db.execute(select(UserTOTP).where(UserTOTP.user_id == current_user.id))).scalar_one_or_none()
  532. # M-R7-A: Guard against silent TOTP replacement when one is already active.
  533. if existing and existing.is_enabled:
  534. await check_rate_limit(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  535. supplied_code = (body.code if body else None) or ""
  536. # S4: narrow the RuntimeError catch to ONLY the property access — that
  537. # is the single line that raises on key-loss. The previous wide try
  538. # block also covered record_failed_attempt, clear_failed_attempts,
  539. # and _assert_totp_not_replayed, so a future RuntimeError from any
  540. # of those would have been misreported as "TOTP secret unavailable".
  541. try:
  542. secret_plain = existing.secret
  543. except RuntimeError:
  544. logger.exception("TOTP decryption failed for user_id=%s", current_user.id)
  545. raise HTTPException(
  546. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  547. detail="TOTP secret unavailable",
  548. )
  549. if not pyotp.TOTP(secret_plain).verify(supplied_code, valid_window=1):
  550. await record_failed_attempt(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  551. raise HTTPException(
  552. status_code=status.HTTP_400_BAD_REQUEST,
  553. detail="Current TOTP code required to replace an active authenticator",
  554. )
  555. await clear_failed_attempts(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  556. _assert_totp_not_replayed(pyotp.TOTP(secret_plain), existing, supplied_code)
  557. await db.flush() # L-3: persist last_totp_counter immediately to block replay
  558. secret = pyotp.random_base32()
  559. totp = pyotp.TOTP(secret)
  560. provisioning_uri = totp.provisioning_uri(name=current_user.username, issuer_name="Bambuddy")
  561. qr_b64 = _generate_totp_qr_b64(provisioning_uri)
  562. if existing:
  563. existing.secret = secret
  564. existing.is_enabled = False
  565. existing.backup_code_hashes = []
  566. else:
  567. db.add(UserTOTP(user_id=current_user.id, secret=secret, is_enabled=False))
  568. await db.commit()
  569. return TOTPSetupResponse(secret=secret, qr_code_b64=qr_b64, issuer="Bambuddy")
  570. @router.post("/2fa/totp/enable", response_model=TOTPEnableResponse)
  571. async def enable_totp(
  572. body: TOTPEnableRequest,
  573. current_user: User = Depends(get_current_active_user),
  574. db: AsyncSession = Depends(get_db),
  575. ) -> TOTPEnableResponse:
  576. """Confirm TOTP setup by verifying a code from the authenticator app.
  577. On success, enables TOTP and returns 10 single-use backup codes (shown once).
  578. L-R7-A: Rate-limited to prevent brute-forcing the 6-digit confirmation code.
  579. """
  580. # L-R7-A: Rate-limit the enable step to prevent brute-forcing the 6-digit code.
  581. await check_rate_limit(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  582. result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == current_user.id))
  583. totp_record = result.scalar_one_or_none()
  584. if not totp_record:
  585. raise HTTPException(
  586. status_code=status.HTTP_400_BAD_REQUEST, detail="TOTP setup not initiated. Call /auth/2fa/totp/setup first."
  587. )
  588. try:
  589. totp_verify = pyotp.TOTP(totp_record.secret).verify(body.code, valid_window=1)
  590. except RuntimeError:
  591. logger.exception("TOTP decryption failed for user_id=%s", totp_record.user_id)
  592. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="TOTP secret unavailable")
  593. if not totp_verify:
  594. await record_failed_attempt(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  595. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid TOTP code")
  596. await clear_failed_attempts(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  597. plain_codes, hashed_codes = _generate_backup_codes()
  598. totp_record.is_enabled = True
  599. totp_record.backup_code_hashes = hashed_codes
  600. await db.commit()
  601. return TOTPEnableResponse(
  602. message="TOTP enabled successfully. Store your backup codes in a safe place.",
  603. backup_codes=plain_codes,
  604. )
  605. @router.post("/2fa/totp/disable")
  606. async def disable_totp(
  607. body: TOTPDisableRequest,
  608. current_user: User = Depends(get_current_active_user),
  609. db: AsyncSession = Depends(get_db),
  610. ) -> dict:
  611. """Disable TOTP by verifying a valid TOTP code or a backup code.
  612. I10: Rate-limited to prevent backup-code brute-forcing from a hijacked session.
  613. """
  614. await check_rate_limit(db, current_user.username)
  615. result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == current_user.id))
  616. totp_record = result.scalar_one_or_none()
  617. if not totp_record or not totp_record.is_enabled:
  618. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="TOTP is not enabled")
  619. # Accept either a valid TOTP code or a valid backup code. When the secret
  620. # cannot be decrypted (encryption key lost), fall through to the backup-
  621. # code path so the user can still disable 2FA with their printed codes.
  622. totp_obj: pyotp.TOTP | None = None
  623. code_valid = False
  624. decryption_failed = False
  625. try:
  626. totp_obj = pyotp.TOTP(totp_record.secret)
  627. code_valid = totp_obj.verify(body.code, valid_window=1)
  628. except RuntimeError:
  629. # S3: track that the failure was server-side so we don't penalise
  630. # the user with a fail-counter increment for a problem they can't fix.
  631. decryption_failed = True
  632. logger.exception(
  633. "TOTP decryption failed for user_id=%s — falling through to backup-code check",
  634. totp_record.user_id,
  635. )
  636. if code_valid and totp_obj is not None:
  637. _assert_totp_not_replayed(totp_obj, totp_record, body.code)
  638. await db.flush() # L-3: persist last_totp_counter immediately to block replay
  639. else:
  640. # Check backup codes — always iterate all entries (L-R9-A: no early break
  641. # to avoid timing oracle based on code position in the list).
  642. for hashed in totp_record.backup_code_hashes:
  643. if pwd_context.verify(body.code, hashed):
  644. code_valid = True
  645. if not code_valid:
  646. # S3: skip the fail-counter debit when the cause was a server-side
  647. # decryption failure (key loss / rotation). The user submitted a
  648. # wrong backup code on top of a broken TOTP, but locking them out
  649. # of the recovery path for an admin's mistake is not the right move.
  650. if not decryption_failed:
  651. await record_failed_attempt(db, current_user.username)
  652. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid code")
  653. await db.execute(delete(UserTOTP).where(UserTOTP.user_id == current_user.id))
  654. await db.commit()
  655. return {"message": "TOTP disabled"}
  656. @router.post("/2fa/totp/regenerate-backup-codes", response_model=BackupCodesResponse)
  657. async def regenerate_backup_codes(
  658. body: TOTPDisableRequest,
  659. current_user: User = Depends(get_current_active_user),
  660. db: AsyncSession = Depends(get_db),
  661. ) -> BackupCodesResponse:
  662. """Generate 10 new backup codes. Requires a valid TOTP code OR a backup code.
  663. M10: Accepts backup codes for consistency with disable_totp — users who have
  664. lost their authenticator app but still have backup codes can regenerate.
  665. Rate-limited to prevent brute-forcing from a hijacked session.
  666. """
  667. await check_rate_limit(db, current_user.username)
  668. result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == current_user.id))
  669. totp_record = result.scalar_one_or_none()
  670. if not totp_record or not totp_record.is_enabled:
  671. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="TOTP is not enabled")
  672. # Same recovery contract as disable_totp: when the TOTP secret cannot be
  673. # decrypted, fall through to the backup-code branch so the user can
  674. # rotate their codes with a printed backup code.
  675. totp_obj: pyotp.TOTP | None = None
  676. code_valid = False
  677. decryption_failed = False
  678. try:
  679. totp_obj = pyotp.TOTP(totp_record.secret)
  680. code_valid = totp_obj.verify(body.code, valid_window=1)
  681. except RuntimeError:
  682. # S3: track server-side failure so we skip the fail-counter debit.
  683. decryption_failed = True
  684. logger.exception(
  685. "TOTP decryption failed for user_id=%s — falling through to backup-code check",
  686. totp_record.user_id,
  687. )
  688. if code_valid and totp_obj is not None:
  689. _assert_totp_not_replayed(totp_obj, totp_record, body.code)
  690. await db.flush() # L-3: persist last_totp_counter immediately to block replay
  691. else:
  692. # Accept a backup code as an alternative (M10)
  693. matched_index: int | None = None
  694. for idx, hashed in enumerate(totp_record.backup_code_hashes):
  695. if pwd_context.verify(body.code, hashed) and matched_index is None:
  696. matched_index = idx
  697. if matched_index is None:
  698. # S3: skip fail-counter debit when the cause was a server-side
  699. # decryption failure (key loss / rotation).
  700. if not decryption_failed:
  701. await record_failed_attempt(db, current_user.username)
  702. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid TOTP or backup code")
  703. # Remove the used backup code
  704. totp_record.backup_code_hashes = [c for i, c in enumerate(totp_record.backup_code_hashes) if i != matched_index]
  705. plain_codes, hashed_codes = _generate_backup_codes()
  706. totp_record.backup_code_hashes = hashed_codes
  707. await db.commit()
  708. return BackupCodesResponse(
  709. backup_codes=plain_codes,
  710. message="Backup codes regenerated. Store them safely — they will not be shown again.",
  711. )
  712. @router.post("/2fa/email/enable")
  713. async def enable_email_otp(
  714. current_user: User = Depends(get_current_active_user),
  715. db: AsyncSession = Depends(get_db),
  716. ) -> dict:
  717. """Step 1 of email OTP enable: send a verification code to the user's email.
  718. C5: Proof of possession — the user must prove they control the registered email
  719. address before email 2FA is activated. Returns a ``setup_token`` that must be
  720. passed to POST /auth/2fa/email/enable/confirm together with the received code.
  721. H-3: Rate-limited to prevent email flooding via repeated calls to this endpoint.
  722. """
  723. await check_email_otp_send_rate(db, current_user.username)
  724. if not current_user.email:
  725. raise HTTPException(
  726. status_code=status.HTTP_400_BAD_REQUEST,
  727. detail="You must have an email address configured to enable email OTP 2FA",
  728. )
  729. smtp_settings = await get_smtp_settings(db)
  730. if not smtp_settings:
  731. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Email service is not configured")
  732. # Generate and store the setup token (reuse AuthEphemeralToken with type "email_otp_setup")
  733. now = datetime.now(timezone.utc)
  734. # Prune any existing pending setup tokens for this user
  735. await db.execute(
  736. delete(AuthEphemeralToken).where(
  737. AuthEphemeralToken.token_type == TokenType.EMAIL_OTP_SETUP,
  738. AuthEphemeralToken.username == current_user.username,
  739. )
  740. )
  741. code = str(secrets.randbelow(1_000_000)).zfill(6)
  742. code_hash = pwd_context.hash(code)
  743. setup_token = secrets.token_urlsafe(32)
  744. db.add(
  745. AuthEphemeralToken(
  746. token=setup_token,
  747. token_type=TokenType.EMAIL_OTP_SETUP,
  748. username=current_user.username,
  749. # Reuse the nonce field to store the code hash
  750. nonce=code_hash,
  751. expires_at=now + timedelta(minutes=10),
  752. )
  753. )
  754. await db.commit()
  755. try:
  756. send_email(
  757. smtp_settings=smtp_settings,
  758. to_email=current_user.email,
  759. subject="Verify your Bambuddy email address for 2FA",
  760. body_text=(
  761. f"Your Bambuddy email 2FA setup code is: {code}\n\n"
  762. "Enter this code to confirm email-based two-factor authentication.\n"
  763. "The code expires in 10 minutes."
  764. ),
  765. body_html=(
  766. "<p>To enable <strong>email-based two-factor authentication</strong> on your Bambuddy account, "
  767. "enter the code below:</p>"
  768. f"<h2 style='letter-spacing:4px'>{code}</h2>"
  769. "<p>The code expires in <strong>10 minutes</strong>. "
  770. "If you did not request this, you can safely ignore this email.</p>"
  771. ),
  772. )
  773. await record_email_otp_send(db, current_user.username)
  774. except Exception as exc:
  775. logger.error("Failed to send email OTP setup code to user_id=%d: %s", current_user.id, exc)
  776. raise HTTPException(
  777. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to send verification email"
  778. )
  779. return {"message": "Verification code sent to your email address", "setup_token": setup_token}
  780. @router.post("/2fa/email/enable/confirm")
  781. async def confirm_enable_email_otp(
  782. body: EmailOTPEnableConfirmRequest,
  783. current_user: User = Depends(get_current_active_user),
  784. db: AsyncSession = Depends(get_db),
  785. ) -> dict:
  786. """Step 2 of email OTP enable: verify the code and activate email 2FA.
  787. H-2 fix: Uses peek-then-consume so a wrong code does NOT burn the setup token.
  788. The token is only deleted after successful code verification, allowing retries
  789. up to the rate limit (5 attempts / 15 min).
  790. M4: Rate-limited to prevent brute-forcing the 6-digit setup code.
  791. """
  792. await check_rate_limit(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  793. now = datetime.now(timezone.utc)
  794. # --- Peek: validate token without consuming ---
  795. peek_result = await db.execute(
  796. select(AuthEphemeralToken).where(
  797. AuthEphemeralToken.token == body.setup_token,
  798. AuthEphemeralToken.token_type == TokenType.EMAIL_OTP_SETUP,
  799. AuthEphemeralToken.username == current_user.username,
  800. AuthEphemeralToken.expires_at > now,
  801. )
  802. )
  803. eph = peek_result.scalar_one_or_none()
  804. if eph is None:
  805. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid or expired setup token")
  806. code_hash = eph.nonce # code hash stored in the nonce field
  807. # --- Verify code before consuming the token ---
  808. if not pwd_context.verify(body.code, code_hash):
  809. await record_failed_attempt(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  810. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid verification code")
  811. # --- Atomically consume the token now that the code is correct ---
  812. # DELETE...RETURNING prevents a concurrent request from using the same token.
  813. del_result = await db.execute(
  814. delete(AuthEphemeralToken)
  815. .where(
  816. AuthEphemeralToken.token == body.setup_token,
  817. AuthEphemeralToken.token_type == TokenType.EMAIL_OTP_SETUP,
  818. AuthEphemeralToken.username == current_user.username,
  819. )
  820. .returning(AuthEphemeralToken.id)
  821. )
  822. if del_result.one_or_none() is None:
  823. # Concurrent request consumed it between peek and delete — treat as invalid.
  824. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid or expired setup token")
  825. await clear_failed_attempts(db, current_user.username, event_type=EventType.TWO_FA_ATTEMPT)
  826. await _set_email_2fa_enabled(db, current_user.id, True)
  827. await db.commit()
  828. return {"message": "Email OTP 2FA enabled"}
  829. @router.post("/2fa/email/disable")
  830. async def disable_email_otp(
  831. body: EmailOTPDisableRequest,
  832. current_user: User = Depends(get_current_active_user),
  833. db: AsyncSession = Depends(get_db),
  834. ) -> dict:
  835. """Disable email-based OTP 2FA for the current user.
  836. C6: Re-authentication required — the caller must supply their account password
  837. to prevent a hijacked session from silently removing a second factor.
  838. LDAP/OIDC-only users (no local password) are exempt from this check.
  839. H-2: Rate-limited to prevent brute-forcing the password via this endpoint.
  840. """
  841. await check_rate_limit(db, current_user.username)
  842. if current_user.password_hash:
  843. if not verify_password(body.password, current_user.password_hash):
  844. await record_failed_attempt(db, current_user.username)
  845. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid password")
  846. await _set_email_2fa_enabled(db, current_user.id, False)
  847. await db.commit()
  848. return {"message": "Email OTP 2FA disabled"}
  849. @router.post("/2fa/email/send")
  850. async def send_email_otp(
  851. request: Request,
  852. body: EmailOTPSendRequest,
  853. db: AsyncSession = Depends(get_db),
  854. ) -> dict:
  855. """Send a 6-digit OTP code to the user's email address.
  856. Requires a valid pre_auth_token obtained during the login flow.
  857. """
  858. # Peek (validate without consuming) first so a rate-limit rejection does not
  859. # permanently burn the caller's pre-auth token.
  860. challenge_id = request.cookies.get("2fa_challenge")
  861. username = await peek_pre_auth_token(db, body.pre_auth_token, challenge_id=challenge_id)
  862. if not username:
  863. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired pre-auth token")
  864. # Enforce rate limit BEFORE consuming the token to prevent OTP email flooding.
  865. await check_email_otp_send_rate(db, username)
  866. user = await get_user_by_username(db, username)
  867. if not user or not user.is_active:
  868. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found or inactive")
  869. if not user.email:
  870. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User has no email address configured")
  871. smtp_settings = await get_smtp_settings(db)
  872. if not smtp_settings:
  873. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Email service is not configured")
  874. # Invalidate all existing unused OTP codes for this user (staged, not yet committed)
  875. await db.execute(
  876. UserOTPCode.__table__.update() # type: ignore[attr-defined]
  877. .where(UserOTPCode.user_id == user.id)
  878. .where(UserOTPCode.used.is_(False))
  879. .values(used=True)
  880. )
  881. # Generate a 6-digit code and stage the record (not committed yet)
  882. code = str(secrets.randbelow(1_000_000)).zfill(6)
  883. code_hash = pwd_context.hash(code)
  884. expires_at = datetime.now(timezone.utc) + timedelta(minutes=UserOTPCode.OTP_TTL_MINUTES)
  885. otp_record = UserOTPCode(
  886. user_id=user.id,
  887. code_hash=code_hash,
  888. attempts=0,
  889. used=False,
  890. expires_at=expires_at,
  891. )
  892. db.add(otp_record)
  893. # M2: Send the email BEFORE consuming the pre-auth token.
  894. # If the send fails we raise an exception here; the session is uncommitted so
  895. # the OTP record is discarded and the original token remains valid for retry.
  896. try:
  897. send_email(
  898. smtp_settings=smtp_settings,
  899. to_email=user.email,
  900. subject="Your Bambuddy verification code",
  901. body_text=f"Your Bambuddy login code is: {code}\n\nThis code expires in {UserOTPCode.OTP_TTL_MINUTES} minutes and can only be used once.",
  902. body_html=(
  903. f"<p>Your <strong>Bambuddy</strong> login verification code is:</p>"
  904. f"<h2 style='letter-spacing:4px'>{code}</h2>"
  905. f"<p>This code expires in <strong>{UserOTPCode.OTP_TTL_MINUTES} minutes</strong> and can only be used once.</p>"
  906. f"<p>If you did not request this code, you can safely ignore this email.</p>"
  907. ),
  908. )
  909. await record_email_otp_send(db, username)
  910. except Exception as exc:
  911. logger.error("Failed to send OTP email to user_id=%d: %s", user.id, exc)
  912. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to send OTP email")
  913. # Email sent — now atomically consume the old token (this also commits the
  914. # staged OTP record) and issue a fresh token for the verify step.
  915. consumed = await consume_pre_auth_token(db, body.pre_auth_token, challenge_id=challenge_id)
  916. if not consumed:
  917. # Raced with another request or token just expired — treat as invalid.
  918. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired pre-auth token")
  919. # Re-issue a fresh pre-auth token bound to the same cookie so the binding
  920. # carries forward through the email → verify step.
  921. fresh_token = await create_pre_auth_token(db, username, challenge_id=challenge_id)
  922. # Return the fresh pre-auth token so the frontend can proceed to verify
  923. return {"message": "Code sent to your email address", "pre_auth_token": fresh_token}
  924. @router.post("/2fa/verify", response_model=TwoFAVerifyResponse)
  925. async def verify_2fa(
  926. request: Request,
  927. body: TwoFAVerifyRequest,
  928. db: AsyncSession = Depends(get_db),
  929. ) -> TwoFAVerifyResponse:
  930. """Verify a 2FA code and exchange the pre_auth_token for a full JWT.
  931. Accepted methods: ``totp``, ``email``, ``backup``.
  932. The pre_auth_token is NOT consumed on failed verification attempts so the
  933. user can retry without restarting the login flow. It is only consumed once
  934. verification succeeds, preventing token replay after success.
  935. """
  936. # Peek without consuming — bad codes must not burn the session token.
  937. # Pass the HttpOnly challenge cookie so the binding check is enforced.
  938. challenge_id = request.cookies.get("2fa_challenge")
  939. username = await peek_pre_auth_token(db, body.pre_auth_token, challenge_id=challenge_id)
  940. if not username:
  941. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired pre-auth token")
  942. await check_rate_limit(db, username)
  943. user = await get_user_by_username(db, username)
  944. if not user or not user.is_active:
  945. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found or inactive")
  946. method = body.method
  947. if method == "totp":
  948. result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == user.id))
  949. totp_record = result.scalar_one_or_none()
  950. if not totp_record or not totp_record.is_enabled:
  951. await record_failed_attempt(db, username)
  952. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="TOTP is not enabled for this user")
  953. try:
  954. totp_obj = pyotp.TOTP(totp_record.secret)
  955. except RuntimeError:
  956. logger.exception("TOTP decryption failed for user_id=%s", totp_record.user_id)
  957. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="TOTP secret unavailable")
  958. if not totp_obj.verify(body.code, valid_window=1):
  959. await record_failed_attempt(db, username)
  960. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid TOTP code")
  961. _assert_totp_not_replayed(totp_obj, totp_record, body.code)
  962. await db.flush() # L-3: persist last_totp_counter immediately to block replay
  963. elif method == "email":
  964. now = datetime.now(timezone.utc)
  965. result = await db.execute(
  966. select(UserOTPCode)
  967. .where(UserOTPCode.user_id == user.id)
  968. .where(UserOTPCode.used.is_(False))
  969. .where(UserOTPCode.expires_at > now)
  970. .order_by(UserOTPCode.created_at.desc())
  971. )
  972. otp_record = result.scalar_one_or_none()
  973. if not otp_record:
  974. await record_failed_attempt(db, username)
  975. raise HTTPException(
  976. status_code=status.HTTP_401_UNAUTHORIZED, detail="No valid OTP code found. Request a new one."
  977. )
  978. if otp_record.attempts >= UserOTPCode.MAX_ATTEMPTS:
  979. otp_record.consume()
  980. await db.commit()
  981. await record_failed_attempt(db, username)
  982. raise HTTPException(
  983. status_code=status.HTTP_401_UNAUTHORIZED, detail="OTP code has been invalidated after too many attempts"
  984. )
  985. if not pwd_context.verify(body.code, otp_record.code_hash):
  986. otp_record.attempts += 1
  987. await db.commit()
  988. await record_failed_attempt(db, username)
  989. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid OTP code")
  990. otp_record.consume()
  991. await db.commit()
  992. else: # method == "backup"
  993. result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == user.id))
  994. totp_record = result.scalar_one_or_none()
  995. if not totp_record or not totp_record.is_enabled:
  996. await record_failed_attempt(db, username)
  997. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="TOTP is not enabled for this user")
  998. # Always iterate all codes — no early break (L-R9-A: constant iteration
  999. # count prevents timing oracle based on used-code position in the list).
  1000. matched_index: int | None = None
  1001. for idx, hashed in enumerate(totp_record.backup_code_hashes):
  1002. if pwd_context.verify(body.code, hashed) and matched_index is None:
  1003. matched_index = idx
  1004. if matched_index is None:
  1005. await record_failed_attempt(db, username)
  1006. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid backup code")
  1007. # M1: Consume the pre-auth token FIRST (atomic single-use enforcement).
  1008. # Only if that succeeds do we remove the backup code — this prevents a race
  1009. # where two concurrent requests both pass code verification but only one
  1010. # should be granted a session.
  1011. consumed_username = await consume_pre_auth_token(db, body.pre_auth_token, challenge_id=challenge_id)
  1012. if not consumed_username:
  1013. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired pre-auth token")
  1014. # Remove the used backup code now that the token is atomically consumed.
  1015. updated_codes = [c for i, c in enumerate(totp_record.backup_code_hashes) if i != matched_index]
  1016. totp_record.backup_code_hashes = updated_codes
  1017. await db.commit()
  1018. await clear_failed_attempts(db, username)
  1019. access_token = create_access_token(
  1020. data={"sub": user.username},
  1021. expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
  1022. )
  1023. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  1024. user = result.scalar_one()
  1025. return TwoFAVerifyResponse(access_token=access_token, token_type="bearer", user=_user_to_response(user))
  1026. # Verification succeeded (TOTP or email) — consume the pre-auth token.
  1027. # C-1: Check the return value; if None the token was already consumed by a
  1028. # concurrent request (race condition) — reject to prevent double-use.
  1029. consumed_username = await consume_pre_auth_token(db, body.pre_auth_token, challenge_id=challenge_id)
  1030. if not consumed_username:
  1031. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired pre-auth token")
  1032. await clear_failed_attempts(db, username)
  1033. access_token = create_access_token(
  1034. data={"sub": user.username},
  1035. expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
  1036. )
  1037. # Reload with groups for permission calculation
  1038. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  1039. user = result.scalar_one()
  1040. return TwoFAVerifyResponse(
  1041. access_token=access_token,
  1042. token_type="bearer",
  1043. user=_user_to_response(user),
  1044. )
  1045. @router.delete("/2fa/admin/{user_id}")
  1046. async def admin_disable_2fa(
  1047. user_id: int,
  1048. body: AdminDisable2FARequest = Body(default_factory=AdminDisable2FARequest),
  1049. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_UPDATE),
  1050. db: AsyncSession = Depends(get_db),
  1051. ) -> dict:
  1052. """Admin endpoint: disable all 2FA for a given user.
  1053. Nit 3: Requires the admin's own password as a re-auth step (matching how
  1054. disable_email_otp protects a user's own 2FA removal). OIDC/LDAP-only admins
  1055. (no local password_hash) are exempt.
  1056. """
  1057. # Nit 3: Re-auth — admin must supply their own password.
  1058. if current_user and current_user.password_hash:
  1059. if not body.admin_password or not verify_password(body.admin_password, current_user.password_hash):
  1060. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Admin password required")
  1061. # Delete TOTP record
  1062. await db.execute(delete(UserTOTP).where(UserTOTP.user_id == user_id))
  1063. # Disable email 2FA setting
  1064. await _set_email_2fa_enabled(db, user_id, False)
  1065. # Invalidate all OTP codes
  1066. await db.execute(
  1067. UserOTPCode.__table__.update() # type: ignore[attr-defined]
  1068. .where(UserOTPCode.user_id == user_id)
  1069. .values(used=True)
  1070. )
  1071. # I2: Invalidate existing JWTs for the target user by bumping password_changed_at.
  1072. # Without this, a stolen token remains valid after 2FA removal.
  1073. target_user = (await db.execute(select(User).where(User.id == user_id))).scalar_one_or_none()
  1074. if target_user:
  1075. target_user.password_changed_at = datetime.now(timezone.utc)
  1076. await db.commit()
  1077. actor = current_user.username if current_user else "anonymous"
  1078. logger.info("Admin %s disabled all 2FA for user_id=%d", actor, user_id)
  1079. return {"message": "2FA disabled for user"}
  1080. # ===========================================================================
  1081. # OIDC Endpoints
  1082. # ===========================================================================
  1083. @router.get("/oidc/providers", response_model=list[OIDCProviderResponse])
  1084. async def list_oidc_providers(
  1085. db: AsyncSession = Depends(get_db),
  1086. ) -> list[OIDCProviderResponse]:
  1087. """List all enabled OIDC providers (public).
  1088. The login page renders icons via /oidc/providers/{id}/icon — `icon_data`
  1089. stays deferred so this list query never pulls the BLOB.
  1090. """
  1091. result = await db.execute(select(OIDCProvider).where(OIDCProvider.is_enabled.is_(True)))
  1092. providers = result.scalars().all()
  1093. return [_build_provider_response(p) for p in providers]
  1094. @router.get("/oidc/providers/all", response_model=list[OIDCProviderResponse])
  1095. async def list_all_oidc_providers(
  1096. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  1097. db: AsyncSession = Depends(get_db),
  1098. ) -> list[OIDCProviderResponse]:
  1099. """List ALL OIDC providers including disabled ones (admin only)."""
  1100. result2 = await db.execute(select(OIDCProvider))
  1101. providers = result2.scalars().all()
  1102. return [_build_provider_response(p) for p in providers]
  1103. @router.post("/oidc/providers", response_model=OIDCProviderResponse, status_code=status.HTTP_201_CREATED)
  1104. async def create_oidc_provider(
  1105. body: OIDCProviderCreate,
  1106. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1107. db: AsyncSession = Depends(get_db),
  1108. ) -> OIDCProviderResponse:
  1109. """Create a new OIDC provider (admin only).
  1110. If `icon_url` is supplied, the icon is fetched server-side and cached in
  1111. the BLOB columns (#1333). A fetch failure aborts the create with 400 —
  1112. no half-configured provider is left in the DB.
  1113. """
  1114. if body.default_group_id is not None:
  1115. grp_chk = await db.execute(select(Group).where(Group.id == body.default_group_id))
  1116. if not grp_chk.scalar_one_or_none():
  1117. raise HTTPException(
  1118. status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
  1119. detail="default_group_id references a non-existent group",
  1120. )
  1121. # Fetch the icon BEFORE creating the row so a failure leaves the DB clean.
  1122. icon_data: bytes | None = None
  1123. icon_content_type: str | None = None
  1124. icon_etag: str | None = None
  1125. if body.icon_url:
  1126. icon_data, icon_content_type, icon_etag = await _fetch_icon_or_400(body.icon_url)
  1127. provider = OIDCProvider(
  1128. name=body.name,
  1129. issuer_url=body.issuer_url.rstrip("/"),
  1130. client_id=body.client_id,
  1131. client_secret=body.client_secret,
  1132. scopes=body.scopes,
  1133. is_enabled=body.is_enabled,
  1134. auto_create_users=body.auto_create_users,
  1135. auto_link_existing_accounts=body.auto_link_existing_accounts,
  1136. email_claim=body.email_claim,
  1137. require_email_verified=body.require_email_verified,
  1138. icon_url=body.icon_url,
  1139. icon_data=icon_data,
  1140. icon_content_type=icon_content_type,
  1141. icon_etag=icon_etag,
  1142. default_group_id=body.default_group_id,
  1143. )
  1144. # SEC-1 + SEC-6: runtime guard mirrors the OIDCProviderCreate model_validator in schemas/auth.py.
  1145. # Catches any future path that bypasses Pydantic validation (direct ORM, scripts).
  1146. _enforce_auto_link_safety(provider)
  1147. db.add(provider)
  1148. await db.commit()
  1149. await db.refresh(provider)
  1150. return _build_provider_response(provider)
  1151. @router.put("/oidc/providers/{provider_id}", response_model=OIDCProviderResponse)
  1152. async def update_oidc_provider(
  1153. provider_id: int,
  1154. body: OIDCProviderUpdate,
  1155. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1156. db: AsyncSession = Depends(get_db),
  1157. ) -> OIDCProviderResponse:
  1158. """Update an existing OIDC provider (admin only).
  1159. Icon refetch fires when:
  1160. 1. The submitted `icon_url` differs from the stored one (URL changed), OR
  1161. 2. The submitted `icon_url` equals the stored one AND `icon_content_type`
  1162. is NULL — this is the upgrade-path edge case: old providers carry
  1163. `icon_url` but no cached bytes until the admin first saves them.
  1164. On fetch failure the request aborts with 400 *before* commit, so the
  1165. existing cached bytes (if any) remain untouched.
  1166. """
  1167. result2 = await db.execute(select(OIDCProvider).where(OIDCProvider.id == provider_id))
  1168. provider = result2.scalar_one_or_none()
  1169. if not provider:
  1170. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Provider not found")
  1171. if body.default_group_id is not None:
  1172. grp_chk = await db.execute(select(Group).where(Group.id == body.default_group_id))
  1173. if not grp_chk.scalar_one_or_none():
  1174. raise HTTPException(
  1175. status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
  1176. detail="default_group_id references a non-existent group",
  1177. )
  1178. dumped = body.model_dump(exclude_none=True)
  1179. # Decide whether an icon refetch is needed BEFORE mutating the ORM object,
  1180. # so the comparison sees provider.icon_url / icon_content_type as they are
  1181. # in the database.
  1182. new_icon_url = dumped.get("icon_url")
  1183. needs_icon_refetch = new_icon_url is not None and (
  1184. new_icon_url != provider.icon_url or provider.icon_content_type is None
  1185. )
  1186. # Fetch FIRST. If the upstream is unreachable or SSRF-blocked, _fetch_icon_or_400
  1187. # raises HTTPException(400) here — provider attributes are still untouched, so
  1188. # the in-memory ORM object stays consistent on the way out (and the DB row is
  1189. # safe regardless via get_db()'s rollback).
  1190. fetched_icon: tuple[bytes, str, str] | None = None
  1191. if needs_icon_refetch:
  1192. fetched_icon = await _fetch_icon_or_400(new_icon_url)
  1193. # Explicit `icon_url: null` in the PUT body means "clear the icon".
  1194. # The exclude_none=True dump above drops None values, which would
  1195. # otherwise silently ignore this request. Check model_fields_set on
  1196. # the unfiltered body to distinguish "client cleared it" from "client
  1197. # didn't include this field at all".
  1198. if "icon_url" in body.model_fields_set and body.icon_url is None:
  1199. provider.icon_url = None
  1200. provider.icon_data = None
  1201. provider.icon_content_type = None
  1202. provider.icon_etag = None
  1203. for field, value in dumped.items():
  1204. if field == "issuer_url" and value:
  1205. value = value.rstrip("/")
  1206. setattr(provider, field, value)
  1207. if fetched_icon is not None:
  1208. provider.icon_data, provider.icon_content_type, provider.icon_etag = fetched_icon
  1209. # SEC-1 + SEC-6: Combined-State-Guard after setattr loop.
  1210. # Checks the final in-memory state (DB values + newly set values combined) to catch
  1211. # partial updates that each pass schema validation individually but are unsafe together.
  1212. _enforce_auto_link_safety(provider)
  1213. await db.commit()
  1214. await db.refresh(provider)
  1215. return _build_provider_response(provider)
  1216. @router.delete("/oidc/providers/{provider_id}")
  1217. async def delete_oidc_provider(
  1218. provider_id: int,
  1219. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1220. db: AsyncSession = Depends(get_db),
  1221. ) -> dict:
  1222. """Delete an OIDC provider and all its user links (admin only)."""
  1223. result2 = await db.execute(select(OIDCProvider).where(OIDCProvider.id == provider_id))
  1224. provider = result2.scalar_one_or_none()
  1225. if not provider:
  1226. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Provider not found")
  1227. await db.delete(provider)
  1228. await db.commit()
  1229. return {"message": "Provider deleted"}
  1230. # ---------------------------------------------------------------------------
  1231. # OIDC provider icon proxy (#1333)
  1232. # ---------------------------------------------------------------------------
  1233. @router.get("/oidc/providers/{provider_id}/icon")
  1234. async def get_oidc_provider_icon(
  1235. provider_id: int,
  1236. if_none_match: str | None = Header(default=None, alias="If-None-Match"),
  1237. db: AsyncSession = Depends(get_db),
  1238. ) -> Response:
  1239. """Serve the cached icon for an enabled OIDC provider (public, no auth).
  1240. Unauthenticated because ``<img>`` tags cannot send Authorization headers
  1241. and the login page renders these icons before the user is signed in — the
  1242. same justification as ``/api/v1/makerworld/thumbnail``. The SSRF guard
  1243. runs at admin-config time (create/update/refresh), not here.
  1244. Disabled providers respond 404 to avoid leaking their existence to
  1245. anonymous callers (mirrors ``GET /oidc/providers`` which filters on
  1246. ``is_enabled``).
  1247. """
  1248. result = await db.execute(
  1249. select(OIDCProvider)
  1250. .options(undefer(OIDCProvider.icon_data))
  1251. .where(OIDCProvider.id == provider_id, OIDCProvider.is_enabled.is_(True))
  1252. )
  1253. provider = result.scalar_one_or_none()
  1254. if provider is None or provider.icon_content_type is None or provider.icon_data is None:
  1255. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Icon not found")
  1256. etag_value = f'"{provider.icon_etag}"'
  1257. cache_headers = {"ETag": etag_value, "Cache-Control": "public, max-age=3600"}
  1258. if _etag_matches(if_none_match, provider.icon_etag):
  1259. return Response(status_code=status.HTTP_304_NOT_MODIFIED, headers=cache_headers)
  1260. return Response(
  1261. content=provider.icon_data,
  1262. media_type=provider.icon_content_type,
  1263. headers=cache_headers,
  1264. )
  1265. @router.delete("/oidc/providers/{provider_id}/icon", status_code=status.HTTP_204_NO_CONTENT)
  1266. async def delete_oidc_provider_icon(
  1267. provider_id: int,
  1268. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1269. db: AsyncSession = Depends(get_db),
  1270. ) -> Response:
  1271. """Remove the icon entirely for a provider (admin only).
  1272. Clears all four icon columns — ``icon_url`` plus the three cached-bytes
  1273. columns. "Remove icon" means the whole record is gone, not just the
  1274. cache; without this the admin form would still show the URL while
  1275. the login page rendered a blank fallback (confusing half-state).
  1276. To re-add an icon the admin re-types the URL in the edit form.
  1277. """
  1278. result = await db.execute(select(OIDCProvider).where(OIDCProvider.id == provider_id))
  1279. provider = result.scalar_one_or_none()
  1280. if provider is None:
  1281. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Provider not found")
  1282. # Setting deferred columns is safe — no read happens, just a write.
  1283. provider.icon_url = None
  1284. provider.icon_data = None
  1285. provider.icon_content_type = None
  1286. provider.icon_etag = None
  1287. await db.commit()
  1288. return Response(status_code=status.HTTP_204_NO_CONTENT)
  1289. @router.post("/oidc/providers/{provider_id}/icon/refresh", response_model=OIDCProviderResponse)
  1290. async def refresh_oidc_provider_icon(
  1291. provider_id: int,
  1292. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1293. db: AsyncSession = Depends(get_db),
  1294. ) -> OIDCProviderResponse:
  1295. """Refetch the icon from the stored `icon_url` (admin only).
  1296. Used when:
  1297. - The IdP changed its icon and the admin wants Bambuddy to pick up the
  1298. new bytes.
  1299. - An upgrade left the provider with an `icon_url` but no cached bytes
  1300. (covered automatically by `update_oidc_provider` too, but this gives
  1301. the UI an explicit "Refresh" button).
  1302. Failure to refetch returns 400 *before* commit, so the previously cached
  1303. bytes survive intact.
  1304. """
  1305. result = await db.execute(select(OIDCProvider).where(OIDCProvider.id == provider_id))
  1306. provider = result.scalar_one_or_none()
  1307. if provider is None:
  1308. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Provider not found")
  1309. if not provider.icon_url:
  1310. raise HTTPException(
  1311. status_code=status.HTTP_400_BAD_REQUEST,
  1312. detail="Provider has no icon_url to refresh",
  1313. )
  1314. icon_data, icon_content_type, icon_etag = await _fetch_icon_or_400(provider.icon_url)
  1315. provider.icon_data = icon_data
  1316. provider.icon_content_type = icon_content_type
  1317. provider.icon_etag = icon_etag
  1318. await db.commit()
  1319. await db.refresh(provider)
  1320. return _build_provider_response(provider)
  1321. @router.get("/oidc/authorize/{provider_id}", response_model=OIDCAuthorizeResponse)
  1322. async def oidc_authorize(
  1323. provider_id: int,
  1324. db: AsyncSession = Depends(get_db),
  1325. ) -> OIDCAuthorizeResponse:
  1326. """Return the OIDC authorization URL for the given provider."""
  1327. result = await db.execute(
  1328. select(OIDCProvider).where(OIDCProvider.id == provider_id).where(OIDCProvider.is_enabled.is_(True))
  1329. )
  1330. provider = result.scalar_one_or_none()
  1331. if not provider:
  1332. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Provider not found or not enabled")
  1333. # Fetch discovery document
  1334. discovery_url = f"{provider.issuer_url.rstrip('/')}/.well-known/openid-configuration"
  1335. try:
  1336. async with httpx.AsyncClient(timeout=10) as client:
  1337. resp = await client.get(discovery_url)
  1338. resp.raise_for_status()
  1339. discovery = resp.json()
  1340. except Exception as exc:
  1341. logger.error("Failed to fetch OIDC discovery for provider %d: %s", provider_id, exc)
  1342. raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Failed to fetch OIDC discovery document")
  1343. authorization_endpoint = discovery.get("authorization_endpoint")
  1344. if not authorization_endpoint:
  1345. raise HTTPException(
  1346. status_code=status.HTTP_502_BAD_GATEWAY, detail="OIDC discovery document missing authorization_endpoint"
  1347. )
  1348. # B2: SSRF guard — reject non-HTTP(S) schemes in the authorization endpoint
  1349. if not authorization_endpoint.startswith(("https://", "http://")):
  1350. logger.warning("OIDC discovery authorization_endpoint has invalid scheme: %s", authorization_endpoint)
  1351. raise HTTPException(
  1352. status_code=status.HTTP_502_BAD_GATEWAY,
  1353. detail="OIDC discovery document contains invalid authorization_endpoint",
  1354. )
  1355. external_url = await _get_base_external_url(db)
  1356. redirect_uri = f"{external_url}/api/v1/auth/oidc/callback"
  1357. now = datetime.now(timezone.utc)
  1358. # Prune expired OIDC states from the DB
  1359. await db.execute(
  1360. delete(AuthEphemeralToken).where(
  1361. AuthEphemeralToken.token_type == TokenType.OIDC_STATE,
  1362. AuthEphemeralToken.expires_at < now,
  1363. )
  1364. )
  1365. state = secrets.token_urlsafe(32)
  1366. nonce = secrets.token_urlsafe(32)
  1367. # PKCE (S256) – required by PocketID and recommended for all OIDC flows
  1368. code_verifier = secrets.token_urlsafe(48) # 64-char URL-safe string
  1369. code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).rstrip(b"=").decode()
  1370. db.add(
  1371. AuthEphemeralToken(
  1372. token=state,
  1373. token_type=TokenType.OIDC_STATE,
  1374. provider_id=provider_id,
  1375. nonce=nonce,
  1376. code_verifier=code_verifier,
  1377. expires_at=now + OIDC_STATE_TTL,
  1378. )
  1379. )
  1380. await db.commit()
  1381. params = urllib.parse.urlencode(
  1382. {
  1383. "response_type": "code",
  1384. "client_id": provider.client_id,
  1385. "redirect_uri": redirect_uri,
  1386. "scope": provider.scopes,
  1387. "state": state,
  1388. "nonce": nonce,
  1389. "code_challenge": code_challenge,
  1390. "code_challenge_method": "S256",
  1391. }
  1392. )
  1393. auth_url = f"{authorization_endpoint}?{params}"
  1394. return OIDCAuthorizeResponse(auth_url=auth_url)
  1395. @router.get("/oidc/callback")
  1396. async def oidc_callback(
  1397. code: str | None = Query(default=None, max_length=2048),
  1398. state: str | None = Query(default=None, max_length=2048),
  1399. error: str | None = Query(default=None, max_length=256),
  1400. db: AsyncSession = Depends(get_db),
  1401. ) -> RedirectResponse:
  1402. """Handle the OIDC authorization code callback from the identity provider."""
  1403. external_url = await _get_base_external_url(db)
  1404. frontend_error_url = f"{external_url}/?oidc_error="
  1405. try:
  1406. if error:
  1407. logger.warning("OIDC callback received error: %s", error)
  1408. return RedirectResponse(url=f"{frontend_error_url}oidc_provider_error", status_code=302)
  1409. if not code or not state:
  1410. return RedirectResponse(url=f"{frontend_error_url}missing_parameters", status_code=302)
  1411. # Atomically validate and consume OIDC state from DB (I6: single-use enforcement).
  1412. # DELETE...RETURNING ensures concurrent callbacks with the same state token
  1413. # cannot both succeed — only the first DELETE finds the row.
  1414. now = datetime.now(timezone.utc)
  1415. state_del = await db.execute(
  1416. delete(AuthEphemeralToken)
  1417. .where(
  1418. AuthEphemeralToken.token == state,
  1419. AuthEphemeralToken.token_type == TokenType.OIDC_STATE,
  1420. AuthEphemeralToken.expires_at > now, # reject expired tokens atomically
  1421. )
  1422. .returning(
  1423. AuthEphemeralToken.provider_id,
  1424. AuthEphemeralToken.nonce,
  1425. AuthEphemeralToken.code_verifier,
  1426. )
  1427. )
  1428. state_row = state_del.one_or_none()
  1429. if state_row is None:
  1430. await db.rollback()
  1431. return RedirectResponse(url=f"{frontend_error_url}invalid_state", status_code=302)
  1432. provider_id, nonce, code_verifier = state_row
  1433. await db.commit()
  1434. # Load provider
  1435. result = await db.execute(select(OIDCProvider).where(OIDCProvider.id == provider_id))
  1436. provider = result.scalar_one_or_none()
  1437. if not provider:
  1438. return RedirectResponse(url=f"{frontend_error_url}provider_not_found", status_code=302)
  1439. redirect_uri = f"{external_url}/api/v1/auth/oidc/callback"
  1440. # ── Step 1: Fetch discovery document ────────────────────────────────
  1441. discovery_url = f"{provider.issuer_url.rstrip('/')}/.well-known/openid-configuration"
  1442. try:
  1443. async with httpx.AsyncClient(timeout=10) as client:
  1444. disc_resp = await client.get(discovery_url)
  1445. disc_resp.raise_for_status()
  1446. discovery = disc_resp.json()
  1447. except Exception as exc:
  1448. logger.error("OIDC discovery fetch failed for provider %d: %s", provider_id, exc)
  1449. return RedirectResponse(url=f"{frontend_error_url}discovery_failed", status_code=302)
  1450. token_endpoint = discovery.get("token_endpoint")
  1451. jwks_uri = discovery.get("jwks_uri")
  1452. if not token_endpoint or not jwks_uri:
  1453. return RedirectResponse(url=f"{frontend_error_url}invalid_discovery_document", status_code=302)
  1454. # L-R7-C: Reject non-HTTP(S) URLs in the discovery document to prevent
  1455. # SSRF via crafted responses (e.g. file://, gopher://, internal schemes).
  1456. if not token_endpoint.startswith(("https://", "http://")) or not jwks_uri.startswith(("https://", "http://")):
  1457. logger.warning(
  1458. "OIDC discovery document contains non-HTTP URL(s): token=%s jwks=%s", token_endpoint, jwks_uri
  1459. )
  1460. return RedirectResponse(url=f"{frontend_error_url}invalid_discovery_document", status_code=302)
  1461. # ── Step 2: Exchange authorization code for tokens ───────────────────
  1462. token_form: dict[str, str] = {
  1463. "grant_type": "authorization_code",
  1464. "code": code,
  1465. "redirect_uri": redirect_uri,
  1466. "client_id": provider.client_id,
  1467. }
  1468. if provider.client_secret:
  1469. token_form["client_secret"] = provider.client_secret
  1470. if code_verifier:
  1471. token_form["code_verifier"] = code_verifier
  1472. try:
  1473. async with httpx.AsyncClient(timeout=15) as client:
  1474. token_resp = await client.post(
  1475. token_endpoint,
  1476. data=token_form,
  1477. headers={"Accept": "application/json"},
  1478. )
  1479. except Exception as exc:
  1480. logger.error("OIDC token exchange request failed for provider %d: %s", provider_id, exc)
  1481. return RedirectResponse(url=f"{frontend_error_url}token_exchange_network_error", status_code=302)
  1482. if not token_resp.is_success:
  1483. try:
  1484. err_body = token_resp.json()
  1485. oidc_err = err_body.get("error", "")
  1486. oidc_desc = err_body.get("error_description", "")
  1487. except Exception:
  1488. oidc_err = ""
  1489. oidc_desc = token_resp.text[:200]
  1490. logger.error(
  1491. "OIDC token exchange HTTP %d for provider %d. redirect_uri=%r error=%r desc=%r",
  1492. token_resp.status_code,
  1493. provider_id,
  1494. redirect_uri,
  1495. oidc_err,
  1496. oidc_desc,
  1497. )
  1498. # Encode the OIDC error code into the redirect so the user sees it in the toast.
  1499. # URL-encode the value to prevent query-parameter injection from provider responses.
  1500. raw_err = oidc_err[:40] if oidc_err else str(token_resp.status_code)
  1501. safe_err = urllib.parse.quote(raw_err, safe="")
  1502. return RedirectResponse(
  1503. url=f"{frontend_error_url}token_exchange_{safe_err}",
  1504. status_code=302,
  1505. )
  1506. try:
  1507. token_data = token_resp.json()
  1508. except Exception as exc:
  1509. logger.error("OIDC token exchange non-JSON response for provider %d: %s", provider_id, exc)
  1510. return RedirectResponse(url=f"{frontend_error_url}token_exchange_bad_response", status_code=302)
  1511. id_token = token_data.get("id_token")
  1512. if not id_token:
  1513. # Only log the keys present — values may contain secrets (access_token, etc.)
  1514. logger.error(
  1515. "OIDC token response missing id_token for provider %d; keys present: %s",
  1516. provider_id,
  1517. list(token_data.keys()),
  1518. )
  1519. return RedirectResponse(url=f"{frontend_error_url}no_id_token", status_code=302)
  1520. # ── Step 3: Fetch JWKS and validate ID token ─────────────────────────
  1521. # Use the issuer from the discovery document as the canonical value (OIDC Core
  1522. # §3.1.3.7 requires iss == discovery issuer exactly). We strip trailing slashes
  1523. # from both sides because some providers (e.g. Authentik, older PocketID versions)
  1524. # are inconsistent between the discovery issuer and the JWT iss claim.
  1525. discovery_issuer: str = discovery.get("issuer", provider.issuer_url).rstrip("/")
  1526. try:
  1527. async with httpx.AsyncClient(timeout=10) as jwks_http:
  1528. jwks_resp = await jwks_http.get(jwks_uri)
  1529. jwks_resp.raise_for_status()
  1530. jwks_data = jwks_resp.json()
  1531. jwks_client = PyJWKClient(jwks_uri)
  1532. jwks_client.fetch_data = lambda: jwks_data # type: ignore[method-assign]
  1533. signing_key = jwks_client.get_signing_key_from_jwt(id_token)
  1534. # M-3: Decode without built-in issuer check, then compare normalised
  1535. # (both sides rstrip("/")) to handle providers like Authentik that include
  1536. # a trailing slash in iss but not in the discovery issuer, or vice-versa.
  1537. claims = jwt.decode(
  1538. id_token,
  1539. signing_key.key,
  1540. algorithms=["RS256", "ES256", "RS384", "ES384", "RS512"],
  1541. audience=provider.client_id,
  1542. options={"verify_iss": False},
  1543. )
  1544. token_iss = claims.get("iss", "").rstrip("/")
  1545. if token_iss != discovery_issuer:
  1546. raise jwt.exceptions.InvalidIssuerError("Invalid issuer")
  1547. except Exception as exc:
  1548. logger.error("OIDC JWT validation failed for provider %d: %s", provider_id, exc, exc_info=True)
  1549. return RedirectResponse(url=f"{frontend_error_url}token_validation_failed", status_code=302)
  1550. # Verify nonce — fail closed: we always send a nonce, so the provider must echo it.
  1551. # Skipping the check when nonce is absent would allow CSRF on non-nonce providers.
  1552. token_nonce = claims.get("nonce")
  1553. if token_nonce is None or token_nonce != nonce:
  1554. logger.warning("OIDC nonce mismatch for provider %d (present=%r)", provider_id, token_nonce is not None)
  1555. return RedirectResponse(url=f"{frontend_error_url}nonce_mismatch", status_code=302)
  1556. provider_sub: str = claims.get("sub", "")
  1557. if not provider_sub:
  1558. return RedirectResponse(url=f"{frontend_error_url}missing_sub_claim", status_code=302)
  1559. # SEC-3: resolve email via Fall A/B/C logic (see _resolve_provider_email).
  1560. provider_email = _resolve_provider_email(provider, claims, provider_sub)
  1561. # ── Step 4: Resolve / create user ────────────────────────────────────
  1562. try:
  1563. # 1. Look up existing OIDC link
  1564. link_result = await db.execute(
  1565. select(UserOIDCLink)
  1566. .where(UserOIDCLink.provider_id == provider_id)
  1567. .where(UserOIDCLink.provider_user_id == provider_sub)
  1568. )
  1569. link = link_result.scalar_one_or_none()
  1570. user: User | None = None
  1571. if link:
  1572. # Existing link → load the linked user
  1573. user_result = await db.execute(
  1574. select(User).where(User.id == link.user_id).options(selectinload(User.groups))
  1575. )
  1576. user = user_result.scalar_one_or_none()
  1577. else:
  1578. # 2. No OIDC link yet — check for an existing user with the same email.
  1579. # Use case-insensitive matching (func.lower) so that "User@Example.com"
  1580. # and "user@example.com" are treated as the same identity, preventing
  1581. # an attacker-controlled IdP from bypassing the auto-link guard by
  1582. # registering the target email with different casing.
  1583. email_user: User | None = None
  1584. if provider_email:
  1585. email_user = await get_user_by_email(db, provider_email)
  1586. if email_user and provider.auto_link_existing_accounts:
  1587. # M-4: Only auto-link when the provider has auto_link_existing_accounts
  1588. # enabled. Operators can disable this to require explicit account linking,
  1589. # preventing an attacker-controlled IdP from hijacking local accounts.
  1590. #
  1591. # M-NEW-6: Refuse auto-link if the target user already has any OIDC
  1592. # link (to any provider). Without this guard an attacker who controls
  1593. # a second OIDC provider with auto_link enabled could add themselves as
  1594. # a second IdP for a user that already authenticates via a legitimate
  1595. # provider, effectively taking over the account.
  1596. existing_links_result = await db.execute(
  1597. select(UserOIDCLink).where(UserOIDCLink.user_id == email_user.id)
  1598. )
  1599. has_existing_oidc_link = existing_links_result.scalar_one_or_none() is not None
  1600. if has_existing_oidc_link:
  1601. logger.warning(
  1602. "Auto-link rejected for user '%s': already linked to another OIDC provider",
  1603. email_user.username,
  1604. )
  1605. return RedirectResponse(url=f"{frontend_error_url}no_linked_account", status_code=302)
  1606. db.add(
  1607. UserOIDCLink(
  1608. user_id=email_user.id,
  1609. provider_id=provider_id,
  1610. provider_user_id=provider_sub,
  1611. provider_email=provider_email,
  1612. )
  1613. )
  1614. await db.commit()
  1615. user = email_user
  1616. logger.info(
  1617. "Auto-linked existing user '%s' to OIDC provider %d via email match",
  1618. email_user.username,
  1619. provider_id,
  1620. )
  1621. elif provider.auto_create_users:
  1622. # 3. No existing user — create one
  1623. if provider_email:
  1624. raw = provider_email.split("@")[0]
  1625. else:
  1626. # Prefer a human-readable IdP claim over the opaque sub.
  1627. # isinstance guards are required: claims may carry non-string
  1628. # values (e.g. a list) that would break .strip().
  1629. # Sanitization is applied per-candidate so that a value that
  1630. # strips to empty (e.g. "!!!") correctly falls through to the
  1631. # next candidate rather than silently becoming "oidcuser".
  1632. _pref = claims.get("preferred_username")
  1633. _name = claims.get("name")
  1634. raw = ""
  1635. if isinstance(_pref, str):
  1636. raw = re.sub(r"[^a-zA-Z0-9._-]", "", _pref.strip())[:30]
  1637. if not raw and isinstance(_name, str):
  1638. raw = re.sub(r"[^a-zA-Z0-9._-]", "", _name.strip())[:30]
  1639. if not raw:
  1640. raw = provider_sub[:30]
  1641. candidate = re.sub(r"[^a-zA-Z0-9._-]", "", raw)[:30] or "oidcuser"
  1642. username = candidate
  1643. counter = 1
  1644. while True:
  1645. existing = await get_user_by_username(db, username)
  1646. if not existing:
  1647. break
  1648. username = f"{candidate}{counter}"
  1649. counter += 1
  1650. # I9: Assign new OIDC users to a group before flush — accessing
  1651. # new_user.groups after a flush triggers a lazy-load which fails
  1652. # in async context. Resolution order:
  1653. # 1. provider.default_group_id (operator-configured)
  1654. # 2. "Viewers" (system fallback for read-only access)
  1655. # 3. no group (last resort if Viewers was deleted)
  1656. # SQLite does not enforce ON DELETE SET NULL, so a dangling
  1657. # default_group_id returns None here and falls through to Viewers.
  1658. default_group: Group | None = None
  1659. if provider.default_group_id is not None:
  1660. dg_result = await db.execute(select(Group).where(Group.id == provider.default_group_id))
  1661. default_group = dg_result.scalar_one_or_none()
  1662. if default_group is None:
  1663. viewers_result = await db.execute(select(Group).where(Group.name == "Viewers"))
  1664. default_group = viewers_result.scalar_one_or_none()
  1665. new_user = User(
  1666. username=username,
  1667. email=provider_email,
  1668. # M-1: auth_source="oidc" prevents local password-reset flow
  1669. # for users who should only authenticate via OIDC.
  1670. auth_source="oidc",
  1671. password_hash=None, # OIDC users never use password auth
  1672. role="user",
  1673. is_active=True,
  1674. groups=[default_group] if default_group else [],
  1675. )
  1676. db.add(new_user)
  1677. await db.flush()
  1678. db.add(
  1679. UserOIDCLink(
  1680. user_id=new_user.id,
  1681. provider_id=provider_id,
  1682. provider_user_id=provider_sub,
  1683. provider_email=provider_email,
  1684. )
  1685. )
  1686. await db.commit()
  1687. user_result = await db.execute(
  1688. select(User).where(User.id == new_user.id).options(selectinload(User.groups))
  1689. )
  1690. user = user_result.scalar_one()
  1691. logger.info("Auto-created user '%s' via OIDC provider %d", username, provider_id)
  1692. else:
  1693. return RedirectResponse(url=f"{frontend_error_url}no_linked_account", status_code=302)
  1694. if not user or not user.is_active:
  1695. return RedirectResponse(url=f"{frontend_error_url}account_inactive", status_code=302)
  1696. # Issue an OIDC exchange token (short-lived, single-use) stored in DB.
  1697. # I7: Opportunistically prune expired exchange tokens to keep the table small.
  1698. now2 = datetime.now(timezone.utc)
  1699. await db.execute(
  1700. delete(AuthEphemeralToken).where(
  1701. AuthEphemeralToken.token_type == TokenType.OIDC_EXCHANGE,
  1702. AuthEphemeralToken.expires_at < now2,
  1703. )
  1704. )
  1705. exchange_token = secrets.token_urlsafe(32)
  1706. db.add(
  1707. AuthEphemeralToken(
  1708. token=exchange_token,
  1709. token_type=TokenType.OIDC_EXCHANGE,
  1710. username=user.username,
  1711. expires_at=now2 + OIDC_EXCHANGE_TTL,
  1712. )
  1713. )
  1714. await db.commit()
  1715. # H-4: Use a URL fragment (#) instead of a query parameter so the exchange
  1716. # token is never sent to the server in the Referer header or server logs.
  1717. return RedirectResponse(url=f"{external_url}/login#oidc_token={exchange_token}", status_code=302)
  1718. except Exception as exc:
  1719. logger.error("OIDC user resolution failed for provider %d: %s", provider_id, exc, exc_info=True)
  1720. try:
  1721. await db.rollback()
  1722. except Exception as rb_exc:
  1723. logger.error("DB rollback failed after OIDC user-resolution error: %s", rb_exc, exc_info=True)
  1724. return RedirectResponse(url=f"{frontend_error_url}user_resolution_failed", status_code=302)
  1725. except Exception as exc:
  1726. # L-1: Log the exception class name internally but never expose it in the
  1727. # redirect URL — leaking exception names aids attacker reconnaissance.
  1728. logger.error("Unexpected error in OIDC callback (%s): %s", type(exc).__name__, exc, exc_info=True)
  1729. try:
  1730. return RedirectResponse(url=f"{frontend_error_url}internal_error", status_code=302)
  1731. except Exception as redirect_exc:
  1732. logger.error("Failed to construct error redirect in OIDC callback: %s", redirect_exc, exc_info=True)
  1733. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="OIDC callback failed")
  1734. @router.post("/oidc/exchange", response_model=LoginResponse)
  1735. async def oidc_exchange(
  1736. body: OIDCExchangeRequest,
  1737. raw_request: Request,
  1738. response: Response,
  1739. db: AsyncSession = Depends(get_db),
  1740. ) -> LoginResponse:
  1741. """Exchange an OIDC exchange token (from the callback redirect) for a full JWT.
  1742. C4: If the resolved user has 2FA enabled the exchange returns a pre_auth_token
  1743. (requires_2fa=True) instead of a full JWT. The frontend must then complete the
  1744. 2FA step exactly as it would after a password-based login.
  1745. """
  1746. now = datetime.now(timezone.utc)
  1747. # Atomically consume the exchange token (DELETE...RETURNING prevents replay).
  1748. consume_result = await db.execute(
  1749. delete(AuthEphemeralToken)
  1750. .where(
  1751. AuthEphemeralToken.token == body.oidc_token,
  1752. AuthEphemeralToken.token_type == TokenType.OIDC_EXCHANGE,
  1753. AuthEphemeralToken.expires_at > now, # reject expired tokens atomically
  1754. )
  1755. .returning(AuthEphemeralToken.username)
  1756. )
  1757. row = consume_result.one_or_none()
  1758. if row is None:
  1759. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired OIDC exchange token")
  1760. (username,) = row
  1761. await db.commit()
  1762. user = await get_user_by_username(db, username)
  1763. if not user or not user.is_active:
  1764. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found or inactive")
  1765. # Reload with groups
  1766. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  1767. user = result.scalar_one()
  1768. # C4: Check whether the user has any 2FA method enabled.
  1769. totp_result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == user.id))
  1770. totp_record = totp_result.scalar_one_or_none()
  1771. totp_enabled = totp_record is not None and totp_record.is_enabled
  1772. email_2fa_enabled = await _get_email_2fa_enabled(db, user.id)
  1773. if totp_enabled or email_2fa_enabled:
  1774. # User has 2FA — issue a pre_auth_token bound to this browser session via
  1775. # an HttpOnly cookie (H-A: mirrors the cookie-binding done in auth.py:login).
  1776. two_fa_methods: list[str] = []
  1777. if totp_enabled:
  1778. two_fa_methods.append("totp")
  1779. if email_2fa_enabled:
  1780. two_fa_methods.append("email")
  1781. if totp_enabled:
  1782. two_fa_methods.append("backup")
  1783. challenge_id = secrets.token_urlsafe(32)
  1784. pre_auth_token = await create_pre_auth_token(db, user.username, challenge_id=challenge_id)
  1785. response.set_cookie(
  1786. key="2fa_challenge",
  1787. value=challenge_id,
  1788. httponly=True,
  1789. secure=raw_request.url.scheme == "https",
  1790. samesite="lax",
  1791. max_age=300,
  1792. path="/api/v1/auth/2fa",
  1793. )
  1794. return LoginResponse(
  1795. requires_2fa=True,
  1796. pre_auth_token=pre_auth_token,
  1797. two_fa_methods=two_fa_methods,
  1798. user=_user_to_response(user),
  1799. )
  1800. access_token = create_access_token(
  1801. data={"sub": user.username},
  1802. expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
  1803. )
  1804. return LoginResponse(
  1805. access_token=access_token,
  1806. token_type="bearer",
  1807. user=_user_to_response(user),
  1808. requires_2fa=False,
  1809. )
  1810. @router.get("/oidc/links", response_model=list[OIDCLinkResponse])
  1811. async def list_oidc_links(
  1812. current_user: User = Depends(get_current_active_user),
  1813. db: AsyncSession = Depends(get_db),
  1814. ) -> list[OIDCLinkResponse]:
  1815. """List all OIDC provider links for the current user."""
  1816. result = await db.execute(
  1817. select(UserOIDCLink).where(UserOIDCLink.user_id == current_user.id).options(selectinload(UserOIDCLink.provider))
  1818. )
  1819. links = result.scalars().all()
  1820. # Defensive null-check on link.provider: on PostgreSQL the FK cascade
  1821. # ensures provider exists, but SQLite ships with FK enforcement off, so
  1822. # a deleted provider could in theory leave the link briefly orphan until
  1823. # the next init_db() cleanup runs. Returning "<deleted>" instead of
  1824. # crashing keeps the endpoint usable in that edge case (#1285 follow-up).
  1825. return [
  1826. OIDCLinkResponse(
  1827. id=link.id,
  1828. provider_id=link.provider_id,
  1829. provider_name=link.provider.name if link.provider else "<deleted>",
  1830. provider_email=link.provider_email,
  1831. created_at=link.created_at.isoformat(),
  1832. )
  1833. for link in links
  1834. ]
  1835. @router.delete("/oidc/links/{provider_id}")
  1836. async def remove_oidc_link(
  1837. provider_id: int,
  1838. current_user: User = Depends(get_current_active_user),
  1839. db: AsyncSession = Depends(get_db),
  1840. ) -> dict:
  1841. """Remove the OIDC link between the current user and a provider."""
  1842. result = await db.execute(
  1843. select(UserOIDCLink)
  1844. .where(UserOIDCLink.user_id == current_user.id)
  1845. .where(UserOIDCLink.provider_id == provider_id)
  1846. )
  1847. link = result.scalar_one_or_none()
  1848. if not link:
  1849. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="OIDC link not found")
  1850. await db.delete(link)
  1851. await db.commit()
  1852. return {"message": "OIDC link removed"}
  1853. # ---------------------------------------------------------------------------
  1854. # Internal helpers
  1855. # ---------------------------------------------------------------------------
  1856. async def _get_base_external_url(db: AsyncSession) -> str:
  1857. """Return the base external URL (no trailing slash, no /login suffix)."""
  1858. external_url = await get_setting(db, "external_url")
  1859. if external_url:
  1860. return external_url.rstrip("/")
  1861. return os.environ.get("APP_URL", "http://localhost:5173").rstrip("/")