auth.py 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. import secrets
  5. from datetime import datetime, timedelta, timezone
  6. from typing import Annotated
  7. import jwt
  8. from fastapi import Depends, Header, HTTPException, status
  9. from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
  10. from jwt.exceptions import PyJWTError as JWTError
  11. from passlib.context import CryptContext
  12. from sqlalchemy import delete, func, select
  13. from sqlalchemy.ext.asyncio import AsyncSession
  14. from sqlalchemy.orm import selectinload
  15. from backend.app.core.database import async_session, get_db
  16. from backend.app.core.permissions import Permission
  17. from backend.app.models.api_key import APIKey
  18. from backend.app.models.auth_ephemeral import AuthEphemeralToken, TokenType
  19. from backend.app.models.settings import Settings
  20. from backend.app.models.user import User
  21. logger = logging.getLogger(__name__)
  22. # GHSA-r2qv-8222-hqg3 (CVSS 9.9) — API key permission enforcement is allowlist-based.
  23. #
  24. # Until 0.2.4.x, ``_check_apikey_permissions`` only consulted the admin denylist
  25. # below. The three documented scope flags on ``APIKey``
  26. # (``can_read_status`` / ``can_queue`` / ``can_control_printer`` / ``can_manage_library``)
  27. # were enforced only by ``check_permission()`` inside ``routes/webhook.py``;
  28. # every other route used ``require_permission_if_auth_enabled`` which fell
  29. # through to the denylist-only path, so an API key with all flags unchecked
  30. # could still stop prints, edit queue items, and read every endpoint not in
  31. # this set. ``require_any_permission_if_auth_enabled`` and
  32. # ``require_ownership_permission`` did not call this helper at all, so admin
  33. # "any-of" routes and ownership-modify routes were entirely ungated for API keys.
  34. #
  35. # Fix: ``_check_apikey_permissions`` now requires every requested permission to
  36. # be present in ``_APIKEY_SCOPE_BY_PERMISSION`` (allowlist), and gates on the
  37. # corresponding scope flag on the API key. Unmapped permissions = 403. This
  38. # means a Permission added to ``core/permissions.py`` without a matching entry
  39. # in ``_APIKEY_SCOPE_BY_PERMISSION`` is automatically denied for API keys —
  40. # the previous denylist shape allowed every new Permission to silently widen
  41. # the API-key surface.
  42. #
  43. # The denylist is retained for documentation / drift-detection only — its
  44. # entries also satisfy "not in the allowlist", so they fail closed regardless.
  45. #
  46. # Mapping rationale (see wiki/features/api-keys.md):
  47. # can_read_status → every ``*_READ`` + camera + stats + system + websocket
  48. # can_queue → queue write ops + archive reprint
  49. # can_control_printer → physical printer + smart-plug control
  50. # can_manage_library → library upload/own + MakerWorld import (separate
  51. # trust level from queue management, hence its own flag)
  52. # admin-only → unmapped (default-deny); covers all create/update/
  53. # delete of admin resources, settings writes, user/
  54. # group/api-key/backup admin ops, discovery scan,
  55. # cloud auth, library ALL-ownership perms, purges
  56. _APIKEY_SCOPE_BY_PERMISSION: dict[Permission, str] = {
  57. # can_read_status — read-only access to status, history, and configuration
  58. Permission.PRINTERS_READ: "can_read_status",
  59. Permission.ARCHIVES_READ: "can_read_status",
  60. Permission.QUEUE_READ: "can_read_status",
  61. Permission.LIBRARY_READ: "can_read_status",
  62. Permission.PROJECTS_READ: "can_read_status",
  63. Permission.FILAMENTS_READ: "can_read_status",
  64. Permission.INVENTORY_READ: "can_read_status",
  65. Permission.INVENTORY_VIEW_ASSIGNMENTS: "can_read_status",
  66. Permission.INVENTORY_FORECAST_READ: "can_read_status",
  67. Permission.SMART_PLUGS_READ: "can_read_status",
  68. Permission.CAMERA_VIEW: "can_read_status",
  69. Permission.MAINTENANCE_READ: "can_read_status",
  70. Permission.KPROFILES_READ: "can_read_status",
  71. Permission.NOTIFICATIONS_READ: "can_read_status",
  72. Permission.NOTIFICATION_TEMPLATES_READ: "can_read_status",
  73. Permission.EXTERNAL_LINKS_READ: "can_read_status",
  74. Permission.FIRMWARE_READ: "can_read_status",
  75. Permission.AMS_HISTORY_READ: "can_read_status",
  76. Permission.STATS_READ: "can_read_status",
  77. Permission.STATS_FILTER_BY_USER: "can_read_status",
  78. Permission.SYSTEM_READ: "can_read_status",
  79. # SETTINGS_READ stays allowed via read-status so SpoolBuddy kiosks keep
  80. # working (they need the UI-language setting via API key).
  81. Permission.SETTINGS_READ: "can_read_status",
  82. Permission.MAKERWORLD_VIEW: "can_read_status",
  83. Permission.WEBSOCKET_CONNECT: "can_read_status",
  84. # can_queue — queue write ops + reprint (which enqueues an existing archive)
  85. Permission.QUEUE_CREATE: "can_queue",
  86. Permission.QUEUE_UPDATE_OWN: "can_queue",
  87. Permission.QUEUE_UPDATE_ALL: "can_queue",
  88. Permission.QUEUE_DELETE_OWN: "can_queue",
  89. Permission.QUEUE_DELETE_ALL: "can_queue",
  90. Permission.QUEUE_REORDER: "can_queue",
  91. Permission.ARCHIVES_REPRINT_OWN: "can_queue",
  92. Permission.ARCHIVES_REPRINT_ALL: "can_queue",
  93. # can_control_printer — physical-world side effects on hardware
  94. Permission.PRINTERS_CONTROL: "can_control_printer",
  95. Permission.PRINTERS_FILES: "can_control_printer",
  96. Permission.PRINTERS_AMS_RFID: "can_control_printer",
  97. Permission.PRINTERS_CLEAR_PLATE: "can_control_printer",
  98. Permission.SMART_PLUGS_CONTROL: "can_control_printer",
  99. # can_manage_library — file-manager scope (upload/rename/delete OWN library
  100. # entries + MakerWorld import which downloads files into the library).
  101. # Bulk/ALL-ownership library ops (UPDATE_ALL / DELETE_ALL / PURGE) stay
  102. # admin-only because they cross the user boundary.
  103. Permission.LIBRARY_UPLOAD: "can_manage_library",
  104. Permission.LIBRARY_UPDATE_OWN: "can_manage_library",
  105. Permission.LIBRARY_DELETE_OWN: "can_manage_library",
  106. Permission.MAKERWORLD_IMPORT: "can_manage_library",
  107. # can_manage_inventory — inventory write scope. Covers the documented
  108. # spool/catalog/forecast write surface AND the SpoolBuddy kiosk endpoints
  109. # (NFC scan, scale reading, system command/update) which used
  110. # INVENTORY_UPDATE as a stand-in for "kiosk write" under the prior
  111. # denylist model. Read-only inventory (INVENTORY_READ etc.) stays under
  112. # can_read_status.
  113. Permission.INVENTORY_CREATE: "can_manage_inventory",
  114. Permission.INVENTORY_UPDATE: "can_manage_inventory",
  115. Permission.INVENTORY_DELETE: "can_manage_inventory",
  116. Permission.INVENTORY_FORECAST_WRITE: "can_manage_inventory",
  117. # can_access_cloud — narrow opt-in scope, gated by the router-level
  118. # ``_cloud_api_key_gate`` and additionally enforced here so the route-
  119. # level ``cloud_caller(Permission.CLOUD_AUTH)`` dep also fails closed
  120. # when the flag is off (defence-in-depth).
  121. Permission.CLOUD_AUTH: "can_access_cloud",
  122. }
  123. # Retained for documentation, drift-detection, and the prior "administrative
  124. # operations" error string. Entries here are also absent from
  125. # ``_APIKEY_SCOPE_BY_PERMISSION``, so they fail closed via the allowlist; the
  126. # denylist is a redundant explicit "these are admin" marker, not the load-
  127. # bearing security check.
  128. _APIKEY_DENIED_PERMISSIONS: frozenset[Permission] = frozenset(
  129. {
  130. # Settings administration (cred storage; rewriting these reaches SMTP/LDAP/MQTT).
  131. Permission.SETTINGS_UPDATE,
  132. Permission.SETTINGS_BACKUP,
  133. Permission.SETTINGS_RESTORE,
  134. # User / group / API-key administration.
  135. Permission.USERS_READ,
  136. Permission.USERS_CREATE,
  137. Permission.USERS_UPDATE,
  138. Permission.USERS_DELETE,
  139. Permission.GROUPS_READ,
  140. Permission.GROUPS_CREATE,
  141. Permission.GROUPS_UPDATE,
  142. Permission.GROUPS_DELETE,
  143. Permission.API_KEYS_CREATE,
  144. Permission.API_KEYS_UPDATE,
  145. Permission.API_KEYS_DELETE,
  146. Permission.API_KEYS_READ,
  147. # GitHub backup admin + firmware OTA.
  148. Permission.GITHUB_BACKUP,
  149. Permission.GITHUB_RESTORE,
  150. Permission.FIRMWARE_UPDATE,
  151. # Resource administration (printer/project/filament/maintenance/k-profile/etc CRUD).
  152. # API keys with the operational scopes can read these resources via
  153. # *_READ permissions but cannot mutate the catalog/registry itself.
  154. Permission.PRINTERS_CREATE,
  155. Permission.PRINTERS_UPDATE,
  156. Permission.PRINTERS_DELETE,
  157. Permission.ARCHIVES_CREATE,
  158. Permission.ARCHIVES_UPDATE_OWN,
  159. Permission.ARCHIVES_UPDATE_ALL,
  160. Permission.ARCHIVES_DELETE_OWN,
  161. Permission.ARCHIVES_DELETE_ALL,
  162. Permission.ARCHIVES_PURGE,
  163. Permission.LIBRARY_UPDATE_ALL,
  164. Permission.LIBRARY_DELETE_ALL,
  165. Permission.LIBRARY_PURGE,
  166. Permission.PROJECTS_CREATE,
  167. Permission.PROJECTS_UPDATE,
  168. Permission.PROJECTS_DELETE,
  169. Permission.FILAMENTS_CREATE,
  170. Permission.FILAMENTS_UPDATE,
  171. Permission.FILAMENTS_DELETE,
  172. Permission.MAINTENANCE_CREATE,
  173. Permission.MAINTENANCE_UPDATE,
  174. Permission.MAINTENANCE_DELETE,
  175. Permission.KPROFILES_CREATE,
  176. Permission.KPROFILES_UPDATE,
  177. Permission.KPROFILES_DELETE,
  178. Permission.NOTIFICATIONS_CREATE,
  179. Permission.NOTIFICATIONS_UPDATE,
  180. Permission.NOTIFICATIONS_DELETE,
  181. Permission.NOTIFICATIONS_USER_EMAIL,
  182. Permission.NOTIFICATION_TEMPLATES_UPDATE,
  183. Permission.EXTERNAL_LINKS_CREATE,
  184. Permission.EXTERNAL_LINKS_UPDATE,
  185. Permission.EXTERNAL_LINKS_DELETE,
  186. Permission.SMART_PLUGS_CREATE,
  187. Permission.SMART_PLUGS_UPDATE,
  188. Permission.SMART_PLUGS_DELETE,
  189. # Network scanning — operator only (no API-key scope for this).
  190. Permission.DISCOVERY_SCAN,
  191. }
  192. )
  193. def _resolve_apikey_scope(perm_string: str) -> str | None:
  194. """Return the scope-flag attribute name gating ``perm_string`` for API keys.
  195. None when the permission is unmapped (= admin-only / not API-key-usable).
  196. """
  197. try:
  198. perm = Permission(perm_string)
  199. except ValueError:
  200. return None
  201. return _APIKEY_SCOPE_BY_PERMISSION.get(perm)
  202. def _check_apikey_permissions(api_key: APIKey, perm_strings: list[str], *, require_any: bool = False) -> None:
  203. """Raise 403 unless ``api_key`` is allowed to use ``perm_strings``.
  204. Allowlist semantics: every requested permission MUST be present in
  205. ``_APIKEY_SCOPE_BY_PERMISSION`` AND its scope flag must be True on
  206. ``api_key``. Unmapped permissions = administrative = 403.
  207. By default ALL requested permissions must pass (mirrors
  208. ``require_permission`` / ``require_permission_if_auth_enabled``).
  209. When ``require_any=True``, only one needs to pass (mirrors
  210. ``require_any_permission_if_auth_enabled``).
  211. """
  212. if not perm_strings:
  213. # Defensive: empty perm list means the dep is auth-only, not perm-gated.
  214. # Routes never call us with [] today, but if they did, returning here
  215. # would silently allow — instead, fail closed.
  216. raise HTTPException(
  217. status_code=status.HTTP_403_FORBIDDEN,
  218. detail="API keys cannot be used for unspecified permissions",
  219. )
  220. last_failure: HTTPException | None = None
  221. for perm_str in perm_strings:
  222. scope_attr = _resolve_apikey_scope(perm_str)
  223. if scope_attr is None:
  224. failure = HTTPException(
  225. status_code=status.HTTP_403_FORBIDDEN,
  226. detail="API keys cannot be used for administrative operations",
  227. )
  228. elif not getattr(api_key, scope_attr, False):
  229. failure = HTTPException(
  230. status_code=status.HTTP_403_FORBIDDEN,
  231. detail=f"API key does not have '{scope_attr}' permission",
  232. )
  233. else:
  234. failure = None
  235. if failure is None and require_any:
  236. return # at least one passed
  237. if failure is not None and not require_any:
  238. raise failure
  239. last_failure = failure
  240. if require_any and last_failure is not None:
  241. raise last_failure
  242. def require_energy_cost_update():
  243. """Dependency for ``POST /settings/electricity-price`` (#1356).
  244. Bypasses the ``_APIKEY_DENIED_PERMISSIONS`` ``SETTINGS_UPDATE`` block for
  245. API keys that explicitly opt into ``can_update_energy_cost``. Full
  246. ``SETTINGS_UPDATE`` for API keys stays denied — this is a narrowly-scoped
  247. door for the Home Assistant dynamic-tariff use case documented in
  248. ``wiki/features/energy.md``, not a general settings-write capability.
  249. Accepts:
  250. * Auth disabled → always allowed (matches other settings routes)
  251. * JWT user with ``SETTINGS_UPDATE`` permission
  252. * API key with ``can_update_energy_cost = True``
  253. """
  254. async def permission_checker(
  255. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  256. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  257. ) -> User | None:
  258. async with async_session() as db:
  259. if not await is_auth_enabled(db):
  260. return None
  261. credentials_exception = HTTPException(
  262. status_code=status.HTTP_401_UNAUTHORIZED,
  263. detail="Could not validate credentials",
  264. headers={"WWW-Authenticate": "Bearer"},
  265. )
  266. # API key path — X-API-Key header or Bearer bb_xxx
  267. api_key_value: str | None = None
  268. if x_api_key:
  269. api_key_value = x_api_key
  270. elif credentials is not None and credentials.credentials.startswith("bb_"):
  271. api_key_value = credentials.credentials
  272. if api_key_value is not None:
  273. api_key = await _validate_api_key(db, api_key_value)
  274. if api_key is None:
  275. raise HTTPException(
  276. status_code=status.HTTP_401_UNAUTHORIZED,
  277. detail="Invalid API key",
  278. headers={"WWW-Authenticate": "Bearer"},
  279. )
  280. if not api_key.can_update_energy_cost:
  281. raise HTTPException(
  282. status_code=status.HTTP_403_FORBIDDEN,
  283. detail="API key does not have 'update_energy_cost' permission",
  284. )
  285. return None
  286. # JWT path
  287. if credentials is None:
  288. raise credentials_exception
  289. try:
  290. payload = jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM])
  291. username: str = payload.get("sub")
  292. if username is None:
  293. raise credentials_exception
  294. jti: str | None = payload.get("jti")
  295. if not jti or await is_jti_revoked(jti):
  296. raise credentials_exception
  297. iat: int | float | None = payload.get("iat")
  298. except JWTError:
  299. raise credentials_exception
  300. user = await get_user_by_username(db, username)
  301. if user is None or not user.is_active:
  302. raise credentials_exception
  303. if not _is_token_fresh(iat, user):
  304. raise credentials_exception
  305. if not user.has_all_permissions(Permission.SETTINGS_UPDATE.value):
  306. raise HTTPException(
  307. status_code=status.HTTP_403_FORBIDDEN,
  308. detail=f"Missing required permissions: {Permission.SETTINGS_UPDATE.value}",
  309. )
  310. return user
  311. return permission_checker
  312. # Password hashing
  313. # Use pbkdf2_sha256 instead of bcrypt to avoid 72-byte limit and passlib initialization issues
  314. # pbkdf2_sha256 is a secure password hashing algorithm without bcrypt's limitations
  315. pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
  316. def _get_jwt_secret() -> str:
  317. """Get the JWT secret key from environment, file, or generate a new one.
  318. Priority:
  319. 1. JWT_SECRET_KEY environment variable
  320. 2. .jwt_secret file in data directory
  321. 3. Generate new random secret and save to file
  322. Returns:
  323. The JWT secret key
  324. """
  325. # 1. Check environment variable first
  326. env_secret = os.environ.get("JWT_SECRET_KEY")
  327. if env_secret:
  328. logger.info("Using JWT secret from JWT_SECRET_KEY environment variable")
  329. return env_secret
  330. # 2. Check for secret file in data directory
  331. from backend.app.core.paths import resolve_data_dir
  332. data_dir = resolve_data_dir()
  333. secret_file = data_dir / ".jwt_secret"
  334. if secret_file.exists():
  335. try:
  336. secret = secret_file.read_text().strip()
  337. if secret and len(secret) >= 32:
  338. logger.info("Using JWT secret from %s", secret_file)
  339. return secret
  340. except OSError as e:
  341. logger.warning("Failed to read JWT secret file: %s", e)
  342. # 3. Generate new random secret
  343. new_secret = secrets.token_urlsafe(64)
  344. # Try to save it
  345. try:
  346. data_dir.mkdir(parents=True, exist_ok=True)
  347. # Note: CodeQL flags this as "clear-text storage of sensitive information" but this is
  348. # intentional and secure - JWT secrets must be readable by the app, we set 0600 permissions,
  349. # and this is standard practice for self-hosted applications (same as .env files).
  350. secret_file.write_text(new_secret) # nosec B105
  351. # Restrict permissions (owner read/write only)
  352. secret_file.chmod(0o600)
  353. logger.info("Generated new JWT secret and saved to %s", secret_file)
  354. except OSError as e:
  355. logger.warning(
  356. "Could not save JWT secret to file (%s). "
  357. "Secret will be regenerated on restart, invalidating existing tokens. "
  358. "Set JWT_SECRET_KEY environment variable for persistence.",
  359. e,
  360. )
  361. return new_secret
  362. # JWT settings
  363. SECRET_KEY = _get_jwt_secret()
  364. ALGORITHM = "HS256"
  365. ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 # 24 hours (M-2: reduced from 7 days)
  366. # HTTP Bearer token
  367. security = HTTPBearer(auto_error=False)
  368. # --- Slicer download tokens ---
  369. # Short-lived, single-use tokens for slicer protocol handlers that can't send
  370. # auth headers. Stored in AuthEphemeralToken (token_type=TokenType.SLICER_DOWNLOAD)
  371. # so they survive server restarts and work in multi-worker deployments (M-3).
  372. SLICER_TOKEN_EXPIRE_MINUTES = 5
  373. async def create_slicer_download_token(resource_type: str, resource_id: int) -> str:
  374. """Create a short-lived, single-use download token for slicer protocol handlers."""
  375. now = datetime.now(timezone.utc)
  376. expires_at = now + timedelta(minutes=SLICER_TOKEN_EXPIRE_MINUTES)
  377. token = secrets.token_urlsafe(24)
  378. resource_key = f"{resource_type}:{resource_id}"
  379. async with async_session() as db:
  380. # Prune expired tokens opportunistically
  381. await db.execute(
  382. delete(AuthEphemeralToken).where(
  383. AuthEphemeralToken.token_type == TokenType.SLICER_DOWNLOAD,
  384. AuthEphemeralToken.expires_at < now,
  385. )
  386. )
  387. db.add(
  388. AuthEphemeralToken(
  389. token=token,
  390. token_type=TokenType.SLICER_DOWNLOAD,
  391. nonce=resource_key,
  392. expires_at=expires_at,
  393. )
  394. )
  395. await db.commit()
  396. return token
  397. async def verify_slicer_download_token(token: str, resource_type: str, resource_id: int) -> bool:
  398. """Verify and atomically consume a slicer download token.
  399. Returns True only if the token is valid, unexpired, and bound to the given resource.
  400. DELETE...RETURNING ensures the token is single-use even under concurrent requests.
  401. M-NEW-1 fix: nonce (resource key) is included in the WHERE clause so the DELETE
  402. only succeeds when the token is presented to the *correct* resource endpoint.
  403. Previously the token was consumed (committed) even when stored_key != expected_key,
  404. permanently invalidating it while returning False to the caller.
  405. """
  406. expected_key = f"{resource_type}:{resource_id}"
  407. now = datetime.now(timezone.utc)
  408. async with async_session() as db:
  409. result = await db.execute(
  410. delete(AuthEphemeralToken)
  411. .where(
  412. AuthEphemeralToken.token == token,
  413. AuthEphemeralToken.token_type == TokenType.SLICER_DOWNLOAD,
  414. AuthEphemeralToken.nonce == expected_key,
  415. AuthEphemeralToken.expires_at > now,
  416. )
  417. .returning(AuthEphemeralToken.id)
  418. )
  419. if result.one_or_none() is None:
  420. return False
  421. await db.commit()
  422. return True
  423. # --- Camera stream tokens ---
  424. # Reusable tokens for camera stream/snapshot endpoints loaded via <img>/<video>
  425. # tags (these cannot send Authorization headers). Unlike slicer tokens they are
  426. # NOT single-use — streams reconnect on errors. Stored in AuthEphemeralToken
  427. # (token_type="camera_stream") for multi-worker compatibility (M-3).
  428. CAMERA_STREAM_TOKEN_EXPIRE_MINUTES = 60
  429. async def create_camera_stream_token() -> str:
  430. """Create a reusable token for camera stream/snapshot access."""
  431. now = datetime.now(timezone.utc)
  432. expires_at = now + timedelta(minutes=CAMERA_STREAM_TOKEN_EXPIRE_MINUTES)
  433. token = secrets.token_urlsafe(24)
  434. async with async_session() as db:
  435. # Prune expired tokens opportunistically
  436. await db.execute(
  437. delete(AuthEphemeralToken).where(
  438. AuthEphemeralToken.token_type == "camera_stream",
  439. AuthEphemeralToken.expires_at < now,
  440. )
  441. )
  442. db.add(
  443. AuthEphemeralToken(
  444. token=token,
  445. token_type="camera_stream",
  446. expires_at=expires_at,
  447. )
  448. )
  449. await db.commit()
  450. return token
  451. WEBSOCKET_TOKEN_EXPIRE_MINUTES = 60
  452. async def create_websocket_token(username: str | None) -> str:
  453. """Create a short-lived token for ``/api/v1/ws`` connections.
  454. Mirrors the camera-stream-token pattern: opaque random string stored
  455. in ``auth_ephemeral_tokens`` with type ``"websocket"`` so the WS
  456. endpoint can verify it *before* calling ``websocket.accept()``.
  457. Records the issuing principal in the ``username`` field — for JWT
  458. callers this is the actual username, for API-keyed callers this is
  459. the empty string (handled in the route layer; we accept None at this
  460. interface so the auth-disabled path doesn't have to fabricate one).
  461. The 60-minute expiry matches camera tokens: long enough to survive
  462. page reloads / brief disconnects, short enough that a leaked token
  463. is not a credential.
  464. """
  465. now = datetime.now(timezone.utc)
  466. expires_at = now + timedelta(minutes=WEBSOCKET_TOKEN_EXPIRE_MINUTES)
  467. token = secrets.token_urlsafe(24)
  468. async with async_session() as db:
  469. # Prune expired tokens opportunistically (same shape as camera).
  470. await db.execute(
  471. delete(AuthEphemeralToken).where(
  472. AuthEphemeralToken.token_type == "websocket",
  473. AuthEphemeralToken.expires_at < now,
  474. )
  475. )
  476. db.add(
  477. AuthEphemeralToken(
  478. token=token,
  479. token_type="websocket",
  480. username=username or "",
  481. expires_at=expires_at,
  482. )
  483. )
  484. await db.commit()
  485. return token
  486. async def verify_websocket_token(token: str) -> str | None:
  487. """Verify a WebSocket connect token.
  488. Returns the recorded ``username`` (possibly ``""`` for API-key
  489. callers, never ``None`` on success) when the token is valid, or
  490. ``None`` when it is missing / expired / unknown. The token is
  491. NOT consumed — a single page reload should not need a new round
  492. trip to mint a replacement.
  493. """
  494. now = datetime.now(timezone.utc)
  495. async with async_session() as db:
  496. result = await db.execute(
  497. select(AuthEphemeralToken).where(
  498. AuthEphemeralToken.token == token,
  499. AuthEphemeralToken.token_type == "websocket",
  500. AuthEphemeralToken.expires_at > now,
  501. )
  502. )
  503. row = result.scalar_one_or_none()
  504. if row is None:
  505. return None
  506. return row.username or ""
  507. async def verify_camera_stream_token(token: str) -> bool:
  508. """Verify a camera stream token is valid (reusable — does not consume it).
  509. Tries the ephemeral 60-minute token first (the common, browser-bound case)
  510. and falls through to long-lived tokens (#1108) for HA / kiosk integrations
  511. that paste a token once and expect it to keep working for days.
  512. """
  513. now = datetime.now(timezone.utc)
  514. async with async_session() as db:
  515. result = await db.execute(
  516. select(AuthEphemeralToken).where(
  517. AuthEphemeralToken.token == token,
  518. AuthEphemeralToken.token_type == "camera_stream",
  519. AuthEphemeralToken.expires_at > now,
  520. )
  521. )
  522. if result.scalar_one_or_none() is not None:
  523. return True
  524. # Long-lived path. Imported lazily so the auth module stays importable
  525. # at startup before the long_lived_tokens model is registered.
  526. from backend.app.services.long_lived_tokens import verify_token as verify_long_lived
  527. record = await verify_long_lived(db, token, scope="camera_stream")
  528. return record is not None
  529. def verify_password(plain_password: str, hashed_password: str) -> bool:
  530. """Verify a password against a hash.
  531. Uses pbkdf2_sha256 which handles long passwords automatically.
  532. """
  533. return pwd_context.verify(plain_password, hashed_password)
  534. def get_password_hash(password: str) -> str:
  535. """Hash a password.
  536. Uses pbkdf2_sha256 which is secure and has no password length limit.
  537. """
  538. return pwd_context.hash(password)
  539. def create_access_token(data: dict, expires_delta: timedelta | None = None) -> str:
  540. """Create a JWT access token with jti (revocation) and iat (freshness) claims."""
  541. to_encode = data.copy()
  542. now = datetime.now(timezone.utc)
  543. if expires_delta:
  544. expire = now + expires_delta
  545. else:
  546. expire = now + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  547. jti = secrets.token_hex(16)
  548. to_encode.update({"exp": expire, "jti": jti, "iat": now})
  549. encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
  550. return encoded_jwt
  551. def _is_token_fresh(iat: int | float | None, user: User) -> bool:
  552. """Return False if the token was issued before the user's last password change.
  553. Used to invalidate all sessions after a password reset/change (M-R7-B).
  554. All tokens without an iat claim are unconditionally rejected — every token
  555. issued by this server carries iat, so absence means the token is forged or
  556. from a pre-iat code path whose max TTL (24 h) has long since expired.
  557. """
  558. if iat is None:
  559. return False
  560. if not hasattr(user, "password_changed_at") or user.password_changed_at is None:
  561. return True # No password change recorded yet (I2 migration handles this)
  562. token_issued_at = datetime.fromtimestamp(iat, tz=timezone.utc)
  563. pca = user.password_changed_at
  564. if pca.tzinfo is None:
  565. pca = pca.replace(tzinfo=timezone.utc)
  566. # JWT iat is whole seconds; truncate pca so tokens issued in the same second pass.
  567. pca = pca.replace(microsecond=0)
  568. return token_issued_at >= pca
  569. async def revoke_jti(jti: str, expires_at: datetime, username: str | None = None) -> None:
  570. """Store a revoked JWT jti so it is rejected on future requests.
  571. Silently ignores duplicate inserts (e.g. double-logout with the same token).
  572. """
  573. from sqlalchemy.exc import IntegrityError
  574. async with async_session() as db:
  575. revoked = AuthEphemeralToken(
  576. token=jti,
  577. token_type="revoked_jti",
  578. username=username,
  579. expires_at=expires_at,
  580. )
  581. db.add(revoked)
  582. try:
  583. await db.commit()
  584. except IntegrityError:
  585. await db.rollback() # jti already revoked — desired state, ignore
  586. async def is_jti_revoked(jti: str) -> bool:
  587. """Return True if the given jti has been revoked."""
  588. async with async_session() as db:
  589. result = await db.execute(
  590. select(AuthEphemeralToken).where(
  591. AuthEphemeralToken.token == jti,
  592. AuthEphemeralToken.token_type == "revoked_jti",
  593. )
  594. )
  595. return result.scalar_one_or_none() is not None
  596. async def get_user_by_username(db: AsyncSession, username: str) -> User | None:
  597. """Get a user by username (case-insensitive) with groups loaded for permission checks."""
  598. result = await db.execute(
  599. select(User).where(func.lower(User.username) == func.lower(username)).options(selectinload(User.groups))
  600. )
  601. return result.scalar_one_or_none()
  602. async def get_user_by_email(db: AsyncSession, email: str) -> User | None:
  603. """Get a user by email (case-insensitive) with groups loaded for permission checks."""
  604. result = await db.execute(
  605. select(User).where(func.lower(User.email) == func.lower(email)).options(selectinload(User.groups))
  606. )
  607. return result.scalar_one_or_none()
  608. async def authenticate_user(db: AsyncSession, username: str, password: str) -> User | None:
  609. """Authenticate a user by username and password.
  610. Username lookup is case-insensitive. Password is case-sensitive.
  611. LDAP and OIDC users must authenticate via their respective providers.
  612. """
  613. user = await get_user_by_username(db, username)
  614. if not user:
  615. return None
  616. if getattr(user, "auth_source", "local") in ("ldap", "oidc"):
  617. return None # LDAP/OIDC users must authenticate via their provider
  618. if not user.password_hash or not verify_password(password, user.password_hash):
  619. return None
  620. if not user.is_active:
  621. return None
  622. return user
  623. async def authenticate_user_by_email(db: AsyncSession, email: str, password: str) -> User | None:
  624. """Authenticate a user by email and password.
  625. Email lookup is case-insensitive. Password is case-sensitive.
  626. LDAP and OIDC users must authenticate via their respective providers.
  627. """
  628. user = await get_user_by_email(db, email)
  629. if not user:
  630. return None
  631. if getattr(user, "auth_source", "local") in ("ldap", "oidc"):
  632. return None # LDAP/OIDC users must authenticate via their provider
  633. if not user.password_hash or not verify_password(password, user.password_hash):
  634. return None
  635. if not user.is_active:
  636. return None
  637. return user
  638. async def is_auth_enabled(db: AsyncSession) -> bool:
  639. """Check if authentication is enabled.
  640. Fails CLOSED on database errors. A previous version of this function
  641. caught every exception and returned False — silently treating an
  642. unavailable database as "auth is disabled" and granting unauthenticated
  643. access to every endpoint that called it (GHSA-6mf4-q26m-47pv, CVSS 9.8).
  644. An attacker could trigger that fail-open by flooding /api/v1/auth/login
  645. to exhaust the process's file-descriptor budget, then hit a protected
  646. endpoint during the window where the next DB op raised.
  647. Legitimate "auth was never configured" still returns False — the
  648. settings row is simply absent, ``scalar_one_or_none`` returns None,
  649. no exception. Any OTHER failure (connection error, fd exhaustion,
  650. schema mismatch, …) propagates so the caller can deny the request
  651. (503 / 500). Fail-closed is the only safe default for an auth probe.
  652. """
  653. result = await db.execute(select(Settings).where(Settings.key == "auth_enabled"))
  654. setting = result.scalar_one_or_none()
  655. if setting is None:
  656. return False
  657. return setting.value.lower() == "true"
  658. async def _user_from_api_key(db: AsyncSession, api_key: APIKey) -> User | None:
  659. """Resolve the owner of a validated API key, or None for legacy ownerless keys.
  660. Cloud routes (and any route that needs caller identity) read the returned
  661. User to look up per-user state like ``cloud_token``. Legacy keys created
  662. before #1182 have ``user_id IS NULL`` and stay anonymous — they keep working
  663. against non-cloud routes for backward compatibility, but cloud routes will
  664. surface a "recreate this key" error rather than 200 with empty results.
  665. """
  666. if api_key.user_id is None:
  667. return None
  668. result = await db.execute(select(User).where(User.id == api_key.user_id))
  669. user = result.scalar_one_or_none()
  670. if user is None or not user.is_active:
  671. # CASCADE on user delete should prevent a dangling user_id, but if
  672. # someone manually deactivates the owner the key shouldn't suddenly
  673. # gain an "anonymous" identity — drop the request to None so cloud
  674. # access fails closed.
  675. return None
  676. return user
  677. async def _validate_api_key(db: AsyncSession, api_key_value: str) -> APIKey | None:
  678. """Validate an API key and return the APIKey object if valid, None otherwise.
  679. L-1: Pre-filter by key_prefix (first 8 chars) before running pbkdf2 so only
  680. O(1) candidate rows are hashed instead of the full key table. The prefix is
  681. not secret (it is shown in the admin UI), so this does not reduce security.
  682. """
  683. try:
  684. # key_prefix is stored as "<first-8-chars>..." (e.g. "bb_Abc12...").
  685. # Matching on the first 8 chars of the submitted key reduces the scan to
  686. # at most one row in practice (2^40 collision space for 5 base64 chars).
  687. key_lookup = api_key_value[:8] if len(api_key_value) >= 8 else api_key_value
  688. result = await db.execute(
  689. select(APIKey).where(
  690. APIKey.enabled.is_(True),
  691. APIKey.key_prefix.like(
  692. key_lookup.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%", escape="\\"
  693. ),
  694. )
  695. )
  696. api_keys = result.scalars().all()
  697. for api_key in api_keys:
  698. if verify_password(api_key_value, api_key.key_hash):
  699. # Check expiration
  700. if api_key.expires_at:
  701. expires = api_key.expires_at
  702. if expires.tzinfo is None:
  703. expires = expires.replace(tzinfo=timezone.utc)
  704. if expires < datetime.now(timezone.utc):
  705. return None # Expired
  706. # Update last_used timestamp
  707. api_key.last_used = datetime.now(timezone.utc)
  708. await db.commit()
  709. return api_key
  710. except Exception as e: # SEC-AUTH-EXC: validation failure returns None; every caller treats None as "invalid key" → 401 (fail-closed)
  711. logger.warning("API key validation error: %s", e)
  712. return None
  713. async def get_current_user_optional(
  714. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  715. ) -> User | None:
  716. """Get the current authenticated user from JWT token, or None if not authenticated.
  717. Returns None only when NO credentials are supplied. If a token is supplied
  718. but invalid/revoked, raises 401 — a revoked token must not grant anonymous
  719. access (I6).
  720. """
  721. if credentials is None:
  722. return None
  723. _unauthorized = HTTPException(
  724. status_code=status.HTTP_401_UNAUTHORIZED,
  725. detail="Could not validate credentials",
  726. headers={"WWW-Authenticate": "Bearer"},
  727. )
  728. try:
  729. token = credentials.credentials
  730. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  731. username: str = payload.get("sub")
  732. if username is None:
  733. raise _unauthorized
  734. jti: str | None = payload.get("jti")
  735. if not jti or await is_jti_revoked(jti):
  736. raise _unauthorized # I6: revoked token → 401, not anonymous
  737. iat: int | float | None = payload.get("iat")
  738. except JWTError:
  739. raise _unauthorized
  740. async with async_session() as db:
  741. user = await get_user_by_username(db, username)
  742. if user is None or not user.is_active:
  743. raise _unauthorized
  744. if not _is_token_fresh(iat, user):
  745. raise _unauthorized
  746. return user
  747. async def get_current_user(
  748. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  749. ) -> User:
  750. """Get the current authenticated user from JWT token."""
  751. credentials_exception = HTTPException(
  752. status_code=status.HTTP_401_UNAUTHORIZED,
  753. detail="Could not validate credentials",
  754. headers={"WWW-Authenticate": "Bearer"},
  755. )
  756. if credentials is None:
  757. raise credentials_exception
  758. try:
  759. token = credentials.credentials
  760. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  761. username: str = payload.get("sub")
  762. if username is None:
  763. raise credentials_exception
  764. jti: str | None = payload.get("jti")
  765. if not jti or await is_jti_revoked(jti):
  766. raise credentials_exception
  767. iat: int | float | None = payload.get("iat")
  768. except JWTError:
  769. raise credentials_exception
  770. async with async_session() as db:
  771. user = await get_user_by_username(db, username)
  772. if user is None:
  773. raise credentials_exception
  774. if not user.is_active:
  775. raise HTTPException(
  776. status_code=status.HTTP_403_FORBIDDEN,
  777. detail="User account is disabled",
  778. )
  779. if not _is_token_fresh(iat, user):
  780. raise credentials_exception
  781. return user
  782. async def get_current_active_user(current_user: Annotated[User, Depends(get_current_user)]) -> User:
  783. """Get the current active user (alias for clarity)."""
  784. return current_user
  785. async def require_auth_if_enabled(
  786. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  787. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  788. ) -> User | None:
  789. """Require authentication if auth is enabled, otherwise return None.
  790. Accepts both JWT tokens (via Authorization: Bearer header) and API keys
  791. (via X-API-Key header or Authorization: Bearer bb_xxx). API keys return
  792. None for backward compatibility — routes that need the API-key owner (i.e.
  793. cloud routes for #1182) resolve it via their own router-level dependency
  794. that stashes ``request.state.api_key_owner``. Returning the owner here
  795. instead would silently grant API-keyed callers access to every route that
  796. fences via ``if current_user is None``, which is a wider surface than
  797. #1182 was designed to expose.
  798. """
  799. async with async_session() as db:
  800. auth_enabled = await is_auth_enabled(db)
  801. if not auth_enabled:
  802. return None
  803. # Check for API key first (X-API-Key header)
  804. if x_api_key:
  805. api_key = await _validate_api_key(db, x_api_key)
  806. if api_key:
  807. return None # API key valid, allow access
  808. # Check for Bearer token (could be JWT or API key)
  809. if credentials is not None:
  810. token = credentials.credentials
  811. # Check if it's an API key (starts with bb_)
  812. if token.startswith("bb_"):
  813. api_key = await _validate_api_key(db, token)
  814. if api_key:
  815. return None # API key valid, allow access
  816. raise HTTPException(
  817. status_code=status.HTTP_401_UNAUTHORIZED,
  818. detail="Invalid API key",
  819. headers={"WWW-Authenticate": "Bearer"},
  820. )
  821. # Otherwise treat as JWT
  822. try:
  823. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  824. username: str = payload.get("sub")
  825. if username is None:
  826. raise HTTPException(
  827. status_code=status.HTTP_401_UNAUTHORIZED,
  828. detail="Could not validate credentials",
  829. headers={"WWW-Authenticate": "Bearer"},
  830. )
  831. jti: str | None = payload.get("jti")
  832. if not jti or await is_jti_revoked(jti):
  833. raise HTTPException(
  834. status_code=status.HTTP_401_UNAUTHORIZED,
  835. detail="Could not validate credentials",
  836. headers={"WWW-Authenticate": "Bearer"},
  837. )
  838. iat: int | float | None = payload.get("iat")
  839. except JWTError:
  840. raise HTTPException(
  841. status_code=status.HTTP_401_UNAUTHORIZED,
  842. detail="Could not validate credentials",
  843. headers={"WWW-Authenticate": "Bearer"},
  844. )
  845. user = await get_user_by_username(db, username)
  846. if user is None or not user.is_active:
  847. raise HTTPException(
  848. status_code=status.HTTP_401_UNAUTHORIZED,
  849. detail="Could not validate credentials",
  850. headers={"WWW-Authenticate": "Bearer"},
  851. )
  852. if not _is_token_fresh(iat, user):
  853. raise HTTPException(
  854. status_code=status.HTTP_401_UNAUTHORIZED,
  855. detail="Could not validate credentials",
  856. headers={"WWW-Authenticate": "Bearer"},
  857. )
  858. return user
  859. # No credentials provided
  860. raise HTTPException(
  861. status_code=status.HTTP_401_UNAUTHORIZED,
  862. detail="Authentication required",
  863. headers={"WWW-Authenticate": "Bearer"},
  864. )
  865. def require_role(required_role: str):
  866. """Dependency factory for role-based access control."""
  867. async def role_checker(current_user: Annotated[User, Depends(get_current_user)]) -> User:
  868. if current_user.role != required_role:
  869. raise HTTPException(
  870. status_code=status.HTTP_403_FORBIDDEN,
  871. detail=f"Requires {required_role} role",
  872. )
  873. return current_user
  874. return role_checker
  875. def require_admin_if_auth_enabled():
  876. """Dependency factory that requires admin role if auth is enabled.
  877. GHSA-r2qv follow-up (audit pattern P3): explicitly fail-closed for API
  878. keys. The previous implementation chained on ``require_auth_if_enabled``
  879. which returns ``None`` for *both* "auth disabled" *and* "valid API
  880. key" — the inner ``admin_checker`` then treated ``None`` as auth-
  881. disabled and admitted the caller. If any route had ever adopted this
  882. dep, any API key with no scope flags set would have satisfied an
  883. admin requirement.
  884. Today no route uses this dep, but rather than leave the footgun
  885. armed, the dep is rewritten to distinguish the two cases by
  886. consulting ``is_auth_enabled`` directly and rejecting API-keyed
  887. requests with 403. "Admin" requires a user-identity role, which API
  888. keys do not carry.
  889. """
  890. async def admin_checker(
  891. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  892. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  893. ) -> User | None:
  894. async with async_session() as db:
  895. if not await is_auth_enabled(db):
  896. return None # Auth disabled — no role to check.
  897. # Reject API-keyed requests up front: admin is a user-role
  898. # concept, not a key-scope concept. The right path for
  899. # admin-equivalent API-key access is a specific Permission
  900. # (e.g. SETTINGS_UPDATE) gated by the allowlist, not the
  901. # admin role.
  902. if x_api_key or (credentials and credentials.credentials.startswith("bb_")):
  903. raise HTTPException(
  904. status_code=status.HTTP_403_FORBIDDEN,
  905. detail="Admin operations require a user role; API keys cannot be admins",
  906. )
  907. # Standard JWT path: validate and require admin role.
  908. if credentials is None:
  909. raise HTTPException(
  910. status_code=status.HTTP_401_UNAUTHORIZED,
  911. detail="Authentication required",
  912. headers={"WWW-Authenticate": "Bearer"},
  913. )
  914. try:
  915. payload = jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM])
  916. username: str = payload.get("sub")
  917. if username is None:
  918. raise HTTPException(
  919. status_code=status.HTTP_401_UNAUTHORIZED,
  920. detail="Could not validate credentials",
  921. headers={"WWW-Authenticate": "Bearer"},
  922. )
  923. jti: str | None = payload.get("jti")
  924. if not jti or await is_jti_revoked(jti):
  925. raise HTTPException(
  926. status_code=status.HTTP_401_UNAUTHORIZED,
  927. detail="Could not validate credentials",
  928. headers={"WWW-Authenticate": "Bearer"},
  929. )
  930. iat: int | float | None = payload.get("iat")
  931. except JWTError:
  932. raise HTTPException(
  933. status_code=status.HTTP_401_UNAUTHORIZED,
  934. detail="Could not validate credentials",
  935. headers={"WWW-Authenticate": "Bearer"},
  936. )
  937. user = await get_user_by_username(db, username)
  938. if user is None or not user.is_active:
  939. raise HTTPException(
  940. status_code=status.HTTP_401_UNAUTHORIZED,
  941. detail="Could not validate credentials",
  942. headers={"WWW-Authenticate": "Bearer"},
  943. )
  944. if not _is_token_fresh(iat, user):
  945. raise HTTPException(
  946. status_code=status.HTTP_401_UNAUTHORIZED,
  947. detail="Could not validate credentials",
  948. headers={"WWW-Authenticate": "Bearer"},
  949. )
  950. if user.role != "admin":
  951. raise HTTPException(
  952. status_code=status.HTTP_403_FORBIDDEN,
  953. detail="Requires admin role",
  954. )
  955. return user
  956. return admin_checker
  957. def generate_api_key() -> tuple[str, str, str]:
  958. """Generate a new API key.
  959. Returns:
  960. tuple: (full_key, key_hash, key_prefix)
  961. - full_key: The complete API key (only shown once on creation)
  962. - key_hash: Hashed version for storage and verification
  963. - key_prefix: First 8 characters for display purposes
  964. """
  965. # Generate a secure random API key (32 bytes = 64 hex characters)
  966. full_key = f"bb_{secrets.token_urlsafe(32)}"
  967. key_hash = get_password_hash(full_key)
  968. key_prefix = full_key[:8] + "..." if len(full_key) > 8 else full_key
  969. return full_key, key_hash, key_prefix
  970. async def get_api_key(
  971. authorization: Annotated[str | None, Header(alias="Authorization")] = None,
  972. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  973. db: AsyncSession = Depends(get_db),
  974. ) -> APIKey:
  975. """Get and validate API key from request headers.
  976. Checks both 'Authorization: Bearer <key>' and 'X-API-Key: <key>' headers.
  977. """
  978. api_key_value = None
  979. if x_api_key:
  980. api_key_value = x_api_key
  981. elif authorization and authorization.startswith("Bearer "):
  982. api_key_value = authorization.replace("Bearer ", "")
  983. if not api_key_value:
  984. raise HTTPException(
  985. status_code=status.HTTP_401_UNAUTHORIZED,
  986. detail="API key required. Provide 'X-API-Key' header or 'Authorization: Bearer <key>'",
  987. )
  988. # Pre-filter by key_prefix to avoid O(n) pbkdf2 hashes across all enabled keys.
  989. key_lookup = api_key_value[:8] if len(api_key_value) >= 8 else api_key_value
  990. result = await db.execute(
  991. select(APIKey).where(
  992. APIKey.enabled.is_(True),
  993. APIKey.key_prefix.like(
  994. key_lookup.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%",
  995. escape="\\",
  996. ),
  997. )
  998. )
  999. api_keys = result.scalars().all()
  1000. for api_key in api_keys:
  1001. # Check if key matches (verify against hash)
  1002. if verify_password(api_key_value, api_key.key_hash):
  1003. # Check expiration
  1004. if api_key.expires_at:
  1005. expires = api_key.expires_at
  1006. if expires.tzinfo is None:
  1007. expires = expires.replace(tzinfo=timezone.utc)
  1008. if expires < datetime.now(timezone.utc):
  1009. raise HTTPException(
  1010. status_code=status.HTTP_401_UNAUTHORIZED,
  1011. detail="API key has expired",
  1012. )
  1013. # Update last_used timestamp
  1014. api_key.last_used = datetime.now(timezone.utc)
  1015. await db.commit()
  1016. return api_key
  1017. raise HTTPException(
  1018. status_code=status.HTTP_401_UNAUTHORIZED,
  1019. detail="Invalid API key",
  1020. )
  1021. async def caller_is_api_key(
  1022. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  1023. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  1024. ) -> bool:
  1025. """Return True when the request is authenticated via API key (X-API-Key or Bearer bb_xxx)."""
  1026. if x_api_key:
  1027. return True
  1028. return credentials is not None and credentials.credentials.startswith("bb_")
  1029. def check_permission(api_key: APIKey, permission: str) -> None:
  1030. """Check if API key has the required permission.
  1031. Args:
  1032. api_key: The API key object
  1033. permission: One of 'queue', 'control_printer', 'read_status'
  1034. Raises:
  1035. HTTPException: If permission is not granted
  1036. """
  1037. permission_map = {
  1038. "queue": "can_queue",
  1039. "control_printer": "can_control_printer",
  1040. "read_status": "can_read_status",
  1041. }
  1042. if permission not in permission_map:
  1043. raise HTTPException(
  1044. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  1045. detail=f"Unknown permission: {permission}",
  1046. )
  1047. attr_name = permission_map[permission]
  1048. if not getattr(api_key, attr_name, False):
  1049. raise HTTPException(
  1050. status_code=status.HTTP_403_FORBIDDEN,
  1051. detail=f"API key does not have '{permission}' permission",
  1052. )
  1053. def check_printer_access(api_key: APIKey, printer_id: int) -> None:
  1054. """Check if API key has access to the specified printer.
  1055. Args:
  1056. api_key: The API key object
  1057. printer_id: The printer ID to check access for
  1058. Raises:
  1059. HTTPException: If access is denied
  1060. """
  1061. # None = global key, access to all printers
  1062. if api_key.printer_ids is None:
  1063. return
  1064. # Empty list or printer not in allowed list = no access
  1065. if printer_id not in api_key.printer_ids:
  1066. raise HTTPException(
  1067. status_code=status.HTTP_403_FORBIDDEN,
  1068. detail=f"API key does not have access to printer {printer_id}",
  1069. )
  1070. # Convenience dependencies - these are functions that return Depends objects
  1071. def RequireAdmin():
  1072. """Dependency that requires admin role."""
  1073. return Depends(require_role("admin"))
  1074. def RequireAdminIfAuthEnabled():
  1075. """Dependency that requires admin role if auth is enabled."""
  1076. return Depends(require_admin_if_auth_enabled())
  1077. def require_permission(*permissions: str | Permission):
  1078. """Dependency factory that requires user to have ALL specified permissions.
  1079. Accepts both JWT tokens (via Authorization: Bearer header) and API keys
  1080. (via X-API-Key header or Authorization: Bearer bb_xxx).
  1081. Args:
  1082. *permissions: Permission strings or Permission enum values to require
  1083. Returns:
  1084. A dependency function that validates permissions
  1085. """
  1086. # Convert Permission enums to strings
  1087. perm_strings = [p.value if isinstance(p, Permission) else p for p in permissions]
  1088. async def permission_checker(
  1089. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  1090. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  1091. ) -> User | None:
  1092. async with async_session() as db:
  1093. # Check for API key first (X-API-Key header)
  1094. if x_api_key:
  1095. api_key = await _validate_api_key(db, x_api_key)
  1096. if api_key:
  1097. _check_apikey_permissions(api_key, perm_strings)
  1098. return None # API key valid, allow access
  1099. credentials_exception = HTTPException(
  1100. status_code=status.HTTP_401_UNAUTHORIZED,
  1101. detail="Could not validate credentials",
  1102. headers={"WWW-Authenticate": "Bearer"},
  1103. )
  1104. if credentials is None:
  1105. raise credentials_exception
  1106. token = credentials.credentials
  1107. # Check if it's an API key (starts with bb_)
  1108. if token.startswith("bb_"):
  1109. api_key = await _validate_api_key(db, token)
  1110. if api_key:
  1111. _check_apikey_permissions(api_key, perm_strings)
  1112. return None # API key valid, allow access
  1113. raise HTTPException(
  1114. status_code=status.HTTP_401_UNAUTHORIZED,
  1115. detail="Invalid API key",
  1116. headers={"WWW-Authenticate": "Bearer"},
  1117. )
  1118. # Otherwise treat as JWT
  1119. try:
  1120. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  1121. username: str = payload.get("sub")
  1122. if username is None:
  1123. raise credentials_exception
  1124. jti: str | None = payload.get("jti")
  1125. if not jti or await is_jti_revoked(jti):
  1126. raise credentials_exception
  1127. iat: int | float | None = payload.get("iat")
  1128. except JWTError:
  1129. raise credentials_exception
  1130. user = await get_user_by_username(db, username)
  1131. if user is None or not user.is_active:
  1132. raise credentials_exception
  1133. if not _is_token_fresh(iat, user):
  1134. raise credentials_exception
  1135. if not user.has_all_permissions(*perm_strings):
  1136. raise HTTPException(
  1137. status_code=status.HTTP_403_FORBIDDEN,
  1138. detail=f"Missing required permissions: {', '.join(perm_strings)}",
  1139. )
  1140. return user
  1141. return permission_checker
  1142. def require_permission_if_auth_enabled(*permissions: str | Permission):
  1143. """Dependency factory that checks permissions only if auth is enabled.
  1144. This provides backward compatibility - when auth is disabled, all access is allowed.
  1145. Accepts both JWT tokens (via Authorization: Bearer header) and API keys
  1146. (via X-API-Key header or Authorization: Bearer bb_xxx).
  1147. Args:
  1148. *permissions: Permission strings or Permission enum values to require
  1149. Returns:
  1150. A dependency function that validates permissions if auth is enabled
  1151. """
  1152. # Convert Permission enums to strings
  1153. perm_strings = [p.value if isinstance(p, Permission) else p for p in permissions]
  1154. async def permission_checker(
  1155. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  1156. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  1157. ) -> User | None:
  1158. async with async_session() as db:
  1159. auth_enabled = await is_auth_enabled(db)
  1160. if not auth_enabled:
  1161. return None # Auth disabled, allow access
  1162. # Check for API key first (X-API-Key header). API-keyed requests
  1163. # bypass the JWT permission check entirely — their scopes live on
  1164. # the APIKey row (can_queue / can_control_printer / can_read_status
  1165. # / can_access_cloud / printer_ids), and the dep returns None so
  1166. # routes don't gain a synthetic User identity that would grant
  1167. # access to fenced surfaces like long-lived-token management.
  1168. # Cloud routes (#1182) resolve the API-key owner separately via
  1169. # their own router-level dependency; see ``cloud.py``.
  1170. if x_api_key:
  1171. api_key = await _validate_api_key(db, x_api_key)
  1172. if api_key:
  1173. _check_apikey_permissions(api_key, perm_strings)
  1174. return None # API key valid, allow access
  1175. # Check for Bearer token (could be JWT or API key)
  1176. if credentials is not None:
  1177. token = credentials.credentials
  1178. # Check if it's an API key (starts with bb_)
  1179. if token.startswith("bb_"):
  1180. api_key = await _validate_api_key(db, token)
  1181. if api_key:
  1182. _check_apikey_permissions(api_key, perm_strings)
  1183. return None # API key valid, allow access
  1184. raise HTTPException(
  1185. status_code=status.HTTP_401_UNAUTHORIZED,
  1186. detail="Invalid API key",
  1187. headers={"WWW-Authenticate": "Bearer"},
  1188. )
  1189. # Otherwise treat as JWT
  1190. try:
  1191. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  1192. username: str = payload.get("sub")
  1193. if username is None:
  1194. raise HTTPException(
  1195. status_code=status.HTTP_401_UNAUTHORIZED,
  1196. detail="Could not validate credentials",
  1197. headers={"WWW-Authenticate": "Bearer"},
  1198. )
  1199. jti: str | None = payload.get("jti")
  1200. if not jti or await is_jti_revoked(jti):
  1201. raise HTTPException(
  1202. status_code=status.HTTP_401_UNAUTHORIZED,
  1203. detail="Could not validate credentials",
  1204. headers={"WWW-Authenticate": "Bearer"},
  1205. )
  1206. iat: int | float | None = payload.get("iat")
  1207. except JWTError:
  1208. raise HTTPException(
  1209. status_code=status.HTTP_401_UNAUTHORIZED,
  1210. detail="Could not validate credentials",
  1211. headers={"WWW-Authenticate": "Bearer"},
  1212. )
  1213. user = await get_user_by_username(db, username)
  1214. if user is None or not user.is_active:
  1215. raise HTTPException(
  1216. status_code=status.HTTP_401_UNAUTHORIZED,
  1217. detail="Could not validate credentials",
  1218. headers={"WWW-Authenticate": "Bearer"},
  1219. )
  1220. if not _is_token_fresh(iat, user):
  1221. raise HTTPException(
  1222. status_code=status.HTTP_401_UNAUTHORIZED,
  1223. detail="Could not validate credentials",
  1224. headers={"WWW-Authenticate": "Bearer"},
  1225. )
  1226. if not user.has_all_permissions(*perm_strings):
  1227. raise HTTPException(
  1228. status_code=status.HTTP_403_FORBIDDEN,
  1229. detail=f"Missing required permissions: {', '.join(perm_strings)}",
  1230. )
  1231. return user
  1232. # No credentials provided
  1233. raise HTTPException(
  1234. status_code=status.HTTP_401_UNAUTHORIZED,
  1235. detail="Authentication required",
  1236. headers={"WWW-Authenticate": "Bearer"},
  1237. )
  1238. return permission_checker
  1239. def RequirePermission(*permissions: str | Permission):
  1240. """Convenience dependency that requires ALL specified permissions."""
  1241. return Depends(require_permission(*permissions))
  1242. def RequirePermissionIfAuthEnabled(*permissions: str | Permission):
  1243. """Convenience dependency that requires permissions if auth is enabled."""
  1244. return Depends(require_permission_if_auth_enabled(*permissions))
  1245. def require_any_permission_if_auth_enabled(*permissions: str | Permission):
  1246. """Dependency factory that requires AT LEAST ONE of the given permissions when auth is enabled."""
  1247. perm_strings = [p.value if isinstance(p, Permission) else p for p in permissions]
  1248. async def checker(
  1249. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  1250. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  1251. ) -> User | None:
  1252. async with async_session() as db:
  1253. auth_enabled = await is_auth_enabled(db)
  1254. if not auth_enabled:
  1255. return None
  1256. if x_api_key:
  1257. api_key = await _validate_api_key(db, x_api_key)
  1258. if api_key:
  1259. # GHSA-r2qv-8222-hqg3: previously returned None unconditionally,
  1260. # letting any valid API key satisfy admin "any-of" route
  1261. # dependencies. require_any → at-least-one must pass the scope check.
  1262. _check_apikey_permissions(api_key, perm_strings, require_any=True)
  1263. return None
  1264. if credentials is not None:
  1265. token = credentials.credentials
  1266. if token.startswith("bb_"):
  1267. api_key = await _validate_api_key(db, token)
  1268. if api_key:
  1269. _check_apikey_permissions(api_key, perm_strings, require_any=True)
  1270. return None
  1271. raise HTTPException(
  1272. status_code=status.HTTP_401_UNAUTHORIZED,
  1273. detail="Invalid API key",
  1274. headers={"WWW-Authenticate": "Bearer"},
  1275. )
  1276. try:
  1277. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  1278. username: str = payload.get("sub")
  1279. if username is None:
  1280. raise HTTPException(
  1281. status_code=status.HTTP_401_UNAUTHORIZED,
  1282. detail="Could not validate credentials",
  1283. headers={"WWW-Authenticate": "Bearer"},
  1284. )
  1285. jti: str | None = payload.get("jti")
  1286. if not jti or await is_jti_revoked(jti):
  1287. raise HTTPException(
  1288. status_code=status.HTTP_401_UNAUTHORIZED,
  1289. detail="Could not validate credentials",
  1290. headers={"WWW-Authenticate": "Bearer"},
  1291. )
  1292. iat: int | float | None = payload.get("iat")
  1293. except JWTError:
  1294. raise HTTPException(
  1295. status_code=status.HTTP_401_UNAUTHORIZED,
  1296. detail="Could not validate credentials",
  1297. headers={"WWW-Authenticate": "Bearer"},
  1298. )
  1299. user = await get_user_by_username(db, username)
  1300. if user is None or not user.is_active:
  1301. raise HTTPException(
  1302. status_code=status.HTTP_401_UNAUTHORIZED,
  1303. detail="Could not validate credentials",
  1304. headers={"WWW-Authenticate": "Bearer"},
  1305. )
  1306. if not _is_token_fresh(iat, user):
  1307. raise HTTPException(
  1308. status_code=status.HTTP_401_UNAUTHORIZED,
  1309. detail="Could not validate credentials",
  1310. headers={"WWW-Authenticate": "Bearer"},
  1311. )
  1312. if not user.has_any_permission(*perm_strings):
  1313. raise HTTPException(
  1314. status_code=status.HTTP_403_FORBIDDEN,
  1315. detail=f"Missing required permissions: {', '.join(perm_strings)}",
  1316. )
  1317. return user
  1318. raise HTTPException(
  1319. status_code=status.HTTP_401_UNAUTHORIZED,
  1320. detail="Authentication required",
  1321. headers={"WWW-Authenticate": "Bearer"},
  1322. )
  1323. return checker
  1324. def RequireAnyPermissionIfAuthEnabled(*permissions: str | Permission):
  1325. """Convenience dependency that requires AT LEAST ONE of the given permissions when auth is enabled."""
  1326. return Depends(require_any_permission_if_auth_enabled(*permissions))
  1327. def require_camera_stream_token_if_auth_enabled():
  1328. """Dependency that validates a camera stream token query param when auth is enabled.
  1329. Used for camera stream/snapshot endpoints that are loaded via <img> tags
  1330. which cannot send Authorization headers. The frontend obtains a token from
  1331. POST /printers/camera/stream-token and appends it as ?token=xxx.
  1332. """
  1333. async def checker(token: str | None = None) -> None:
  1334. async with async_session() as db:
  1335. if not await is_auth_enabled(db):
  1336. return # Auth disabled, allow access
  1337. if not token or not await verify_camera_stream_token(token):
  1338. raise HTTPException(
  1339. status_code=status.HTTP_401_UNAUTHORIZED,
  1340. detail="Valid camera stream token required. Obtain one from POST /api/v1/printers/camera/stream-token",
  1341. )
  1342. return checker
  1343. RequireCameraStreamTokenIfAuthEnabled = Depends(require_camera_stream_token_if_auth_enabled())
  1344. def require_ownership_permission(
  1345. all_permission: str | Permission,
  1346. own_permission: str | Permission,
  1347. ):
  1348. """Dependency factory for ownership-based permission checks.
  1349. - User with ``all_permission`` can modify any item
  1350. - User with ``own_permission`` can only modify items where created_by_id == user.id
  1351. - Ownerless items (created_by_id = null) require ``all_permission``
  1352. - API keys (via X-API-Key header or Bearer bb_xxx) must satisfy the
  1353. ``all_permission``'s API-key scope flag (e.g. ``can_queue`` for
  1354. ``QUEUE_UPDATE_ALL``) and then receive ``can_modify_all=True``.
  1355. OWN/ALL ownership pairs map to the same scope flag in
  1356. ``_APIKEY_SCOPE_BY_PERMISSION`` so checking ``all_permission`` is the
  1357. correct gate; API keys have no per-row ownership identity. Pre-
  1358. GHSA-r2qv-8222-hqg3 fix this returned ``(None, True)`` for any valid
  1359. key with no scope check — see ``core/auth.py`` allowlist commentary.
  1360. Returns:
  1361. A dependency function that returns (user, can_modify_all).
  1362. - can_modify_all=True: user can modify any item
  1363. - can_modify_all=False: user can only modify their own items
  1364. """
  1365. all_perm = all_permission.value if isinstance(all_permission, Permission) else all_permission
  1366. own_perm = own_permission.value if isinstance(own_permission, Permission) else own_permission
  1367. async def checker(
  1368. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  1369. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  1370. ) -> tuple[User | None, bool]:
  1371. """Returns (user, can_modify_all).
  1372. - can_modify_all=True: user can modify any item
  1373. - can_modify_all=False: user can only modify their own items
  1374. """
  1375. async with async_session() as db:
  1376. auth_enabled = await is_auth_enabled(db)
  1377. if not auth_enabled:
  1378. return None, True # Auth disabled, allow all
  1379. # GHSA-r2qv-8222-hqg3: previously API keys received (None, True)
  1380. # unconditionally on ownership-modify routes — a "queue-only" key
  1381. # could delete any user's archives, library files, queue items.
  1382. # OWN and ALL ownership perms both map to the same scope flag
  1383. # (e.g. both QUEUE_UPDATE_OWN and QUEUE_UPDATE_ALL → can_queue),
  1384. # so checking ``all_perm`` against the api_key's scope is the
  1385. # correct gate. API keys don't have per-row ownership identity, so
  1386. # on pass we keep can_modify_all=True (preserves prior intent,
  1387. # narrows access to keys with the right scope flag).
  1388. if x_api_key:
  1389. api_key = await _validate_api_key(db, x_api_key)
  1390. if api_key:
  1391. _check_apikey_permissions(api_key, [all_perm])
  1392. return None, True
  1393. # Check for Bearer token (could be JWT or API key)
  1394. if credentials is not None:
  1395. token = credentials.credentials
  1396. # Check if it's an API key (starts with bb_)
  1397. if token.startswith("bb_"):
  1398. api_key = await _validate_api_key(db, token)
  1399. if api_key:
  1400. _check_apikey_permissions(api_key, [all_perm])
  1401. return None, True
  1402. raise HTTPException(
  1403. status_code=status.HTTP_401_UNAUTHORIZED,
  1404. detail="Invalid API key",
  1405. headers={"WWW-Authenticate": "Bearer"},
  1406. )
  1407. # Otherwise treat as JWT
  1408. try:
  1409. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  1410. username: str = payload.get("sub")
  1411. if username is None:
  1412. raise HTTPException(
  1413. status_code=status.HTTP_401_UNAUTHORIZED,
  1414. detail="Could not validate credentials",
  1415. headers={"WWW-Authenticate": "Bearer"},
  1416. )
  1417. jti: str | None = payload.get("jti")
  1418. if not jti or await is_jti_revoked(jti):
  1419. raise HTTPException(
  1420. status_code=status.HTTP_401_UNAUTHORIZED,
  1421. detail="Could not validate credentials",
  1422. headers={"WWW-Authenticate": "Bearer"},
  1423. )
  1424. iat: int | float | None = payload.get("iat")
  1425. except JWTError:
  1426. raise HTTPException(
  1427. status_code=status.HTTP_401_UNAUTHORIZED,
  1428. detail="Could not validate credentials",
  1429. headers={"WWW-Authenticate": "Bearer"},
  1430. )
  1431. user = await get_user_by_username(db, username)
  1432. if user is None or not user.is_active:
  1433. raise HTTPException(
  1434. status_code=status.HTTP_401_UNAUTHORIZED,
  1435. detail="Could not validate credentials",
  1436. headers={"WWW-Authenticate": "Bearer"},
  1437. )
  1438. if not _is_token_fresh(iat, user):
  1439. raise HTTPException(
  1440. status_code=status.HTTP_401_UNAUTHORIZED,
  1441. detail="Could not validate credentials",
  1442. headers={"WWW-Authenticate": "Bearer"},
  1443. )
  1444. if user.has_permission(all_perm):
  1445. return user, True
  1446. if user.has_permission(own_perm):
  1447. return user, False
  1448. raise HTTPException(
  1449. status_code=status.HTTP_403_FORBIDDEN,
  1450. detail=f"Missing permission: {own_perm} or {all_perm}",
  1451. )
  1452. # No credentials provided
  1453. raise HTTPException(
  1454. status_code=status.HTTP_401_UNAUTHORIZED,
  1455. detail="Authentication required",
  1456. headers={"WWW-Authenticate": "Bearer"},
  1457. )
  1458. return checker