auth.py 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. import logging
  2. import os
  3. import secrets
  4. from datetime import datetime, timedelta, timezone
  5. from typing import Annotated
  6. import jwt as _jwt
  7. from fastapi import APIRouter, BackgroundTasks, Depends, Header, HTTPException, Request, Response, status
  8. from fastapi.security import HTTPAuthorizationCredentials
  9. from jwt.exceptions import PyJWTError
  10. from sqlalchemy import delete, select
  11. from sqlalchemy.exc import SQLAlchemyError
  12. from sqlalchemy.ext.asyncio import AsyncSession
  13. from sqlalchemy.orm import selectinload
  14. from backend.app.api.routes.settings import get_external_login_url
  15. from backend.app.core.auth import (
  16. ACCESS_TOKEN_EXPIRE_MINUTES,
  17. ALGORITHM,
  18. SECRET_KEY,
  19. Permission,
  20. RequirePermissionIfAuthEnabled,
  21. _is_token_fresh,
  22. _validate_api_key,
  23. authenticate_user,
  24. authenticate_user_by_email,
  25. create_access_token,
  26. get_current_active_user,
  27. get_password_hash,
  28. get_user_by_email,
  29. get_user_by_username,
  30. is_jti_revoked,
  31. revoke_jti,
  32. security,
  33. )
  34. from backend.app.core.database import async_session, get_db
  35. from backend.app.core.permissions import ALL_PERMISSIONS
  36. from backend.app.models.auth_ephemeral import AuthEphemeralToken, AuthRateLimitEvent, EventType, TokenType
  37. from backend.app.models.group import Group
  38. from backend.app.models.settings import Settings
  39. from backend.app.models.user import User
  40. from backend.app.schemas.auth import (
  41. EncryptionRowCounts,
  42. EncryptionStatusResponse,
  43. ForgotPasswordConfirmRequest,
  44. ForgotPasswordRequest,
  45. ForgotPasswordResponse,
  46. GroupBrief,
  47. LDAPProvisionRequest,
  48. LDAPSearchResultResponse,
  49. LoginRequest,
  50. LoginResponse,
  51. ResetPasswordRequest,
  52. ResetPasswordResponse,
  53. SetupRequest,
  54. SetupResponse,
  55. SMTPSettings,
  56. TestSMTPRequest,
  57. TestSMTPResponse,
  58. UserResponse,
  59. _validate_password_complexity,
  60. )
  61. from backend.app.services.email_service import (
  62. create_password_reset_link_email_from_template,
  63. get_smtp_settings,
  64. save_smtp_settings,
  65. send_email,
  66. )
  67. _logger = logging.getLogger(__name__)
  68. def _user_to_response(user: User) -> UserResponse:
  69. """Convert a User model to UserResponse schema."""
  70. return UserResponse(
  71. id=user.id,
  72. username=user.username,
  73. email=user.email,
  74. role=user.role,
  75. is_active=user.is_active,
  76. is_admin=user.is_admin,
  77. auth_source=getattr(user, "auth_source", "local"),
  78. groups=[GroupBrief(id=g.id, name=g.name) for g in user.groups],
  79. permissions=sorted(user.get_permissions()),
  80. created_at=user.created_at.isoformat(),
  81. )
  82. def _api_key_to_user_response(api_key) -> UserResponse:
  83. """Create a synthetic admin UserResponse for a valid API key."""
  84. return UserResponse(
  85. id=0,
  86. username=f"api-key:{api_key.key_prefix}",
  87. email=None,
  88. role="admin",
  89. is_active=True,
  90. is_admin=True,
  91. groups=[],
  92. permissions=sorted(ALL_PERMISSIONS),
  93. created_at=api_key.created_at.isoformat(),
  94. )
  95. # ---------------------------------------------------------------------------
  96. # M-R9-A: Real client IP resolution for rate limiting behind reverse proxies.
  97. # Set TRUSTED_PROXY_IPS (comma-separated) to enable X-Forwarded-For trust.
  98. # Without this env var client.host is used directly (safe default).
  99. # ---------------------------------------------------------------------------
  100. _TRUSTED_PROXY_IPS: frozenset[str] = frozenset(
  101. ip.strip() for ip in os.environ.get("TRUSTED_PROXY_IPS", "").split(",") if ip.strip()
  102. )
  103. def _get_client_ip(request: Request) -> str:
  104. """Return the real client IP for rate-limiting purposes.
  105. When TRUSTED_PROXY_IPS is configured and the direct TCP peer is a trusted
  106. proxy, X-Forwarded-For is evaluated right-to-left: the rightmost IP that is
  107. NOT itself a trusted proxy is the true client address (M-R10-A fix).
  108. Standard nginx with proxy_add_x_forwarded_for *appends* the client IP, so
  109. the rightmost entry is always the one added by the last trusted proxy —
  110. i.e. the real client. Walking right-to-left and skipping known proxies is
  111. safe for multi-hop chains as well.
  112. Falls back to request.client.host when TRUSTED_PROXY_IPS is unset (direct
  113. deployment without a reverse proxy).
  114. """
  115. # I5: Use a per-request unique token instead of "unknown" when the transport
  116. # layer provides no client address. This prevents all such requests from
  117. # sharing one rate-limit bucket, and avoids collision with a literal username
  118. # "unknown". The token is not stable across requests, which is intentional:
  119. # we cannot track the IP so we also cannot rate-limit by it meaningfully.
  120. direct_ip = request.client.host if request.client else f"__no_ip_{secrets.token_hex(8)}__"
  121. if _TRUSTED_PROXY_IPS and direct_ip in _TRUSTED_PROXY_IPS:
  122. forwarded_for = request.headers.get("X-Forwarded-For", "")
  123. ips = [ip.strip() for ip in forwarded_for.split(",") if ip.strip()]
  124. # Walk right-to-left; skip IPs that belong to trusted proxies.
  125. for ip in reversed(ips):
  126. if ip not in _TRUSTED_PROXY_IPS:
  127. return ip
  128. # Edge case: every entry is a trusted proxy — fall back to leftmost.
  129. if ips:
  130. return ips[0]
  131. return direct_ip
  132. router = APIRouter(prefix="/auth", tags=["authentication"])
  133. async def is_auth_enabled(db: AsyncSession) -> bool:
  134. """Check if authentication is enabled."""
  135. result = await db.execute(select(Settings).where(Settings.key == "auth_enabled"))
  136. setting = result.scalar_one_or_none()
  137. if setting is None:
  138. return False
  139. return setting.value.lower() == "true"
  140. async def is_advanced_auth_enabled(db: AsyncSession) -> bool:
  141. """Check if advanced authentication is enabled."""
  142. result = await db.execute(select(Settings).where(Settings.key == "advanced_auth_enabled"))
  143. setting = result.scalar_one_or_none()
  144. if setting is None:
  145. return False
  146. return setting.value.lower() == "true"
  147. async def set_advanced_auth_enabled(db: AsyncSession, enabled: bool) -> None:
  148. """Set advanced authentication enabled status."""
  149. from backend.app.core.db_dialect import upsert_setting
  150. await upsert_setting(db, Settings, "advanced_auth_enabled", "true" if enabled else "false")
  151. async def set_auth_enabled(db: AsyncSession, enabled: bool) -> None:
  152. """Set authentication enabled status."""
  153. from backend.app.core.db_dialect import upsert_setting
  154. await upsert_setting(db, Settings, "auth_enabled", "true" if enabled else "false")
  155. # Note: Don't commit here - let get_db handle it or commit explicitly in the route
  156. async def is_setup_completed(db: AsyncSession) -> bool:
  157. """Check if setup has been completed."""
  158. result = await db.execute(select(Settings).where(Settings.key == "setup_completed"))
  159. setting = result.scalar_one_or_none()
  160. return setting and setting.value.lower() == "true"
  161. async def set_setup_completed(db: AsyncSession, completed: bool) -> None:
  162. """Set setup completed status."""
  163. from backend.app.core.db_dialect import upsert_setting
  164. await upsert_setting(db, Settings, "setup_completed", "true" if completed else "false")
  165. # Note: Don't commit here - let get_db handle it or commit explicitly in the route
  166. @router.post("/setup", response_model=SetupResponse)
  167. async def setup_auth(request: SetupRequest, db: AsyncSession = Depends(get_db)):
  168. """First-time setup: enable/disable authentication and create admin user."""
  169. import logging
  170. logger = logging.getLogger(__name__)
  171. try:
  172. # If auth is currently enabled, block unauthenticated setup changes.
  173. # Use the admin panel (/disable endpoint) to modify auth when it's already on.
  174. if await is_auth_enabled(db):
  175. raise HTTPException(
  176. status_code=status.HTTP_403_FORBIDDEN,
  177. detail="Authentication is already configured. Use the admin panel to modify auth settings.",
  178. )
  179. admin_created = False
  180. if request.auth_enabled:
  181. # Check if admin users already exist
  182. admin_users_result = await db.execute(select(User).where(User.role == "admin"))
  183. existing_admin_users = list(admin_users_result.scalars().all())
  184. has_admin_users = len(existing_admin_users) > 0
  185. if has_admin_users:
  186. # Admin users already exist, just enable auth (don't create new admin)
  187. logger.info(
  188. f"Admin users already exist ({len(existing_admin_users)} found), enabling authentication without creating new admin"
  189. )
  190. admin_created = False
  191. else:
  192. # No admin users exist, require admin credentials to create first admin
  193. if not request.admin_username or not request.admin_password:
  194. raise HTTPException(
  195. status_code=status.HTTP_400_BAD_REQUEST,
  196. detail="Admin username and password are required when enabling authentication (no admin users exist)",
  197. )
  198. # Enforce password complexity only when actually creating a new admin.
  199. # Schema-level validation was removed so that re-enabling auth with an
  200. # existing admin (or LDAP) doesn't reject whatever placeholder the form sends.
  201. try:
  202. _validate_password_complexity(request.admin_password)
  203. except ValueError as exc:
  204. raise HTTPException(
  205. status_code=status.HTTP_400_BAD_REQUEST,
  206. detail=str(exc),
  207. )
  208. # Check if username already exists (shouldn't happen if no admin users exist, but check anyway)
  209. existing_user = await get_user_by_username(db, request.admin_username)
  210. if existing_user:
  211. raise HTTPException(
  212. status_code=status.HTTP_400_BAD_REQUEST,
  213. detail="User with this username already exists",
  214. )
  215. # Create admin user FIRST (before enabling auth)
  216. try:
  217. logger.info("Creating admin user: %s", request.admin_username)
  218. admin_user = User(
  219. username=request.admin_username,
  220. password_hash=get_password_hash(request.admin_password),
  221. role="admin",
  222. is_active=True,
  223. )
  224. # Try to add user to Administrators group if it exists
  225. admin_group_result = await db.execute(select(Group).where(Group.name == "Administrators"))
  226. admin_group = admin_group_result.scalar_one_or_none()
  227. if admin_group:
  228. admin_user.groups.append(admin_group)
  229. logger.info("Added new admin user to Administrators group")
  230. db.add(admin_user)
  231. logger.info("Admin user added to session: %s", request.admin_username)
  232. admin_created = True
  233. except Exception as e:
  234. await db.rollback()
  235. logger.error("Failed to create admin user: %s", e, exc_info=True)
  236. raise HTTPException(
  237. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  238. detail="Failed to create admin user",
  239. )
  240. # Set auth enabled and mark setup as completed
  241. await set_auth_enabled(db, request.auth_enabled)
  242. await set_setup_completed(db, True)
  243. await db.commit()
  244. if admin_created:
  245. await db.refresh(admin_user)
  246. logger.info("Admin user created successfully: %s", admin_user.id)
  247. logger.info("Setup completed: auth_enabled=%s, admin_created=%s", request.auth_enabled, admin_created)
  248. return SetupResponse(auth_enabled=request.auth_enabled, admin_created=admin_created)
  249. except HTTPException:
  250. raise
  251. except Exception as e:
  252. logger.error("Setup error: %s", e, exc_info=True)
  253. await db.rollback()
  254. raise HTTPException(
  255. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  256. detail="Setup failed",
  257. )
  258. @router.get("/status")
  259. async def get_auth_status(db: AsyncSession = Depends(get_db)):
  260. """Get authentication status (public endpoint)."""
  261. auth_enabled = await is_auth_enabled(db)
  262. setup_completed = await is_setup_completed(db)
  263. # Only require setup if it hasn't been completed yet
  264. requires_setup = not setup_completed
  265. return {"auth_enabled": auth_enabled, "requires_setup": requires_setup}
  266. @router.post("/disable", response_model=dict)
  267. async def disable_auth(
  268. current_user: User = Depends(get_current_active_user),
  269. db: AsyncSession = Depends(get_db),
  270. ):
  271. """Disable authentication (admin only)."""
  272. import logging
  273. logger = logging.getLogger(__name__)
  274. # Reload user with groups for proper is_admin check
  275. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  276. user = result.scalar_one()
  277. # Only admins can disable authentication
  278. if not user.is_admin:
  279. raise HTTPException(
  280. status_code=status.HTTP_403_FORBIDDEN,
  281. detail="Only admins can disable authentication",
  282. )
  283. try:
  284. await set_auth_enabled(db, False)
  285. await db.commit()
  286. logger.info("Authentication disabled by admin user: %s", user.username)
  287. return {"message": "Authentication disabled successfully", "auth_enabled": False}
  288. except Exception as e:
  289. await db.rollback()
  290. logger.error("Failed to disable authentication: %s", e, exc_info=True)
  291. raise HTTPException(
  292. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  293. detail="Failed to disable authentication",
  294. )
  295. @router.post("/login", response_model=LoginResponse)
  296. async def login(raw_request: Request, request: LoginRequest, response: Response, db: AsyncSession = Depends(get_db)):
  297. """Login and get access token.
  298. Supports username or email-based login. Username lookup is case-insensitive.
  299. When 2FA is enabled for the user the response contains ``requires_2fa=True``
  300. and a short-lived ``pre_auth_token`` instead of the final JWT. The client
  301. must then call ``POST /auth/2fa/verify`` (or first ``POST /auth/2fa/email/send``
  302. to trigger an email OTP) to obtain the real access token.
  303. """
  304. # Check if auth is enabled
  305. auth_enabled = await is_auth_enabled(db)
  306. if not auth_enabled:
  307. raise HTTPException(
  308. status_code=status.HTTP_400_BAD_REQUEST,
  309. detail="Authentication is not enabled",
  310. )
  311. # Rate-limit repeated login failures — two independent buckets (M-R5-B / M-R6-A):
  312. # 1. Per-username (10/15 min): prevents password brute-force on a known account.
  313. # 2. Per-IP (20/15 min): prevents an attacker from locking out arbitrary accounts
  314. # (DoS) by sending failures for many usernames from a single address.
  315. from backend.app.api.routes.mfa import MAX_LOGIN_ATTEMPTS, check_rate_limit, record_failed_attempt
  316. await check_rate_limit(db, request.username, event_type=EventType.LOGIN_ATTEMPT, max_attempts=MAX_LOGIN_ATTEMPTS)
  317. client_ip = _get_client_ip(raw_request)
  318. await check_rate_limit(db, client_ip, event_type=EventType.LOGIN_IP, max_attempts=20)
  319. # Check if LDAP is enabled
  320. ldap_user = None
  321. ldap_settings = await _get_ldap_settings(db)
  322. if ldap_settings:
  323. try:
  324. from backend.app.services.ldap_service import (
  325. authenticate_ldap_user,
  326. parse_ldap_config,
  327. )
  328. ldap_config = parse_ldap_config(ldap_settings)
  329. if ldap_config:
  330. ldap_user = authenticate_ldap_user(ldap_config, request.username, request.password)
  331. if ldap_user:
  332. # LDAP auth succeeded — find or create local user
  333. user = await get_user_by_username(db, ldap_user.username)
  334. if user and user.auth_source != "ldap":
  335. # Username exists as local user — don't override
  336. user = None
  337. ldap_user = None
  338. elif not user:
  339. if not ldap_config.auto_provision:
  340. # User doesn't exist and auto-provision is off
  341. ldap_user = None
  342. else:
  343. # Auto-provision LDAP user
  344. user = await _provision_ldap_user(db, ldap_user, ldap_config)
  345. if user and ldap_user:
  346. # Update email and group mappings on each login
  347. await _sync_ldap_user(db, user, ldap_user, ldap_config)
  348. except Exception as e:
  349. import logging
  350. logging.getLogger(__name__).warning("LDAP authentication error, falling back to local: %s", e)
  351. ldap_user = None
  352. # Try username-based authentication (skip if already authenticated via LDAP)
  353. if not ldap_user:
  354. user = await authenticate_user(db, request.username, request.password)
  355. # If username auth failed and advanced auth is enabled, try email-based authentication
  356. if not user and not ldap_user:
  357. advanced_auth = await is_advanced_auth_enabled(db)
  358. if advanced_auth:
  359. user = await authenticate_user_by_email(db, request.username, request.password)
  360. if not user:
  361. await record_failed_attempt(db, request.username, event_type=EventType.LOGIN_ATTEMPT)
  362. await record_failed_attempt(db, client_ip, event_type=EventType.LOGIN_IP)
  363. raise HTTPException(
  364. status_code=status.HTTP_401_UNAUTHORIZED,
  365. detail="Incorrect username or password",
  366. headers={"WWW-Authenticate": "Bearer"},
  367. )
  368. # Reload user with groups for proper permission calculation
  369. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  370. user = result.scalar_one()
  371. # L-R6-A: Password was correct — reset login failure counters for both buckets
  372. from backend.app.api.routes.mfa import clear_failed_attempts
  373. await clear_failed_attempts(db, user.username, event_type=EventType.LOGIN_ATTEMPT)
  374. await clear_failed_attempts(db, client_ip, event_type=EventType.LOGIN_IP)
  375. # --- 2FA check ---
  376. # Determine which 2FA methods are active for this user.
  377. from backend.app.models.settings import Settings as _Settings
  378. from backend.app.models.user_totp import UserTOTP
  379. totp_result = await db.execute(select(UserTOTP).where(UserTOTP.user_id == user.id))
  380. user_totp = totp_result.scalar_one_or_none()
  381. totp_enabled = user_totp is not None and user_totp.is_enabled
  382. email_2fa_result = await db.execute(select(_Settings).where(_Settings.key == f"user_{user.id}_email_2fa_enabled"))
  383. email_2fa_setting = email_2fa_result.scalar_one_or_none()
  384. email_otp_enabled = (
  385. email_2fa_setting is not None and email_2fa_setting.value.lower() == "true" and user.email is not None
  386. )
  387. if totp_enabled or email_otp_enabled:
  388. # Import here to avoid circular imports
  389. from backend.app.api.routes.mfa import create_pre_auth_token
  390. # Bind the pre_auth_token to an HttpOnly cookie so XSS cannot steal the
  391. # token from JS memory and complete 2FA from a different client.
  392. challenge_id = secrets.token_urlsafe(32)
  393. pre_auth_token = await create_pre_auth_token(db, user.username, challenge_id=challenge_id)
  394. response.set_cookie(
  395. key="2fa_challenge",
  396. value=challenge_id,
  397. httponly=True,
  398. # H-1: only transmit over HTTPS so the binding cookie can't be intercepted
  399. # on mixed-content deployments. Falls back to False on plain HTTP so tests
  400. # and local development still work (the client wouldn't send it otherwise).
  401. secure=raw_request.url.scheme == "https",
  402. samesite="lax",
  403. max_age=300,
  404. path="/api/v1/auth/2fa",
  405. )
  406. methods: list[str] = []
  407. if totp_enabled:
  408. methods.append("totp")
  409. if email_otp_enabled:
  410. methods.append("email")
  411. # Backup codes are always available when TOTP is set up
  412. if totp_enabled:
  413. methods.append("backup")
  414. return LoginResponse(
  415. requires_2fa=True,
  416. pre_auth_token=pre_auth_token,
  417. two_fa_methods=methods,
  418. )
  419. # No 2FA — issue full token immediately
  420. access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  421. access_token = create_access_token(data={"sub": user.username}, expires_delta=access_token_expires)
  422. return LoginResponse(
  423. access_token=access_token,
  424. token_type="bearer",
  425. user=_user_to_response(user),
  426. )
  427. @router.get("/me", response_model=UserResponse)
  428. async def get_current_user_info(
  429. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  430. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  431. db: AsyncSession = Depends(get_db),
  432. ):
  433. """Get current user information.
  434. Accepts JWT tokens (via Authorization: Bearer header) and API keys
  435. (via X-API-Key header or Authorization: Bearer bb_xxx).
  436. API keys return a synthetic admin user with all permissions.
  437. """
  438. import jwt
  439. from jwt.exceptions import PyJWTError as JWTError
  440. # Check for API key via X-API-Key header
  441. if x_api_key:
  442. api_key = await _validate_api_key(db, x_api_key)
  443. if api_key:
  444. return _api_key_to_user_response(api_key)
  445. # Check for Bearer token (could be JWT or API key)
  446. if credentials is not None:
  447. token = credentials.credentials
  448. # Check if it's an API key (starts with bb_)
  449. if token.startswith("bb_"):
  450. api_key = await _validate_api_key(db, token)
  451. if api_key:
  452. return _api_key_to_user_response(api_key)
  453. raise HTTPException(
  454. status_code=status.HTTP_401_UNAUTHORIZED,
  455. detail="Invalid API key",
  456. headers={"WWW-Authenticate": "Bearer"},
  457. )
  458. # Otherwise treat as JWT
  459. try:
  460. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  461. username: str = payload.get("sub")
  462. if username is None:
  463. raise HTTPException(
  464. status_code=status.HTTP_401_UNAUTHORIZED,
  465. detail="Could not validate credentials",
  466. headers={"WWW-Authenticate": "Bearer"},
  467. )
  468. jti: str | None = payload.get("jti")
  469. if not jti or await is_jti_revoked(jti): # B1: logout bypass fix
  470. raise HTTPException(
  471. status_code=status.HTTP_401_UNAUTHORIZED,
  472. detail="Could not validate credentials",
  473. headers={"WWW-Authenticate": "Bearer"},
  474. )
  475. iat: int | float | None = payload.get("iat")
  476. except JWTError:
  477. raise HTTPException(
  478. status_code=status.HTTP_401_UNAUTHORIZED,
  479. detail="Could not validate credentials",
  480. headers={"WWW-Authenticate": "Bearer"},
  481. )
  482. user = await get_user_by_username(db, username)
  483. if user is None or not user.is_active:
  484. raise HTTPException(
  485. status_code=status.HTTP_401_UNAUTHORIZED,
  486. detail="Could not validate credentials",
  487. headers={"WWW-Authenticate": "Bearer"},
  488. )
  489. # Reload with groups for proper permission calculation
  490. result = await db.execute(select(User).where(User.id == user.id).options(selectinload(User.groups)))
  491. user = result.scalar_one()
  492. # L-R8-A: reject tokens issued before the last password change
  493. if not _is_token_fresh(iat, user):
  494. raise HTTPException(
  495. status_code=status.HTTP_401_UNAUTHORIZED,
  496. detail="Could not validate credentials",
  497. headers={"WWW-Authenticate": "Bearer"},
  498. )
  499. return _user_to_response(user)
  500. # No credentials provided
  501. raise HTTPException(
  502. status_code=status.HTTP_401_UNAUTHORIZED,
  503. detail="Authentication required",
  504. headers={"WWW-Authenticate": "Bearer"},
  505. )
  506. @router.post("/logout")
  507. async def logout(
  508. raw_request: Request,
  509. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  510. ):
  511. """Logout — revokes the current JWT so it cannot be reused after logout."""
  512. if credentials is not None:
  513. raw_token = credentials.credentials
  514. # Nit2: Verify signature before revoking to prevent DoS-revoke attacks
  515. # (an attacker crafting a token with an arbitrary jti cannot force
  516. # revocation of a legitimate token because the signature check rejects it).
  517. # Expired tokens are still accepted — the user is logging out and their
  518. # token may have just expired; we still want to record the revocation.
  519. try:
  520. verified = _jwt.decode(
  521. raw_token,
  522. SECRET_KEY,
  523. algorithms=[ALGORITHM],
  524. options={"verify_exp": False}, # allow expired tokens at logout
  525. )
  526. jti: str | None = verified.get("jti")
  527. exp = verified.get("exp")
  528. username: str | None = verified.get("sub")
  529. if jti and exp:
  530. expires_at = datetime.fromtimestamp(exp, tz=timezone.utc)
  531. try:
  532. await revoke_jti(jti, expires_at, username)
  533. except Exception as exc:
  534. _logger.error("Failed to revoke JTI on logout for user %s: %s", username, exc)
  535. except PyJWTError:
  536. client_ip = _get_client_ip(raw_request)
  537. ua = raw_request.headers.get("user-agent", "<unknown>")
  538. _logger.error(
  539. "Logout received token that failed signature verification — skipping revocation "
  540. "(possible tamper attempt; ip=%s ua=%s)",
  541. client_ip,
  542. ua,
  543. )
  544. return {"message": "Logged out successfully"}
  545. # Advanced Authentication Endpoints
  546. @router.post("/smtp/test", response_model=TestSMTPResponse)
  547. async def test_smtp_connection(
  548. test_request: TestSMTPRequest,
  549. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  550. db: AsyncSession = Depends(get_db),
  551. ):
  552. """Test SMTP connection using saved settings (admin only when auth enabled)."""
  553. import logging
  554. logger = logging.getLogger(__name__)
  555. try:
  556. smtp_settings = await get_smtp_settings(db)
  557. if not smtp_settings:
  558. return TestSMTPResponse(success=False, message="SMTP settings not configured. Save SMTP settings first.")
  559. # Send test email
  560. send_email(
  561. smtp_settings=smtp_settings,
  562. to_email=test_request.test_recipient,
  563. subject="BamBuddy SMTP Test",
  564. body_text="This is a test email from BamBuddy. If you received this, your SMTP settings are working correctly!",
  565. body_html="<p>This is a test email from <strong>BamBuddy</strong>.</p><p>If you received this, your SMTP settings are working correctly!</p>",
  566. )
  567. logger.info(f"Test email sent successfully to {test_request.test_recipient}")
  568. return TestSMTPResponse(success=True, message="Test email sent successfully")
  569. except Exception as e:
  570. logger.error("Failed to send test email: %s", e)
  571. return TestSMTPResponse(success=False, message="Failed to send test email")
  572. @router.get("/smtp", response_model=SMTPSettings | None)
  573. async def get_smtp_config(
  574. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  575. db: AsyncSession = Depends(get_db),
  576. ):
  577. """Get SMTP settings (admin only when auth enabled). Password is not returned."""
  578. smtp_settings = await get_smtp_settings(db)
  579. if smtp_settings:
  580. # Don't return password in response
  581. smtp_settings.smtp_password = None
  582. return smtp_settings
  583. @router.post("/smtp", response_model=dict)
  584. async def save_smtp_config(
  585. smtp_settings: SMTPSettings,
  586. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  587. db: AsyncSession = Depends(get_db),
  588. ):
  589. """Save SMTP settings (admin only when auth enabled)."""
  590. import logging
  591. logger = logging.getLogger(__name__)
  592. try:
  593. await save_smtp_settings(db, smtp_settings)
  594. await db.commit()
  595. logger.info(f"SMTP settings updated by admin user: {current_user.username if current_user else 'anonymous'}")
  596. return {"message": "SMTP settings saved successfully"}
  597. except Exception as e:
  598. await db.rollback()
  599. logger.error("Failed to save SMTP settings: %s", e)
  600. raise HTTPException(
  601. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  602. detail="Failed to save SMTP settings",
  603. )
  604. @router.post("/advanced-auth/enable", response_model=dict)
  605. async def enable_advanced_auth(
  606. current_user: User = Depends(get_current_active_user),
  607. db: AsyncSession = Depends(get_db),
  608. ):
  609. """Enable advanced authentication (admin only).
  610. Requires SMTP settings to be configured and tested first.
  611. """
  612. import logging
  613. logger = logging.getLogger(__name__)
  614. # Reload user with groups for proper is_admin check
  615. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  616. user = result.scalar_one()
  617. if not user.is_admin:
  618. raise HTTPException(
  619. status_code=status.HTTP_403_FORBIDDEN,
  620. detail="Only admins can enable advanced authentication",
  621. )
  622. # Verify SMTP settings are configured
  623. smtp_settings = await get_smtp_settings(db)
  624. if not smtp_settings:
  625. raise HTTPException(
  626. status_code=status.HTTP_400_BAD_REQUEST,
  627. detail="SMTP settings must be configured before enabling advanced authentication",
  628. )
  629. try:
  630. await set_advanced_auth_enabled(db, True)
  631. await db.commit()
  632. logger.info(f"Advanced authentication enabled by admin user: {user.username}")
  633. return {"message": "Advanced authentication enabled successfully", "advanced_auth_enabled": True}
  634. except Exception as e:
  635. await db.rollback()
  636. logger.error("Failed to enable advanced authentication: %s", e)
  637. raise HTTPException(
  638. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  639. detail="Failed to enable advanced authentication",
  640. )
  641. @router.post("/advanced-auth/disable", response_model=dict)
  642. async def disable_advanced_auth(
  643. current_user: User = Depends(get_current_active_user),
  644. db: AsyncSession = Depends(get_db),
  645. ):
  646. """Disable advanced authentication (admin only)."""
  647. import logging
  648. logger = logging.getLogger(__name__)
  649. # Reload user with groups for proper is_admin check
  650. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  651. user = result.scalar_one()
  652. if not user.is_admin:
  653. raise HTTPException(
  654. status_code=status.HTTP_403_FORBIDDEN,
  655. detail="Only admins can disable advanced authentication",
  656. )
  657. try:
  658. await set_advanced_auth_enabled(db, False)
  659. await db.commit()
  660. logger.info(f"Advanced authentication disabled by admin user: {user.username}")
  661. return {"message": "Advanced authentication disabled successfully", "advanced_auth_enabled": False}
  662. except Exception as e:
  663. await db.rollback()
  664. logger.error("Failed to disable advanced authentication: %s", e)
  665. raise HTTPException(
  666. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  667. detail="Failed to disable advanced authentication",
  668. )
  669. @router.get("/advanced-auth/status")
  670. async def get_advanced_auth_status(db: AsyncSession = Depends(get_db)):
  671. """Get advanced authentication status."""
  672. advanced_auth_enabled = await is_advanced_auth_enabled(db)
  673. smtp_configured = await get_smtp_settings(db) is not None
  674. return {
  675. "advanced_auth_enabled": advanced_auth_enabled,
  676. "smtp_configured": smtp_configured,
  677. }
  678. # TTL for password-reset tokens (H-6)
  679. _RESET_TOKEN_TTL = timedelta(hours=1)
  680. # Rate-limit for password-reset email sends per identifier (M-A)
  681. _MAX_PWD_RESET_SENDS = 3
  682. _PWD_RESET_SEND_WINDOW = timedelta(minutes=15)
  683. # L-NEW-6: per-IP cap to prevent mass-reset flooding across many addresses
  684. _MAX_PWD_RESET_SENDS_PER_IP = 10
  685. async def _send_reset_email_or_delete_token(
  686. reset_token: str,
  687. smtp_settings,
  688. to_email: str,
  689. subject: str,
  690. text_body: str,
  691. html_body: str,
  692. log_label: str,
  693. ) -> None:
  694. """Background task: send a password-reset email and delete the token on failure.
  695. C1: FastAPI silently swallows BackgroundTask exceptions. This wrapper
  696. catches send failures, deletes the single-use token so it cannot be used
  697. (user is not locked out forever — they can request a new link), and logs at
  698. ERROR so operators are alerted without leaking details to the caller.
  699. """
  700. try:
  701. send_email(smtp_settings, to_email, subject, text_body, html_body)
  702. _logger.info("Password reset email sent (%s) to %s", log_label, to_email)
  703. except Exception as exc:
  704. _logger.error(
  705. "Password reset email failed (%s) to %s — deleting token to unblock re-request: %s",
  706. log_label,
  707. to_email,
  708. exc,
  709. )
  710. try:
  711. async with async_session() as db:
  712. await db.execute(
  713. delete(AuthEphemeralToken).where(
  714. AuthEphemeralToken.token == reset_token,
  715. AuthEphemeralToken.token_type == TokenType.PASSWORD_RESET,
  716. )
  717. )
  718. await db.commit()
  719. except Exception as db_exc:
  720. _logger.error("Failed to delete reset token after send failure: %s", db_exc)
  721. @router.post("/forgot-password", response_model=ForgotPasswordResponse)
  722. async def forgot_password(
  723. request: ForgotPasswordRequest,
  724. background_tasks: BackgroundTasks,
  725. raw_request: Request,
  726. db: AsyncSession = Depends(get_db),
  727. ):
  728. """Request password reset via email (advanced auth only).
  729. H-6: Issues a short-lived single-use reset token and emails the user a
  730. secure link instead of a plaintext temporary password. The new password is
  731. set only when the user clicks the link and POSTs to /forgot-password/confirm.
  732. """
  733. # Check if advanced auth is enabled
  734. advanced_auth = await is_advanced_auth_enabled(db)
  735. if not advanced_auth:
  736. raise HTTPException(
  737. status_code=status.HTTP_400_BAD_REQUEST,
  738. detail="Advanced authentication is not enabled",
  739. )
  740. # M-A: Rate-limit by normalised email to prevent reset-email flooding.
  741. # Apply unconditionally (before the user lookup) so unknown emails are also
  742. # throttled — this prevents both flooding and timing-based enumeration.
  743. identifier = request.email.lower()
  744. cutoff = datetime.now(timezone.utc) - _PWD_RESET_SEND_WINDOW
  745. rate_result = await db.execute(
  746. select(AuthRateLimitEvent).where(
  747. AuthRateLimitEvent.username == identifier,
  748. AuthRateLimitEvent.event_type == EventType.PASSWORD_RESET_SEND,
  749. AuthRateLimitEvent.occurred_at > cutoff,
  750. )
  751. )
  752. if len(rate_result.scalars().all()) >= _MAX_PWD_RESET_SENDS:
  753. raise HTTPException(
  754. status_code=status.HTTP_429_TOO_MANY_REQUESTS,
  755. detail=f"Too many password reset requests. Please wait {_PWD_RESET_SEND_WINDOW.seconds // 60} minutes.",
  756. )
  757. # L-NEW-6: per-IP rate limit — prevents mass-reset flooding across many
  758. # different email addresses from a single source IP.
  759. client_ip = _get_client_ip(raw_request)
  760. ip_rate_result = await db.execute(
  761. select(AuthRateLimitEvent).where(
  762. AuthRateLimitEvent.username == client_ip,
  763. AuthRateLimitEvent.event_type == EventType.PASSWORD_RESET_IP,
  764. AuthRateLimitEvent.occurred_at > cutoff,
  765. )
  766. )
  767. if len(ip_rate_result.scalars().all()) >= _MAX_PWD_RESET_SENDS_PER_IP:
  768. raise HTTPException(
  769. status_code=status.HTTP_429_TOO_MANY_REQUESTS,
  770. detail=f"Too many password reset requests. Please wait {_PWD_RESET_SEND_WINDOW.seconds // 60} minutes.",
  771. )
  772. # Nit7: Always record the IP-level event (prevents spray attacks across many
  773. # different email addresses from one IP). The email-level event is only
  774. # recorded when we actually send an email to a local user — LDAP/OIDC users
  775. # do not consume a slot because this flow is a no-op for them.
  776. db.add(AuthRateLimitEvent(username=client_ip, event_type=EventType.PASSWORD_RESET_IP))
  777. await db.commit()
  778. # Get SMTP settings
  779. smtp_settings = await get_smtp_settings(db)
  780. if not smtp_settings:
  781. raise HTTPException(
  782. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  783. detail="Email service is not configured",
  784. )
  785. # Find user by email — always return success to prevent email enumeration.
  786. user = await get_user_by_email(db, request.email)
  787. # M-1: exclude LDAP and OIDC users — they must use their respective provider.
  788. if user and user.is_active and user.auth_source not in ("ldap", "oidc"):
  789. try:
  790. # Record email-level slot only for local users who will actually receive
  791. # the reset email (Nit7: don't waste the user's quota for LDAP/OIDC no-ops).
  792. db.add(AuthRateLimitEvent(username=identifier, event_type=EventType.PASSWORD_RESET_SEND))
  793. now = datetime.now(timezone.utc)
  794. # Prune any outstanding reset tokens for this user before issuing a new one.
  795. await db.execute(
  796. delete(AuthEphemeralToken).where(
  797. AuthEphemeralToken.token_type == TokenType.PASSWORD_RESET,
  798. AuthEphemeralToken.username == user.username,
  799. )
  800. )
  801. reset_token = secrets.token_urlsafe(32)
  802. db.add(
  803. AuthEphemeralToken(
  804. token=reset_token,
  805. token_type=TokenType.PASSWORD_RESET,
  806. username=user.username,
  807. expires_at=now + _RESET_TOKEN_TTL,
  808. )
  809. )
  810. await db.commit()
  811. login_url = await get_external_login_url(db)
  812. # M-B: Deliver token in the URL fragment so it never reaches the server
  813. # in access-logs or Referer headers (mirrors H-4 for the OIDC token).
  814. reset_url = f"{login_url}#reset_token={reset_token}"
  815. subject, text_body, html_body = await create_password_reset_link_email_from_template(
  816. db, user.username, reset_url
  817. )
  818. # L-R9-B: send asynchronously so response time is independent of
  819. # whether the user exists (prevents email-existence timing oracle).
  820. # C1: wrapper deletes the token if SMTP fails so the user can re-request.
  821. background_tasks.add_task(
  822. _send_reset_email_or_delete_token,
  823. reset_token,
  824. smtp_settings,
  825. user.email,
  826. subject,
  827. text_body,
  828. html_body,
  829. "forgot_password",
  830. )
  831. _logger.info("Password reset email queued for %s", user.email)
  832. except Exception as e:
  833. _logger.error("Failed to send password reset email: %s", e)
  834. # Don't reveal error to caller for security
  835. return ForgotPasswordResponse(
  836. message="If the email address is associated with an account, a password reset email has been sent."
  837. )
  838. @router.post("/forgot-password/confirm", response_model=ForgotPasswordResponse)
  839. async def forgot_password_confirm(request: ForgotPasswordConfirmRequest, db: AsyncSession = Depends(get_db)):
  840. """Complete a password reset by supplying the token from the reset email.
  841. H-6: Atomically consumes the single-use token (DELETE…RETURNING) and sets
  842. the new password. Expired or already-used tokens are silently rejected with
  843. the same response to prevent oracle attacks.
  844. """
  845. now = datetime.now(timezone.utc)
  846. result = await db.execute(
  847. delete(AuthEphemeralToken)
  848. .where(
  849. AuthEphemeralToken.token == request.token,
  850. AuthEphemeralToken.token_type == TokenType.PASSWORD_RESET,
  851. )
  852. .returning(AuthEphemeralToken.username, AuthEphemeralToken.expires_at)
  853. )
  854. row = result.one_or_none()
  855. await db.commit()
  856. if row is None:
  857. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid or expired password reset token")
  858. username, expires_at = row
  859. # SQLite returns naive datetimes; treat them as UTC.
  860. if expires_at.tzinfo is None:
  861. expires_at = expires_at.replace(tzinfo=timezone.utc)
  862. if now > expires_at:
  863. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid or expired password reset token")
  864. user = await get_user_by_username(db, username)
  865. # M-1: block LDAP/OIDC users — they authenticate via their provider, not local password.
  866. if not user or not user.is_active or user.auth_source in ("ldap", "oidc"):
  867. raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid or expired password reset token")
  868. user.password_hash = get_password_hash(request.new_password)
  869. user.password_changed_at = now # M-R7-B: invalidate all prior JWTs
  870. await db.commit()
  871. _logger.info("Password reset completed for user '%s'", username)
  872. return ForgotPasswordResponse(message="Password has been reset successfully.")
  873. @router.post("/reset-password", response_model=ResetPasswordResponse)
  874. async def reset_user_password(
  875. request: ResetPasswordRequest,
  876. background_tasks: BackgroundTasks,
  877. current_user: User = Depends(get_current_active_user),
  878. db: AsyncSession = Depends(get_db),
  879. ):
  880. """Reset a user's password and send them an email (admin only, advanced auth only)."""
  881. # Reload user with groups for proper is_admin check
  882. result = await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  883. admin_user = result.scalar_one()
  884. if not admin_user.is_admin:
  885. raise HTTPException(
  886. status_code=status.HTTP_403_FORBIDDEN,
  887. detail="Only admins can reset user passwords",
  888. )
  889. # Check if advanced auth is enabled
  890. advanced_auth = await is_advanced_auth_enabled(db)
  891. if not advanced_auth:
  892. raise HTTPException(
  893. status_code=status.HTTP_400_BAD_REQUEST,
  894. detail="Advanced authentication is not enabled",
  895. )
  896. # Get SMTP settings
  897. smtp_settings = await get_smtp_settings(db)
  898. if not smtp_settings:
  899. raise HTTPException(
  900. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  901. detail="Email service is not configured",
  902. )
  903. # Find user to reset
  904. result = await db.execute(select(User).where(User.id == request.user_id))
  905. user = result.scalar_one_or_none()
  906. if not user:
  907. raise HTTPException(
  908. status_code=status.HTTP_404_NOT_FOUND,
  909. detail="User not found",
  910. )
  911. # M-1: block LDAP/OIDC users — passwords are managed by their respective providers.
  912. if user.auth_source in ("ldap", "oidc"):
  913. raise HTTPException(
  914. status_code=status.HTTP_400_BAD_REQUEST,
  915. detail="Cannot reset password for LDAP/OIDC users — authentication is managed by their provider",
  916. )
  917. if not user.email:
  918. raise HTTPException(
  919. status_code=status.HTTP_400_BAD_REQUEST,
  920. detail="User does not have an email address configured",
  921. )
  922. try:
  923. # H-B: Issue a single-use reset link instead of generating a plaintext password.
  924. # The admin never sees the credential — the user sets their own password.
  925. now = datetime.now(timezone.utc)
  926. await db.execute(
  927. delete(AuthEphemeralToken).where(
  928. AuthEphemeralToken.token_type == TokenType.PASSWORD_RESET,
  929. AuthEphemeralToken.username == user.username,
  930. )
  931. )
  932. reset_token = secrets.token_urlsafe(32)
  933. db.add(
  934. AuthEphemeralToken(
  935. token=reset_token,
  936. token_type=TokenType.PASSWORD_RESET,
  937. username=user.username,
  938. expires_at=now + _RESET_TOKEN_TTL,
  939. )
  940. )
  941. await db.commit()
  942. login_url = await get_external_login_url(db)
  943. reset_url = f"{login_url}#reset_token={reset_token}"
  944. subject, text_body, html_body = await create_password_reset_link_email_from_template(
  945. db, user.username, reset_url
  946. )
  947. background_tasks.add_task(
  948. _send_reset_email_or_delete_token,
  949. reset_token,
  950. smtp_settings,
  951. user.email,
  952. subject,
  953. text_body,
  954. html_body,
  955. "admin_reset",
  956. )
  957. _logger.info("Admin password reset link queued for user '%s' by admin '%s'", user.username, admin_user.username)
  958. return ResetPasswordResponse(message=f"Password reset link sent to {user.email}")
  959. except Exception as e:
  960. await db.rollback()
  961. _logger.error("Failed to send admin password reset for user '%s': %s", user.username, e)
  962. raise HTTPException(
  963. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  964. detail="Failed to send password reset link. Check server logs.", # L-R7-B: no internal details
  965. )
  966. # LDAP Authentication Helpers
  967. async def _get_ldap_settings(db: AsyncSession) -> dict[str, str] | None:
  968. """Get LDAP settings from the database. Returns None if LDAP is not enabled."""
  969. ldap_keys = [
  970. "ldap_enabled",
  971. "ldap_server_url",
  972. "ldap_bind_dn",
  973. "ldap_bind_password",
  974. "ldap_search_base",
  975. "ldap_user_filter",
  976. "ldap_security",
  977. "ldap_group_mapping",
  978. "ldap_auto_provision",
  979. "ldap_ca_cert_path",
  980. "ldap_default_group",
  981. ]
  982. result = await db.execute(select(Settings).where(Settings.key.in_(ldap_keys)))
  983. settings = {s.key: s.value for s in result.scalars().all()}
  984. if settings.get("ldap_enabled", "false").lower() != "true":
  985. return None
  986. return settings
  987. async def _provision_ldap_user(db: AsyncSession, ldap_user, ldap_config) -> User:
  988. """Create a new local user from LDAP authentication."""
  989. import logging
  990. from backend.app.services.ldap_service import resolve_group_mapping
  991. logger = logging.getLogger(__name__)
  992. new_user = User(
  993. username=ldap_user.username,
  994. email=ldap_user.email,
  995. password_hash=None,
  996. role="user",
  997. auth_source="ldap",
  998. is_active=True,
  999. )
  1000. # Map LDAP groups to BamBuddy groups, falling back to the configured default group
  1001. # when the user is authenticated but has no matching group mapping (#921-follow-up).
  1002. mapped_group_names = resolve_group_mapping(ldap_user.groups, ldap_config.group_mapping)
  1003. if not mapped_group_names and ldap_config.default_group:
  1004. mapped_group_names = [ldap_config.default_group]
  1005. logger.warning(
  1006. "LDAP user %s has no mapped groups — assigning configured default group '%s'",
  1007. ldap_user.username,
  1008. ldap_config.default_group,
  1009. )
  1010. if mapped_group_names:
  1011. groups_result = await db.execute(select(Group).where(Group.name.in_(mapped_group_names)))
  1012. new_user.groups = list(groups_result.scalars().all())
  1013. db.add(new_user)
  1014. await db.commit()
  1015. await db.refresh(new_user)
  1016. logger.info("Auto-provisioned LDAP user: %s (groups: %s)", new_user.username, mapped_group_names)
  1017. return new_user
  1018. async def _sync_ldap_user(db: AsyncSession, user: User, ldap_user, ldap_config) -> None:
  1019. """Sync LDAP user attributes (email, groups) on each login.
  1020. Group sync only touches BamBuddy groups that LDAP is configured to manage —
  1021. that is, the values of `group_mapping` plus `default_group`. Any group
  1022. outside that set is assumed to be a manual admin assignment and is
  1023. preserved across logins (#1292). Manual assignments to a BamBuddy group
  1024. that IS LDAP-managed are still overridden by LDAP truth, because revoking
  1025. access in LDAP must propagate to BamBuddy on next login.
  1026. """
  1027. import logging
  1028. from backend.app.services.ldap_service import resolve_group_mapping
  1029. logger = logging.getLogger(__name__)
  1030. changed = False
  1031. # Update email if changed
  1032. if ldap_user.email and ldap_user.email != user.email:
  1033. user.email = ldap_user.email
  1034. changed = True
  1035. # Compute the set of BamBuddy groups LDAP is allowed to manage. Anything
  1036. # outside this set is left alone so manual admin assignments survive logins.
  1037. ldap_managed_names: set[str] = set(ldap_config.group_mapping.values())
  1038. if ldap_config.default_group:
  1039. ldap_managed_names.add(ldap_config.default_group)
  1040. # Resolve what LDAP says the user should currently be in.
  1041. mapped_group_names = resolve_group_mapping(ldap_user.groups, ldap_config.group_mapping)
  1042. if not mapped_group_names and ldap_config.default_group:
  1043. mapped_group_names = [ldap_config.default_group]
  1044. logger.warning(
  1045. "LDAP user %s has no mapped groups — assigning configured default group '%s'",
  1046. user.username,
  1047. ldap_config.default_group,
  1048. )
  1049. if mapped_group_names:
  1050. groups_result = await db.execute(select(Group).where(Group.name.in_(mapped_group_names)))
  1051. new_ldap_groups = list(groups_result.scalars().all())
  1052. else:
  1053. new_ldap_groups = []
  1054. # Preserve manual assignments to non-LDAP-managed groups; replace only
  1055. # the LDAP-managed slice with the resolved set.
  1056. preserved_manual_groups = [g for g in user.groups if g.name not in ldap_managed_names]
  1057. new_groups = preserved_manual_groups + new_ldap_groups
  1058. current_group_ids = {g.id for g in user.groups}
  1059. new_group_ids = {g.id for g in new_groups}
  1060. if current_group_ids != new_group_ids:
  1061. user.groups = new_groups
  1062. changed = True
  1063. if changed:
  1064. await db.commit()
  1065. logger.info("Synced LDAP user attributes: %s", user.username)
  1066. @router.post("/ldap/test")
  1067. async def test_ldap(
  1068. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1069. db: AsyncSession = Depends(get_db),
  1070. ):
  1071. """Test LDAP connection using saved settings (admin only when auth enabled)."""
  1072. import logging
  1073. from backend.app.services.ldap_service import parse_ldap_config, test_ldap_connection
  1074. logger = logging.getLogger(__name__)
  1075. ldap_settings = await _get_ldap_settings(db)
  1076. if not ldap_settings:
  1077. # LDAP might not be enabled yet but settings might still exist — read all keys
  1078. ldap_keys = [
  1079. "ldap_enabled",
  1080. "ldap_server_url",
  1081. "ldap_bind_dn",
  1082. "ldap_bind_password",
  1083. "ldap_search_base",
  1084. "ldap_user_filter",
  1085. "ldap_security",
  1086. "ldap_group_mapping",
  1087. "ldap_auto_provision",
  1088. ]
  1089. result = await db.execute(select(Settings).where(Settings.key.in_(ldap_keys)))
  1090. ldap_settings = {s.key: s.value for s in result.scalars().all()}
  1091. # Force enabled for test
  1092. ldap_settings["ldap_enabled"] = "true"
  1093. config = parse_ldap_config(ldap_settings)
  1094. if not config:
  1095. return {"success": False, "message": "LDAP server URL is not configured"}
  1096. success, message = test_ldap_connection(config)
  1097. if success:
  1098. logger.info("LDAP connection test successful")
  1099. else:
  1100. logger.warning("LDAP connection test failed: %s", message)
  1101. return {"success": success, "message": message}
  1102. @router.get("/ldap/status")
  1103. async def get_ldap_status(db: AsyncSession = Depends(get_db)):
  1104. """Get LDAP authentication status."""
  1105. # Only fetch the minimum keys needed — never load secrets
  1106. ldap_keys = ["ldap_enabled", "ldap_server_url"]
  1107. result = await db.execute(select(Settings).where(Settings.key.in_(ldap_keys)))
  1108. settings = {s.key: s.value for s in result.scalars().all()}
  1109. return {
  1110. "ldap_enabled": settings.get("ldap_enabled", "false").lower() == "true",
  1111. "ldap_configured": bool(settings.get("ldap_server_url")),
  1112. }
  1113. # =============================================================================
  1114. # Manual LDAP user provisioning (#1298)
  1115. # =============================================================================
  1116. # Admins can search the directory and provision users directly from the UI
  1117. # without enabling auto-provision on login. The two endpoints below pair with
  1118. # the new "LDAP" tab in the user-create modal.
  1119. @router.get("/ldap/search", response_model=list[LDAPSearchResultResponse])
  1120. async def search_ldap_directory(
  1121. q: str,
  1122. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_CREATE),
  1123. db: AsyncSession = Depends(get_db),
  1124. ):
  1125. """Search the LDAP directory for users matching `q`.
  1126. Returns up to 25 candidates. The query is matched (case-insensitively, with
  1127. wildcards on both sides) against sAMAccountName, uid, mail, displayName,
  1128. and cn — covering both AD and OpenLDAP layouts. Each result is annotated
  1129. with `already_provisioned` so the UI can grey out usernames that already
  1130. exist as BamBuddy users.
  1131. Requires USERS_CREATE permission. Minimum query length is 2 characters.
  1132. """
  1133. from sqlalchemy import func as sa_func
  1134. from backend.app.services.ldap_service import parse_ldap_config, search_ldap_users
  1135. query = q.strip()
  1136. if len(query) < 2:
  1137. raise HTTPException(
  1138. status_code=status.HTTP_400_BAD_REQUEST,
  1139. detail="Query must be at least 2 characters",
  1140. )
  1141. ldap_settings = await _get_ldap_settings(db)
  1142. if not ldap_settings:
  1143. raise HTTPException(
  1144. status_code=status.HTTP_400_BAD_REQUEST,
  1145. detail="LDAP is not enabled",
  1146. )
  1147. config = parse_ldap_config(ldap_settings)
  1148. if not config:
  1149. raise HTTPException(
  1150. status_code=status.HTTP_400_BAD_REQUEST,
  1151. detail="LDAP server URL is not configured",
  1152. )
  1153. try:
  1154. results = search_ldap_users(config, query, limit=25)
  1155. except Exception as e:
  1156. _logger.exception("LDAP directory search failed")
  1157. # Admin-only endpoint — surface the underlying reason so the operator
  1158. # can fix it (auth_middleware already restricted access to USERS_CREATE).
  1159. raise HTTPException(
  1160. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  1161. detail=f"LDAP search failed: {type(e).__name__}: {e}",
  1162. )
  1163. if not results:
  1164. return []
  1165. # Annotate `already_provisioned` so the SPA can dim/disable rows that map
  1166. # to an existing local row. Case-insensitive lookup mirrors create_user.
  1167. usernames_lower = [r.username.lower() for r in results]
  1168. existing_query = await db.execute(select(User.username).where(sa_func.lower(User.username).in_(usernames_lower)))
  1169. existing_lower = {str(name).lower() for name in existing_query.scalars().all()}
  1170. return [
  1171. LDAPSearchResultResponse(
  1172. username=r.username,
  1173. email=r.email,
  1174. display_name=r.display_name,
  1175. dn=r.dn,
  1176. already_provisioned=r.username.lower() in existing_lower,
  1177. )
  1178. for r in results
  1179. ]
  1180. @router.post("/ldap/provision", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
  1181. async def provision_ldap_user(
  1182. payload: LDAPProvisionRequest,
  1183. _: User | None = RequirePermissionIfAuthEnabled(Permission.USERS_CREATE),
  1184. db: AsyncSession = Depends(get_db),
  1185. ):
  1186. """Provision a BamBuddy user from an existing LDAP directory entry.
  1187. Re-resolves the username via the service-account bind (rather than trusting
  1188. the request body) so group mappings and email come from a fresh LDAP read.
  1189. Applies the same group-mapping / default-group logic as the auto-provision
  1190. login path (`_provision_ldap_user`), so behavior stays identical regardless
  1191. of whether the user was created here or on first login.
  1192. Requires USERS_CREATE.
  1193. """
  1194. from sqlalchemy import func as sa_func
  1195. from backend.app.services.ldap_service import lookup_ldap_user, parse_ldap_config
  1196. username = payload.username.strip()
  1197. if not username:
  1198. raise HTTPException(
  1199. status_code=status.HTTP_400_BAD_REQUEST,
  1200. detail="Username is required",
  1201. )
  1202. ldap_settings = await _get_ldap_settings(db)
  1203. if not ldap_settings:
  1204. raise HTTPException(
  1205. status_code=status.HTTP_400_BAD_REQUEST,
  1206. detail="LDAP is not enabled",
  1207. )
  1208. config = parse_ldap_config(ldap_settings)
  1209. if not config:
  1210. raise HTTPException(
  1211. status_code=status.HTTP_400_BAD_REQUEST,
  1212. detail="LDAP server URL is not configured",
  1213. )
  1214. # Look up via service bind. Service-bind failures bubble up as 503; missing
  1215. # entries surface as 404 to distinguish "directory unreachable" from
  1216. # "username doesn't exist in the directory" in the UI.
  1217. try:
  1218. ldap_user = lookup_ldap_user(config, username)
  1219. except Exception as e:
  1220. _logger.exception("LDAP lookup failed during provision")
  1221. raise HTTPException(
  1222. status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
  1223. detail=f"LDAP lookup failed: {type(e).__name__}: {e}",
  1224. )
  1225. if ldap_user is None:
  1226. raise HTTPException(
  1227. status_code=status.HTTP_404_NOT_FOUND,
  1228. detail=f"User '{username}' not found in LDAP directory",
  1229. )
  1230. # Reject duplicates — the canonical username from LDAP is what gets stored,
  1231. # so the conflict check uses that rather than the request payload.
  1232. existing = await db.execute(select(User).where(sa_func.lower(User.username) == sa_func.lower(ldap_user.username)))
  1233. existing_user = existing.scalar_one_or_none()
  1234. if existing_user is not None:
  1235. if existing_user.auth_source == "ldap":
  1236. detail = f"LDAP user '{ldap_user.username}' is already provisioned"
  1237. else:
  1238. detail = f"A local user with the username '{ldap_user.username}' already exists"
  1239. raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=detail)
  1240. new_user = await _provision_ldap_user(db, ldap_user, config)
  1241. # Reload with groups eagerly loaded so _user_to_response can serialize them
  1242. # without lazy-load warnings (matches create_user / list_users pattern).
  1243. result = await db.execute(select(User).where(User.id == new_user.id).options(selectinload(User.groups)))
  1244. new_user = result.scalar_one()
  1245. _logger.info("Manually provisioned LDAP user %s (id=%d)", new_user.username, new_user.id)
  1246. return _user_to_response(new_user)
  1247. # =============================================================================
  1248. # Long-lived camera-stream tokens (#1108)
  1249. # =============================================================================
  1250. # Camera-only V1. Issue scope: a token a user can paste into Home Assistant /
  1251. # Frigate / a kiosk and have it keep working for days/weeks rather than
  1252. # refreshing the 60-minute ephemeral token. Permission gate: CAMERA_VIEW
  1253. # (same blast radius as the existing 60-min token-mint endpoint).
  1254. def _long_lived_token_to_response(record, *, plaintext: str | None = None) -> dict:
  1255. """Serialise a LongLivedToken row for the SPA. Plaintext is included
  1256. only at create time (and then never again), per the issue's "shown once"
  1257. contract.
  1258. """
  1259. return {
  1260. "id": record.id,
  1261. "user_id": record.user_id,
  1262. "name": record.name,
  1263. "scope": record.scope,
  1264. "lookup_prefix": record.lookup_prefix,
  1265. "created_at": record.created_at.isoformat() if record.created_at else None,
  1266. "expires_at": record.expires_at.isoformat() if record.expires_at else None,
  1267. "last_used_at": record.last_used_at.isoformat() if record.last_used_at else None,
  1268. # Plaintext is the ONLY field the user ever sees in full — copied once
  1269. # to a clipboard / kiosk config and then forgotten.
  1270. "token": plaintext,
  1271. }
  1272. @router.post("/tokens", response_model=dict, status_code=status.HTTP_201_CREATED)
  1273. async def create_long_lived_camera_token(
  1274. payload: dict,
  1275. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.CAMERA_VIEW),
  1276. db: AsyncSession = Depends(get_db),
  1277. ):
  1278. """Mint a long-lived camera-stream token (#1108).
  1279. Body: ``{"name": str, "expires_in_days": int, "scope": "camera_stream"}``.
  1280. The plaintext token is returned **exactly once** in the response. The DB
  1281. only ever stores a pbkdf2 hash, so a leaked DB dump cannot replay the
  1282. token. Hard cap of 365 days; the issue's ``expire_in: 0`` (never) is
  1283. explicitly rejected.
  1284. """
  1285. from backend.app.services.long_lived_tokens import (
  1286. ALLOWED_SCOPES,
  1287. MAX_TOKEN_LIFETIME_DAYS,
  1288. create_token,
  1289. )
  1290. # Auth-disabled path: tokens are user-owned, but if auth is off there is
  1291. # no user to own them. Refuse rather than silently picking a random user.
  1292. if current_user is None:
  1293. raise HTTPException(
  1294. status_code=status.HTTP_403_FORBIDDEN,
  1295. detail="Long-lived tokens require authentication to be enabled",
  1296. )
  1297. name = payload.get("name")
  1298. if not isinstance(name, str) or not name.strip():
  1299. raise HTTPException(status_code=400, detail="name is required")
  1300. expires_in_days = payload.get("expires_in_days")
  1301. if not isinstance(expires_in_days, int) or expires_in_days <= 0:
  1302. raise HTTPException(
  1303. status_code=400,
  1304. detail=(
  1305. f"expires_in_days must be a positive integer (max {MAX_TOKEN_LIFETIME_DAYS}; #1108: no infinite tokens)"
  1306. ),
  1307. )
  1308. scope = payload.get("scope", "camera_stream")
  1309. if scope not in ALLOWED_SCOPES:
  1310. raise HTTPException(status_code=400, detail=f"unsupported scope: {scope!r}")
  1311. try:
  1312. created = await create_token(
  1313. db,
  1314. user_id=current_user.id,
  1315. name=name,
  1316. expires_in_days=expires_in_days,
  1317. scope=scope,
  1318. )
  1319. except ValueError as e:
  1320. raise HTTPException(status_code=400, detail=str(e))
  1321. _logger.info(
  1322. "Long-lived camera token created: user=%s name=%r scope=%s expires=%s",
  1323. current_user.username,
  1324. name,
  1325. scope,
  1326. created.record.expires_at.isoformat(),
  1327. )
  1328. return _long_lived_token_to_response(created.record, plaintext=created.plaintext)
  1329. @router.get("/tokens", response_model=list[dict])
  1330. async def list_long_lived_tokens(
  1331. user_id: int | None = None,
  1332. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.CAMERA_VIEW),
  1333. db: AsyncSession = Depends(get_db),
  1334. ):
  1335. """List long-lived tokens.
  1336. Default: caller's own tokens.
  1337. Admins can pass ``?user_id=N`` to see another user's tokens, or omit it
  1338. to see everything (handy for leak triage).
  1339. """
  1340. from backend.app.services.long_lived_tokens import list_user_tokens
  1341. # Auth-disabled installs don't have a notion of "my tokens" — refuse so
  1342. # we don't leak a global list to whoever can hit the API.
  1343. if current_user is None:
  1344. raise HTTPException(
  1345. status_code=status.HTTP_403_FORBIDDEN,
  1346. detail="Long-lived tokens require authentication to be enabled",
  1347. )
  1348. # Reload with groups so is_admin reflects group membership reliably.
  1349. user_with_groups = (
  1350. await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  1351. ).scalar_one()
  1352. if user_id is None or user_id == current_user.id:
  1353. records = await list_user_tokens(db, current_user.id)
  1354. elif user_with_groups.is_admin:
  1355. records = await list_user_tokens(db, user_id)
  1356. else:
  1357. raise HTTPException(
  1358. status_code=status.HTTP_403_FORBIDDEN,
  1359. detail="Only admins can list other users' tokens",
  1360. )
  1361. return [_long_lived_token_to_response(r) for r in records]
  1362. @router.get("/tokens/all", response_model=list[dict])
  1363. async def list_all_long_lived_tokens(
  1364. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.CAMERA_VIEW),
  1365. db: AsyncSession = Depends(get_db),
  1366. ):
  1367. """Admin-only: every active long-lived token in the system, newest first.
  1368. Used by the leak-triage view in admin settings.
  1369. """
  1370. from backend.app.services.long_lived_tokens import list_all_tokens
  1371. if current_user is None:
  1372. raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Auth required")
  1373. user_with_groups = (
  1374. await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  1375. ).scalar_one()
  1376. if not user_with_groups.is_admin:
  1377. raise HTTPException(
  1378. status_code=status.HTTP_403_FORBIDDEN,
  1379. detail="Admin only",
  1380. )
  1381. records = await list_all_tokens(db)
  1382. return [_long_lived_token_to_response(r) for r in records]
  1383. @router.delete("/tokens/{token_id}", status_code=status.HTTP_204_NO_CONTENT)
  1384. async def revoke_long_lived_token(
  1385. token_id: int,
  1386. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.CAMERA_VIEW),
  1387. db: AsyncSession = Depends(get_db),
  1388. ):
  1389. """Revoke a long-lived token. Owners can revoke their own; admins any."""
  1390. from backend.app.models.long_lived_token import LongLivedToken
  1391. from backend.app.services.long_lived_tokens import revoke_token
  1392. if current_user is None:
  1393. raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Auth required")
  1394. record = (await db.execute(select(LongLivedToken).where(LongLivedToken.id == token_id))).scalar_one_or_none()
  1395. if record is None:
  1396. raise HTTPException(status_code=404, detail="Token not found")
  1397. if record.user_id != current_user.id:
  1398. # Reload for is_admin so admins can revoke any user's token (leak response).
  1399. user_with_groups = (
  1400. await db.execute(select(User).where(User.id == current_user.id).options(selectinload(User.groups)))
  1401. ).scalar_one()
  1402. if not user_with_groups.is_admin:
  1403. raise HTTPException(
  1404. status_code=status.HTTP_403_FORBIDDEN,
  1405. detail="You can only revoke your own tokens",
  1406. )
  1407. revoked = await revoke_token(db, token_id)
  1408. if not revoked:
  1409. # Already revoked is treated as 404 for idempotency from the UI side.
  1410. raise HTTPException(status_code=404, detail="Token not found or already revoked")
  1411. _logger.info(
  1412. "Long-lived camera token revoked: id=%d by user=%s",
  1413. token_id,
  1414. current_user.username,
  1415. )
  1416. return Response(status_code=status.HTTP_204_NO_CONTENT)
  1417. @router.get("/encryption-status", response_model=EncryptionStatusResponse)
  1418. async def get_encryption_status(
  1419. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  1420. db: AsyncSession = Depends(get_db),
  1421. ) -> EncryptionStatusResponse:
  1422. """Report at-rest encryption status for OIDC + TOTP secrets.
  1423. Surfaces:
  1424. (a) whether a key is configured and where it came from
  1425. (b) how many rows are still legacy plaintext
  1426. (c) whether decryption is broken (no key OR key cannot decrypt existing rows)
  1427. (d) the count of rows skipped during the last re-encryption migration
  1428. S2: gated on SETTINGS_UPDATE so Viewers (who only have SETTINGS_READ)
  1429. cannot read encryption-status — admin/operator only.
  1430. """
  1431. from sqlalchemy import case, func, not_, select
  1432. from backend.app.core.database import get_migration_error_count
  1433. from backend.app.core.encryption import get_key_source, is_encryption_active, mfa_decrypt
  1434. from backend.app.models.oidc_provider import OIDCProvider
  1435. from backend.app.models.user_totp import UserTOTP
  1436. key_configured = is_encryption_active()
  1437. key_source = get_key_source() or "none"
  1438. try:
  1439. oidc_row = await db.execute(
  1440. select(
  1441. func.sum(case((not_(OIDCProvider._client_secret_enc.like("fernet:%")), 1), else_=0)),
  1442. func.sum(case((OIDCProvider._client_secret_enc.like("fernet:%"), 1), else_=0)),
  1443. )
  1444. )
  1445. legacy_oidc, encrypted_oidc = oidc_row.one()
  1446. totp_row = await db.execute(
  1447. select(
  1448. func.sum(case((not_(UserTOTP._secret_enc.like("fernet:%")), 1), else_=0)),
  1449. func.sum(case((UserTOTP._secret_enc.like("fernet:%"), 1), else_=0)),
  1450. )
  1451. )
  1452. legacy_totp, encrypted_totp = totp_row.one()
  1453. except SQLAlchemyError:
  1454. _logger.exception("Failed to query encryption row counts")
  1455. raise HTTPException(status_code=500, detail="Failed to retrieve encryption status")
  1456. legacy_plaintext_rows = EncryptionRowCounts(
  1457. oidc_providers=int(legacy_oidc or 0),
  1458. user_totp=int(legacy_totp or 0),
  1459. )
  1460. encrypted_rows = EncryptionRowCounts(
  1461. oidc_providers=int(encrypted_oidc or 0),
  1462. user_totp=int(encrypted_totp or 0),
  1463. )
  1464. # B4: detect "wrong key" state — sample-decrypt one encrypted row to
  1465. # distinguish "no key" from "key configured but cannot decrypt these rows".
  1466. # The legacy computed-field check (key_configured=False AND encrypted>0)
  1467. # missed the case where an operator pasted a different valid Fernet key
  1468. # (rotation, cross-deployment restore, env override) — status would show
  1469. # green while every encrypted row was unrecoverable.
  1470. decryption_broken = False
  1471. total_encrypted = encrypted_rows.oidc_providers + encrypted_rows.user_totp
  1472. if not key_configured and total_encrypted > 0:
  1473. decryption_broken = True
  1474. elif key_configured and total_encrypted > 0:
  1475. sample_value: str | None = None
  1476. try:
  1477. if encrypted_rows.oidc_providers > 0:
  1478. r = await db.execute(
  1479. select(OIDCProvider._client_secret_enc)
  1480. .where(OIDCProvider._client_secret_enc.like("fernet:%"))
  1481. .limit(1)
  1482. )
  1483. sample_value = r.scalar_one_or_none()
  1484. if sample_value is None and encrypted_rows.user_totp > 0:
  1485. r = await db.execute(select(UserTOTP._secret_enc).where(UserTOTP._secret_enc.like("fernet:%")).limit(1))
  1486. sample_value = r.scalar_one_or_none()
  1487. except SQLAlchemyError:
  1488. _logger.exception("Failed to query sample encrypted row for decryption probe")
  1489. # Over-alert is safer than silent corruption — surface as broken.
  1490. decryption_broken = True
  1491. sample_value = None
  1492. if sample_value:
  1493. try:
  1494. mfa_decrypt(sample_value)
  1495. except RuntimeError:
  1496. decryption_broken = True
  1497. return EncryptionStatusResponse(
  1498. key_configured=key_configured,
  1499. key_source=key_source,
  1500. legacy_plaintext_rows=legacy_plaintext_rows,
  1501. encrypted_rows=encrypted_rows,
  1502. decryption_broken=decryption_broken,
  1503. migration_error_count=get_migration_error_count(),
  1504. )