auth.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. import secrets
  5. from datetime import datetime, timedelta, timezone
  6. from pathlib import Path
  7. from typing import Annotated
  8. import jwt
  9. from fastapi import Depends, Header, HTTPException, status
  10. from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
  11. from jwt.exceptions import PyJWTError as JWTError
  12. from passlib.context import CryptContext
  13. from sqlalchemy import delete, func, select
  14. from sqlalchemy.ext.asyncio import AsyncSession
  15. from sqlalchemy.orm import selectinload
  16. from backend.app.core.database import async_session, get_db
  17. from backend.app.core.permissions import Permission
  18. from backend.app.models.api_key import APIKey
  19. from backend.app.models.auth_ephemeral import AuthEphemeralToken, TokenType
  20. from backend.app.models.settings import Settings
  21. from backend.app.models.user import User
  22. logger = logging.getLogger(__name__)
  23. # SETTINGS_READ is intentionally not denied — the SpoolBuddy kiosk reads settings
  24. # via API key (e.g. to sync the UI language).
  25. _APIKEY_DENIED_PERMISSIONS: frozenset[Permission] = frozenset(
  26. {
  27. Permission.SETTINGS_UPDATE,
  28. Permission.SETTINGS_BACKUP,
  29. Permission.SETTINGS_RESTORE,
  30. Permission.USERS_READ,
  31. Permission.USERS_CREATE,
  32. Permission.USERS_UPDATE,
  33. Permission.USERS_DELETE,
  34. Permission.GROUPS_READ,
  35. Permission.GROUPS_CREATE,
  36. Permission.GROUPS_UPDATE,
  37. Permission.GROUPS_DELETE,
  38. Permission.API_KEYS_CREATE,
  39. Permission.API_KEYS_UPDATE,
  40. Permission.API_KEYS_DELETE,
  41. Permission.API_KEYS_READ,
  42. Permission.GITHUB_BACKUP,
  43. Permission.GITHUB_RESTORE,
  44. Permission.FIRMWARE_UPDATE,
  45. }
  46. )
  47. def _check_apikey_permissions(perm_strings: list[str]) -> None:
  48. """Raise 403 if any required permission is admin-only (not accessible via API key)."""
  49. denied = _APIKEY_DENIED_PERMISSIONS.intersection(perm_strings)
  50. if denied:
  51. raise HTTPException(
  52. status_code=status.HTTP_403_FORBIDDEN,
  53. detail="API keys cannot be used for administrative operations",
  54. )
  55. # Password hashing
  56. # Use pbkdf2_sha256 instead of bcrypt to avoid 72-byte limit and passlib initialization issues
  57. # pbkdf2_sha256 is a secure password hashing algorithm without bcrypt's limitations
  58. pwd_context = CryptContext(schemes=["pbkdf2_sha256"], deprecated="auto")
  59. def _get_jwt_secret() -> str:
  60. """Get the JWT secret key from environment, file, or generate a new one.
  61. Priority:
  62. 1. JWT_SECRET_KEY environment variable
  63. 2. .jwt_secret file in data directory
  64. 3. Generate new random secret and save to file
  65. Returns:
  66. The JWT secret key
  67. """
  68. # 1. Check environment variable first
  69. env_secret = os.environ.get("JWT_SECRET_KEY")
  70. if env_secret:
  71. logger.info("Using JWT secret from JWT_SECRET_KEY environment variable")
  72. return env_secret
  73. # 2. Check for secret file in data directory
  74. # Use DATA_DIR env var (same as rest of app), fallback to data/ subdirectory
  75. data_dir_env = os.environ.get("DATA_DIR")
  76. if data_dir_env:
  77. data_dir = Path(data_dir_env)
  78. else:
  79. # Fallback to data/ subdirectory under project root (not project root itself!)
  80. data_dir = Path(__file__).parent.parent.parent.parent / "data"
  81. secret_file = data_dir / ".jwt_secret"
  82. if secret_file.exists():
  83. try:
  84. secret = secret_file.read_text().strip()
  85. if secret and len(secret) >= 32:
  86. logger.info("Using JWT secret from %s", secret_file)
  87. return secret
  88. except OSError as e:
  89. logger.warning("Failed to read JWT secret file: %s", e)
  90. # 3. Generate new random secret
  91. new_secret = secrets.token_urlsafe(64)
  92. # Try to save it
  93. try:
  94. data_dir.mkdir(parents=True, exist_ok=True)
  95. # Note: CodeQL flags this as "clear-text storage of sensitive information" but this is
  96. # intentional and secure - JWT secrets must be readable by the app, we set 0600 permissions,
  97. # and this is standard practice for self-hosted applications (same as .env files).
  98. secret_file.write_text(new_secret) # nosec B105
  99. # Restrict permissions (owner read/write only)
  100. secret_file.chmod(0o600)
  101. logger.info("Generated new JWT secret and saved to %s", secret_file)
  102. except OSError as e:
  103. logger.warning(
  104. "Could not save JWT secret to file (%s). "
  105. "Secret will be regenerated on restart, invalidating existing tokens. "
  106. "Set JWT_SECRET_KEY environment variable for persistence.",
  107. e,
  108. )
  109. return new_secret
  110. # JWT settings
  111. SECRET_KEY = _get_jwt_secret()
  112. ALGORITHM = "HS256"
  113. ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 # 24 hours (M-2: reduced from 7 days)
  114. # HTTP Bearer token
  115. security = HTTPBearer(auto_error=False)
  116. # --- Slicer download tokens ---
  117. # Short-lived, single-use tokens for slicer protocol handlers that can't send
  118. # auth headers. Stored in AuthEphemeralToken (token_type=TokenType.SLICER_DOWNLOAD)
  119. # so they survive server restarts and work in multi-worker deployments (M-3).
  120. SLICER_TOKEN_EXPIRE_MINUTES = 5
  121. async def create_slicer_download_token(resource_type: str, resource_id: int) -> str:
  122. """Create a short-lived, single-use download token for slicer protocol handlers."""
  123. now = datetime.now(timezone.utc)
  124. expires_at = now + timedelta(minutes=SLICER_TOKEN_EXPIRE_MINUTES)
  125. token = secrets.token_urlsafe(24)
  126. resource_key = f"{resource_type}:{resource_id}"
  127. async with async_session() as db:
  128. # Prune expired tokens opportunistically
  129. await db.execute(
  130. delete(AuthEphemeralToken).where(
  131. AuthEphemeralToken.token_type == TokenType.SLICER_DOWNLOAD,
  132. AuthEphemeralToken.expires_at < now,
  133. )
  134. )
  135. db.add(
  136. AuthEphemeralToken(
  137. token=token,
  138. token_type=TokenType.SLICER_DOWNLOAD,
  139. nonce=resource_key,
  140. expires_at=expires_at,
  141. )
  142. )
  143. await db.commit()
  144. return token
  145. async def verify_slicer_download_token(token: str, resource_type: str, resource_id: int) -> bool:
  146. """Verify and atomically consume a slicer download token.
  147. Returns True only if the token is valid, unexpired, and bound to the given resource.
  148. DELETE...RETURNING ensures the token is single-use even under concurrent requests.
  149. M-NEW-1 fix: nonce (resource key) is included in the WHERE clause so the DELETE
  150. only succeeds when the token is presented to the *correct* resource endpoint.
  151. Previously the token was consumed (committed) even when stored_key != expected_key,
  152. permanently invalidating it while returning False to the caller.
  153. """
  154. expected_key = f"{resource_type}:{resource_id}"
  155. now = datetime.now(timezone.utc)
  156. async with async_session() as db:
  157. result = await db.execute(
  158. delete(AuthEphemeralToken)
  159. .where(
  160. AuthEphemeralToken.token == token,
  161. AuthEphemeralToken.token_type == TokenType.SLICER_DOWNLOAD,
  162. AuthEphemeralToken.nonce == expected_key,
  163. AuthEphemeralToken.expires_at > now,
  164. )
  165. .returning(AuthEphemeralToken.id)
  166. )
  167. if result.one_or_none() is None:
  168. return False
  169. await db.commit()
  170. return True
  171. # --- Camera stream tokens ---
  172. # Reusable tokens for camera stream/snapshot endpoints loaded via <img>/<video>
  173. # tags (these cannot send Authorization headers). Unlike slicer tokens they are
  174. # NOT single-use — streams reconnect on errors. Stored in AuthEphemeralToken
  175. # (token_type="camera_stream") for multi-worker compatibility (M-3).
  176. CAMERA_STREAM_TOKEN_EXPIRE_MINUTES = 60
  177. async def create_camera_stream_token() -> str:
  178. """Create a reusable token for camera stream/snapshot access."""
  179. now = datetime.now(timezone.utc)
  180. expires_at = now + timedelta(minutes=CAMERA_STREAM_TOKEN_EXPIRE_MINUTES)
  181. token = secrets.token_urlsafe(24)
  182. async with async_session() as db:
  183. # Prune expired tokens opportunistically
  184. await db.execute(
  185. delete(AuthEphemeralToken).where(
  186. AuthEphemeralToken.token_type == "camera_stream",
  187. AuthEphemeralToken.expires_at < now,
  188. )
  189. )
  190. db.add(
  191. AuthEphemeralToken(
  192. token=token,
  193. token_type="camera_stream",
  194. expires_at=expires_at,
  195. )
  196. )
  197. await db.commit()
  198. return token
  199. async def verify_camera_stream_token(token: str) -> bool:
  200. """Verify a camera stream token is valid (reusable — does not consume it).
  201. Tries the ephemeral 60-minute token first (the common, browser-bound case)
  202. and falls through to long-lived tokens (#1108) for HA / kiosk integrations
  203. that paste a token once and expect it to keep working for days.
  204. """
  205. now = datetime.now(timezone.utc)
  206. async with async_session() as db:
  207. result = await db.execute(
  208. select(AuthEphemeralToken).where(
  209. AuthEphemeralToken.token == token,
  210. AuthEphemeralToken.token_type == "camera_stream",
  211. AuthEphemeralToken.expires_at > now,
  212. )
  213. )
  214. if result.scalar_one_or_none() is not None:
  215. return True
  216. # Long-lived path. Imported lazily so the auth module stays importable
  217. # at startup before the long_lived_tokens model is registered.
  218. from backend.app.services.long_lived_tokens import verify_token as verify_long_lived
  219. record = await verify_long_lived(db, token, scope="camera_stream")
  220. return record is not None
  221. def verify_password(plain_password: str, hashed_password: str) -> bool:
  222. """Verify a password against a hash.
  223. Uses pbkdf2_sha256 which handles long passwords automatically.
  224. """
  225. return pwd_context.verify(plain_password, hashed_password)
  226. def get_password_hash(password: str) -> str:
  227. """Hash a password.
  228. Uses pbkdf2_sha256 which is secure and has no password length limit.
  229. """
  230. return pwd_context.hash(password)
  231. def create_access_token(data: dict, expires_delta: timedelta | None = None) -> str:
  232. """Create a JWT access token with jti (revocation) and iat (freshness) claims."""
  233. to_encode = data.copy()
  234. now = datetime.now(timezone.utc)
  235. if expires_delta:
  236. expire = now + expires_delta
  237. else:
  238. expire = now + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
  239. jti = secrets.token_hex(16)
  240. to_encode.update({"exp": expire, "jti": jti, "iat": now})
  241. encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
  242. return encoded_jwt
  243. def _is_token_fresh(iat: int | float | None, user: User) -> bool:
  244. """Return False if the token was issued before the user's last password change.
  245. Used to invalidate all sessions after a password reset/change (M-R7-B).
  246. All tokens without an iat claim are unconditionally rejected — every token
  247. issued by this server carries iat, so absence means the token is forged or
  248. from a pre-iat code path whose max TTL (24 h) has long since expired.
  249. """
  250. if iat is None:
  251. return False
  252. if not hasattr(user, "password_changed_at") or user.password_changed_at is None:
  253. return True # No password change recorded yet (I2 migration handles this)
  254. token_issued_at = datetime.fromtimestamp(iat, tz=timezone.utc)
  255. pca = user.password_changed_at
  256. if pca.tzinfo is None:
  257. pca = pca.replace(tzinfo=timezone.utc)
  258. # JWT iat is whole seconds; truncate pca so tokens issued in the same second pass.
  259. pca = pca.replace(microsecond=0)
  260. return token_issued_at >= pca
  261. async def revoke_jti(jti: str, expires_at: datetime, username: str | None = None) -> None:
  262. """Store a revoked JWT jti so it is rejected on future requests.
  263. Silently ignores duplicate inserts (e.g. double-logout with the same token).
  264. """
  265. from sqlalchemy.exc import IntegrityError
  266. async with async_session() as db:
  267. revoked = AuthEphemeralToken(
  268. token=jti,
  269. token_type="revoked_jti",
  270. username=username,
  271. expires_at=expires_at,
  272. )
  273. db.add(revoked)
  274. try:
  275. await db.commit()
  276. except IntegrityError:
  277. await db.rollback() # jti already revoked — desired state, ignore
  278. async def is_jti_revoked(jti: str) -> bool:
  279. """Return True if the given jti has been revoked."""
  280. async with async_session() as db:
  281. result = await db.execute(
  282. select(AuthEphemeralToken).where(
  283. AuthEphemeralToken.token == jti,
  284. AuthEphemeralToken.token_type == "revoked_jti",
  285. )
  286. )
  287. return result.scalar_one_or_none() is not None
  288. async def get_user_by_username(db: AsyncSession, username: str) -> User | None:
  289. """Get a user by username (case-insensitive) with groups loaded for permission checks."""
  290. result = await db.execute(
  291. select(User).where(func.lower(User.username) == func.lower(username)).options(selectinload(User.groups))
  292. )
  293. return result.scalar_one_or_none()
  294. async def get_user_by_email(db: AsyncSession, email: str) -> User | None:
  295. """Get a user by email (case-insensitive) with groups loaded for permission checks."""
  296. result = await db.execute(
  297. select(User).where(func.lower(User.email) == func.lower(email)).options(selectinload(User.groups))
  298. )
  299. return result.scalar_one_or_none()
  300. async def authenticate_user(db: AsyncSession, username: str, password: str) -> User | None:
  301. """Authenticate a user by username and password.
  302. Username lookup is case-insensitive. Password is case-sensitive.
  303. LDAP and OIDC users must authenticate via their respective providers.
  304. """
  305. user = await get_user_by_username(db, username)
  306. if not user:
  307. return None
  308. if getattr(user, "auth_source", "local") in ("ldap", "oidc"):
  309. return None # LDAP/OIDC users must authenticate via their provider
  310. if not user.password_hash or not verify_password(password, user.password_hash):
  311. return None
  312. if not user.is_active:
  313. return None
  314. return user
  315. async def authenticate_user_by_email(db: AsyncSession, email: str, password: str) -> User | None:
  316. """Authenticate a user by email and password.
  317. Email lookup is case-insensitive. Password is case-sensitive.
  318. LDAP and OIDC users must authenticate via their respective providers.
  319. """
  320. user = await get_user_by_email(db, email)
  321. if not user:
  322. return None
  323. if getattr(user, "auth_source", "local") in ("ldap", "oidc"):
  324. return None # LDAP/OIDC users must authenticate via their provider
  325. if not user.password_hash or not verify_password(password, user.password_hash):
  326. return None
  327. if not user.is_active:
  328. return None
  329. return user
  330. async def is_auth_enabled(db: AsyncSession) -> bool:
  331. """Check if authentication is enabled."""
  332. try:
  333. result = await db.execute(select(Settings).where(Settings.key == "auth_enabled"))
  334. setting = result.scalar_one_or_none()
  335. if setting is None:
  336. return False
  337. return setting.value.lower() == "true"
  338. except Exception:
  339. # If settings table doesn't exist or query fails, assume auth is disabled
  340. return False
  341. async def _user_from_api_key(db: AsyncSession, api_key: APIKey) -> User | None:
  342. """Resolve the owner of a validated API key, or None for legacy ownerless keys.
  343. Cloud routes (and any route that needs caller identity) read the returned
  344. User to look up per-user state like ``cloud_token``. Legacy keys created
  345. before #1182 have ``user_id IS NULL`` and stay anonymous — they keep working
  346. against non-cloud routes for backward compatibility, but cloud routes will
  347. surface a "recreate this key" error rather than 200 with empty results.
  348. """
  349. if api_key.user_id is None:
  350. return None
  351. result = await db.execute(select(User).where(User.id == api_key.user_id))
  352. user = result.scalar_one_or_none()
  353. if user is None or not user.is_active:
  354. # CASCADE on user delete should prevent a dangling user_id, but if
  355. # someone manually deactivates the owner the key shouldn't suddenly
  356. # gain an "anonymous" identity — drop the request to None so cloud
  357. # access fails closed.
  358. return None
  359. return user
  360. async def _validate_api_key(db: AsyncSession, api_key_value: str) -> APIKey | None:
  361. """Validate an API key and return the APIKey object if valid, None otherwise.
  362. L-1: Pre-filter by key_prefix (first 8 chars) before running pbkdf2 so only
  363. O(1) candidate rows are hashed instead of the full key table. The prefix is
  364. not secret (it is shown in the admin UI), so this does not reduce security.
  365. """
  366. try:
  367. # key_prefix is stored as "<first-8-chars>..." (e.g. "bb_Abc12...").
  368. # Matching on the first 8 chars of the submitted key reduces the scan to
  369. # at most one row in practice (2^40 collision space for 5 base64 chars).
  370. key_lookup = api_key_value[:8] if len(api_key_value) >= 8 else api_key_value
  371. result = await db.execute(
  372. select(APIKey).where(
  373. APIKey.enabled.is_(True),
  374. APIKey.key_prefix.like(
  375. key_lookup.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%", escape="\\"
  376. ),
  377. )
  378. )
  379. api_keys = result.scalars().all()
  380. for api_key in api_keys:
  381. if verify_password(api_key_value, api_key.key_hash):
  382. # Check expiration
  383. if api_key.expires_at:
  384. expires = api_key.expires_at
  385. if expires.tzinfo is None:
  386. expires = expires.replace(tzinfo=timezone.utc)
  387. if expires < datetime.now(timezone.utc):
  388. return None # Expired
  389. # Update last_used timestamp
  390. api_key.last_used = datetime.now(timezone.utc)
  391. await db.commit()
  392. return api_key
  393. except Exception as e:
  394. logger.warning("API key validation error: %s", e)
  395. return None
  396. async def get_current_user_optional(
  397. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  398. ) -> User | None:
  399. """Get the current authenticated user from JWT token, or None if not authenticated.
  400. Returns None only when NO credentials are supplied. If a token is supplied
  401. but invalid/revoked, raises 401 — a revoked token must not grant anonymous
  402. access (I6).
  403. """
  404. if credentials is None:
  405. return None
  406. _unauthorized = HTTPException(
  407. status_code=status.HTTP_401_UNAUTHORIZED,
  408. detail="Could not validate credentials",
  409. headers={"WWW-Authenticate": "Bearer"},
  410. )
  411. try:
  412. token = credentials.credentials
  413. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  414. username: str = payload.get("sub")
  415. if username is None:
  416. raise _unauthorized
  417. jti: str | None = payload.get("jti")
  418. if not jti or await is_jti_revoked(jti):
  419. raise _unauthorized # I6: revoked token → 401, not anonymous
  420. iat: int | float | None = payload.get("iat")
  421. except JWTError:
  422. raise _unauthorized
  423. async with async_session() as db:
  424. user = await get_user_by_username(db, username)
  425. if user is None or not user.is_active:
  426. raise _unauthorized
  427. if not _is_token_fresh(iat, user):
  428. raise _unauthorized
  429. return user
  430. async def get_current_user(
  431. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  432. ) -> User:
  433. """Get the current authenticated user from JWT token."""
  434. credentials_exception = HTTPException(
  435. status_code=status.HTTP_401_UNAUTHORIZED,
  436. detail="Could not validate credentials",
  437. headers={"WWW-Authenticate": "Bearer"},
  438. )
  439. if credentials is None:
  440. raise credentials_exception
  441. try:
  442. token = credentials.credentials
  443. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  444. username: str = payload.get("sub")
  445. if username is None:
  446. raise credentials_exception
  447. jti: str | None = payload.get("jti")
  448. if not jti or await is_jti_revoked(jti):
  449. raise credentials_exception
  450. iat: int | float | None = payload.get("iat")
  451. except JWTError:
  452. raise credentials_exception
  453. async with async_session() as db:
  454. user = await get_user_by_username(db, username)
  455. if user is None:
  456. raise credentials_exception
  457. if not user.is_active:
  458. raise HTTPException(
  459. status_code=status.HTTP_403_FORBIDDEN,
  460. detail="User account is disabled",
  461. )
  462. if not _is_token_fresh(iat, user):
  463. raise credentials_exception
  464. return user
  465. async def get_current_active_user(current_user: Annotated[User, Depends(get_current_user)]) -> User:
  466. """Get the current active user (alias for clarity)."""
  467. return current_user
  468. async def require_auth_if_enabled(
  469. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  470. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  471. ) -> User | None:
  472. """Require authentication if auth is enabled, otherwise return None.
  473. Accepts both JWT tokens (via Authorization: Bearer header) and API keys
  474. (via X-API-Key header or Authorization: Bearer bb_xxx). API keys return
  475. None for backward compatibility — routes that need the API-key owner (i.e.
  476. cloud routes for #1182) resolve it via their own router-level dependency
  477. that stashes ``request.state.api_key_owner``. Returning the owner here
  478. instead would silently grant API-keyed callers access to every route that
  479. fences via ``if current_user is None``, which is a wider surface than
  480. #1182 was designed to expose.
  481. """
  482. async with async_session() as db:
  483. auth_enabled = await is_auth_enabled(db)
  484. if not auth_enabled:
  485. return None
  486. # Check for API key first (X-API-Key header)
  487. if x_api_key:
  488. api_key = await _validate_api_key(db, x_api_key)
  489. if api_key:
  490. return None # API key valid, allow access
  491. # Check for Bearer token (could be JWT or API key)
  492. if credentials is not None:
  493. token = credentials.credentials
  494. # Check if it's an API key (starts with bb_)
  495. if token.startswith("bb_"):
  496. api_key = await _validate_api_key(db, token)
  497. if api_key:
  498. return None # API key valid, allow access
  499. raise HTTPException(
  500. status_code=status.HTTP_401_UNAUTHORIZED,
  501. detail="Invalid API key",
  502. headers={"WWW-Authenticate": "Bearer"},
  503. )
  504. # Otherwise treat as JWT
  505. try:
  506. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  507. username: str = payload.get("sub")
  508. if username is None:
  509. raise HTTPException(
  510. status_code=status.HTTP_401_UNAUTHORIZED,
  511. detail="Could not validate credentials",
  512. headers={"WWW-Authenticate": "Bearer"},
  513. )
  514. jti: str | None = payload.get("jti")
  515. if not jti or await is_jti_revoked(jti):
  516. raise HTTPException(
  517. status_code=status.HTTP_401_UNAUTHORIZED,
  518. detail="Could not validate credentials",
  519. headers={"WWW-Authenticate": "Bearer"},
  520. )
  521. iat: int | float | None = payload.get("iat")
  522. except JWTError:
  523. raise HTTPException(
  524. status_code=status.HTTP_401_UNAUTHORIZED,
  525. detail="Could not validate credentials",
  526. headers={"WWW-Authenticate": "Bearer"},
  527. )
  528. user = await get_user_by_username(db, username)
  529. if user is None or not user.is_active:
  530. raise HTTPException(
  531. status_code=status.HTTP_401_UNAUTHORIZED,
  532. detail="Could not validate credentials",
  533. headers={"WWW-Authenticate": "Bearer"},
  534. )
  535. if not _is_token_fresh(iat, user):
  536. raise HTTPException(
  537. status_code=status.HTTP_401_UNAUTHORIZED,
  538. detail="Could not validate credentials",
  539. headers={"WWW-Authenticate": "Bearer"},
  540. )
  541. return user
  542. # No credentials provided
  543. raise HTTPException(
  544. status_code=status.HTTP_401_UNAUTHORIZED,
  545. detail="Authentication required",
  546. headers={"WWW-Authenticate": "Bearer"},
  547. )
  548. def require_role(required_role: str):
  549. """Dependency factory for role-based access control."""
  550. async def role_checker(current_user: Annotated[User, Depends(get_current_user)]) -> User:
  551. if current_user.role != required_role:
  552. raise HTTPException(
  553. status_code=status.HTTP_403_FORBIDDEN,
  554. detail=f"Requires {required_role} role",
  555. )
  556. return current_user
  557. return role_checker
  558. def require_admin_if_auth_enabled():
  559. """Dependency factory that requires admin role if auth is enabled."""
  560. async def admin_checker(
  561. current_user: Annotated[User | None, Depends(require_auth_if_enabled)] = None,
  562. ) -> User | None:
  563. if current_user is None:
  564. return None # Auth not enabled, allow access
  565. if current_user.role != "admin":
  566. raise HTTPException(
  567. status_code=status.HTTP_403_FORBIDDEN,
  568. detail="Requires admin role",
  569. )
  570. return current_user
  571. return admin_checker
  572. def generate_api_key() -> tuple[str, str, str]:
  573. """Generate a new API key.
  574. Returns:
  575. tuple: (full_key, key_hash, key_prefix)
  576. - full_key: The complete API key (only shown once on creation)
  577. - key_hash: Hashed version for storage and verification
  578. - key_prefix: First 8 characters for display purposes
  579. """
  580. # Generate a secure random API key (32 bytes = 64 hex characters)
  581. full_key = f"bb_{secrets.token_urlsafe(32)}"
  582. key_hash = get_password_hash(full_key)
  583. key_prefix = full_key[:8] + "..." if len(full_key) > 8 else full_key
  584. return full_key, key_hash, key_prefix
  585. async def get_api_key(
  586. authorization: Annotated[str | None, Header(alias="Authorization")] = None,
  587. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  588. db: AsyncSession = Depends(get_db),
  589. ) -> APIKey:
  590. """Get and validate API key from request headers.
  591. Checks both 'Authorization: Bearer <key>' and 'X-API-Key: <key>' headers.
  592. """
  593. api_key_value = None
  594. if x_api_key:
  595. api_key_value = x_api_key
  596. elif authorization and authorization.startswith("Bearer "):
  597. api_key_value = authorization.replace("Bearer ", "")
  598. if not api_key_value:
  599. raise HTTPException(
  600. status_code=status.HTTP_401_UNAUTHORIZED,
  601. detail="API key required. Provide 'X-API-Key' header or 'Authorization: Bearer <key>'",
  602. )
  603. # Pre-filter by key_prefix to avoid O(n) pbkdf2 hashes across all enabled keys.
  604. key_lookup = api_key_value[:8] if len(api_key_value) >= 8 else api_key_value
  605. result = await db.execute(
  606. select(APIKey).where(
  607. APIKey.enabled.is_(True),
  608. APIKey.key_prefix.like(
  609. key_lookup.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%",
  610. escape="\\",
  611. ),
  612. )
  613. )
  614. api_keys = result.scalars().all()
  615. for api_key in api_keys:
  616. # Check if key matches (verify against hash)
  617. if verify_password(api_key_value, api_key.key_hash):
  618. # Check expiration
  619. if api_key.expires_at:
  620. expires = api_key.expires_at
  621. if expires.tzinfo is None:
  622. expires = expires.replace(tzinfo=timezone.utc)
  623. if expires < datetime.now(timezone.utc):
  624. raise HTTPException(
  625. status_code=status.HTTP_401_UNAUTHORIZED,
  626. detail="API key has expired",
  627. )
  628. # Update last_used timestamp
  629. api_key.last_used = datetime.now(timezone.utc)
  630. await db.commit()
  631. return api_key
  632. raise HTTPException(
  633. status_code=status.HTTP_401_UNAUTHORIZED,
  634. detail="Invalid API key",
  635. )
  636. async def caller_is_api_key(
  637. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  638. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  639. ) -> bool:
  640. """Return True when the request is authenticated via API key (X-API-Key or Bearer bb_xxx)."""
  641. if x_api_key:
  642. return True
  643. return credentials is not None and credentials.credentials.startswith("bb_")
  644. def check_permission(api_key: APIKey, permission: str) -> None:
  645. """Check if API key has the required permission.
  646. Args:
  647. api_key: The API key object
  648. permission: One of 'queue', 'control_printer', 'read_status'
  649. Raises:
  650. HTTPException: If permission is not granted
  651. """
  652. permission_map = {
  653. "queue": "can_queue",
  654. "control_printer": "can_control_printer",
  655. "read_status": "can_read_status",
  656. }
  657. if permission not in permission_map:
  658. raise HTTPException(
  659. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  660. detail=f"Unknown permission: {permission}",
  661. )
  662. attr_name = permission_map[permission]
  663. if not getattr(api_key, attr_name, False):
  664. raise HTTPException(
  665. status_code=status.HTTP_403_FORBIDDEN,
  666. detail=f"API key does not have '{permission}' permission",
  667. )
  668. def check_printer_access(api_key: APIKey, printer_id: int) -> None:
  669. """Check if API key has access to the specified printer.
  670. Args:
  671. api_key: The API key object
  672. printer_id: The printer ID to check access for
  673. Raises:
  674. HTTPException: If access is denied
  675. """
  676. # None = global key, access to all printers
  677. if api_key.printer_ids is None:
  678. return
  679. # Empty list or printer not in allowed list = no access
  680. if printer_id not in api_key.printer_ids:
  681. raise HTTPException(
  682. status_code=status.HTTP_403_FORBIDDEN,
  683. detail=f"API key does not have access to printer {printer_id}",
  684. )
  685. # Convenience dependencies - these are functions that return Depends objects
  686. def RequireAdmin():
  687. """Dependency that requires admin role."""
  688. return Depends(require_role("admin"))
  689. def RequireAdminIfAuthEnabled():
  690. """Dependency that requires admin role if auth is enabled."""
  691. return Depends(require_admin_if_auth_enabled())
  692. def require_permission(*permissions: str | Permission):
  693. """Dependency factory that requires user to have ALL specified permissions.
  694. Accepts both JWT tokens (via Authorization: Bearer header) and API keys
  695. (via X-API-Key header or Authorization: Bearer bb_xxx).
  696. Args:
  697. *permissions: Permission strings or Permission enum values to require
  698. Returns:
  699. A dependency function that validates permissions
  700. """
  701. # Convert Permission enums to strings
  702. perm_strings = [p.value if isinstance(p, Permission) else p for p in permissions]
  703. async def permission_checker(
  704. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  705. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  706. ) -> User | None:
  707. async with async_session() as db:
  708. # Check for API key first (X-API-Key header)
  709. if x_api_key:
  710. api_key = await _validate_api_key(db, x_api_key)
  711. if api_key:
  712. _check_apikey_permissions(perm_strings)
  713. return None # API key valid, allow access
  714. credentials_exception = HTTPException(
  715. status_code=status.HTTP_401_UNAUTHORIZED,
  716. detail="Could not validate credentials",
  717. headers={"WWW-Authenticate": "Bearer"},
  718. )
  719. if credentials is None:
  720. raise credentials_exception
  721. token = credentials.credentials
  722. # Check if it's an API key (starts with bb_)
  723. if token.startswith("bb_"):
  724. api_key = await _validate_api_key(db, token)
  725. if api_key:
  726. _check_apikey_permissions(perm_strings)
  727. return None # API key valid, allow access
  728. raise HTTPException(
  729. status_code=status.HTTP_401_UNAUTHORIZED,
  730. detail="Invalid API key",
  731. headers={"WWW-Authenticate": "Bearer"},
  732. )
  733. # Otherwise treat as JWT
  734. try:
  735. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  736. username: str = payload.get("sub")
  737. if username is None:
  738. raise credentials_exception
  739. jti: str | None = payload.get("jti")
  740. if not jti or await is_jti_revoked(jti):
  741. raise credentials_exception
  742. iat: int | float | None = payload.get("iat")
  743. except JWTError:
  744. raise credentials_exception
  745. user = await get_user_by_username(db, username)
  746. if user is None or not user.is_active:
  747. raise credentials_exception
  748. if not _is_token_fresh(iat, user):
  749. raise credentials_exception
  750. if not user.has_all_permissions(*perm_strings):
  751. raise HTTPException(
  752. status_code=status.HTTP_403_FORBIDDEN,
  753. detail=f"Missing required permissions: {', '.join(perm_strings)}",
  754. )
  755. return user
  756. return permission_checker
  757. def require_permission_if_auth_enabled(*permissions: str | Permission):
  758. """Dependency factory that checks permissions only if auth is enabled.
  759. This provides backward compatibility - when auth is disabled, all access is allowed.
  760. Accepts both JWT tokens (via Authorization: Bearer header) and API keys
  761. (via X-API-Key header or Authorization: Bearer bb_xxx).
  762. Args:
  763. *permissions: Permission strings or Permission enum values to require
  764. Returns:
  765. A dependency function that validates permissions if auth is enabled
  766. """
  767. # Convert Permission enums to strings
  768. perm_strings = [p.value if isinstance(p, Permission) else p for p in permissions]
  769. async def permission_checker(
  770. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  771. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  772. ) -> User | None:
  773. async with async_session() as db:
  774. auth_enabled = await is_auth_enabled(db)
  775. if not auth_enabled:
  776. return None # Auth disabled, allow access
  777. # Check for API key first (X-API-Key header). API-keyed requests
  778. # bypass the JWT permission check entirely — their scopes live on
  779. # the APIKey row (can_queue / can_control_printer / can_read_status
  780. # / can_access_cloud / printer_ids), and the dep returns None so
  781. # routes don't gain a synthetic User identity that would grant
  782. # access to fenced surfaces like long-lived-token management.
  783. # Cloud routes (#1182) resolve the API-key owner separately via
  784. # their own router-level dependency; see ``cloud.py``.
  785. if x_api_key:
  786. api_key = await _validate_api_key(db, x_api_key)
  787. if api_key:
  788. _check_apikey_permissions(perm_strings)
  789. return None # API key valid, allow access
  790. # Check for Bearer token (could be JWT or API key)
  791. if credentials is not None:
  792. token = credentials.credentials
  793. # Check if it's an API key (starts with bb_)
  794. if token.startswith("bb_"):
  795. api_key = await _validate_api_key(db, token)
  796. if api_key:
  797. _check_apikey_permissions(perm_strings)
  798. return None # API key valid, allow access
  799. raise HTTPException(
  800. status_code=status.HTTP_401_UNAUTHORIZED,
  801. detail="Invalid API key",
  802. headers={"WWW-Authenticate": "Bearer"},
  803. )
  804. # Otherwise treat as JWT
  805. try:
  806. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  807. username: str = payload.get("sub")
  808. if username is None:
  809. raise HTTPException(
  810. status_code=status.HTTP_401_UNAUTHORIZED,
  811. detail="Could not validate credentials",
  812. headers={"WWW-Authenticate": "Bearer"},
  813. )
  814. jti: str | None = payload.get("jti")
  815. if not jti or await is_jti_revoked(jti):
  816. raise HTTPException(
  817. status_code=status.HTTP_401_UNAUTHORIZED,
  818. detail="Could not validate credentials",
  819. headers={"WWW-Authenticate": "Bearer"},
  820. )
  821. iat: int | float | None = payload.get("iat")
  822. except JWTError:
  823. raise HTTPException(
  824. status_code=status.HTTP_401_UNAUTHORIZED,
  825. detail="Could not validate credentials",
  826. headers={"WWW-Authenticate": "Bearer"},
  827. )
  828. user = await get_user_by_username(db, username)
  829. if user is None or not user.is_active:
  830. raise HTTPException(
  831. status_code=status.HTTP_401_UNAUTHORIZED,
  832. detail="Could not validate credentials",
  833. headers={"WWW-Authenticate": "Bearer"},
  834. )
  835. if not _is_token_fresh(iat, user):
  836. raise HTTPException(
  837. status_code=status.HTTP_401_UNAUTHORIZED,
  838. detail="Could not validate credentials",
  839. headers={"WWW-Authenticate": "Bearer"},
  840. )
  841. if not user.has_all_permissions(*perm_strings):
  842. raise HTTPException(
  843. status_code=status.HTTP_403_FORBIDDEN,
  844. detail=f"Missing required permissions: {', '.join(perm_strings)}",
  845. )
  846. return user
  847. # No credentials provided
  848. raise HTTPException(
  849. status_code=status.HTTP_401_UNAUTHORIZED,
  850. detail="Authentication required",
  851. headers={"WWW-Authenticate": "Bearer"},
  852. )
  853. return permission_checker
  854. def RequirePermission(*permissions: str | Permission):
  855. """Convenience dependency that requires ALL specified permissions."""
  856. return Depends(require_permission(*permissions))
  857. def RequirePermissionIfAuthEnabled(*permissions: str | Permission):
  858. """Convenience dependency that requires permissions if auth is enabled."""
  859. return Depends(require_permission_if_auth_enabled(*permissions))
  860. def require_any_permission_if_auth_enabled(*permissions: str | Permission):
  861. """Dependency factory that requires AT LEAST ONE of the given permissions when auth is enabled."""
  862. perm_strings = [p.value if isinstance(p, Permission) else p for p in permissions]
  863. async def checker(
  864. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  865. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  866. ) -> User | None:
  867. async with async_session() as db:
  868. auth_enabled = await is_auth_enabled(db)
  869. if not auth_enabled:
  870. return None
  871. if x_api_key:
  872. api_key = await _validate_api_key(db, x_api_key)
  873. if api_key:
  874. return None
  875. if credentials is not None:
  876. token = credentials.credentials
  877. if token.startswith("bb_"):
  878. api_key = await _validate_api_key(db, token)
  879. if api_key:
  880. return None
  881. raise HTTPException(
  882. status_code=status.HTTP_401_UNAUTHORIZED,
  883. detail="Invalid API key",
  884. headers={"WWW-Authenticate": "Bearer"},
  885. )
  886. try:
  887. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  888. username: str = payload.get("sub")
  889. if username is None:
  890. raise HTTPException(
  891. status_code=status.HTTP_401_UNAUTHORIZED,
  892. detail="Could not validate credentials",
  893. headers={"WWW-Authenticate": "Bearer"},
  894. )
  895. jti: str | None = payload.get("jti")
  896. if not jti or await is_jti_revoked(jti):
  897. raise HTTPException(
  898. status_code=status.HTTP_401_UNAUTHORIZED,
  899. detail="Could not validate credentials",
  900. headers={"WWW-Authenticate": "Bearer"},
  901. )
  902. iat: int | float | None = payload.get("iat")
  903. except JWTError:
  904. raise HTTPException(
  905. status_code=status.HTTP_401_UNAUTHORIZED,
  906. detail="Could not validate credentials",
  907. headers={"WWW-Authenticate": "Bearer"},
  908. )
  909. user = await get_user_by_username(db, username)
  910. if user is None or not user.is_active:
  911. raise HTTPException(
  912. status_code=status.HTTP_401_UNAUTHORIZED,
  913. detail="Could not validate credentials",
  914. headers={"WWW-Authenticate": "Bearer"},
  915. )
  916. if not _is_token_fresh(iat, user):
  917. raise HTTPException(
  918. status_code=status.HTTP_401_UNAUTHORIZED,
  919. detail="Could not validate credentials",
  920. headers={"WWW-Authenticate": "Bearer"},
  921. )
  922. if not user.has_any_permission(*perm_strings):
  923. raise HTTPException(
  924. status_code=status.HTTP_403_FORBIDDEN,
  925. detail=f"Missing required permissions: {', '.join(perm_strings)}",
  926. )
  927. return user
  928. raise HTTPException(
  929. status_code=status.HTTP_401_UNAUTHORIZED,
  930. detail="Authentication required",
  931. headers={"WWW-Authenticate": "Bearer"},
  932. )
  933. return checker
  934. def RequireAnyPermissionIfAuthEnabled(*permissions: str | Permission):
  935. """Convenience dependency that requires AT LEAST ONE of the given permissions when auth is enabled."""
  936. return Depends(require_any_permission_if_auth_enabled(*permissions))
  937. def require_camera_stream_token_if_auth_enabled():
  938. """Dependency that validates a camera stream token query param when auth is enabled.
  939. Used for camera stream/snapshot endpoints that are loaded via <img> tags
  940. which cannot send Authorization headers. The frontend obtains a token from
  941. POST /printers/camera/stream-token and appends it as ?token=xxx.
  942. """
  943. async def checker(token: str | None = None) -> None:
  944. async with async_session() as db:
  945. if not await is_auth_enabled(db):
  946. return # Auth disabled, allow access
  947. if not token or not await verify_camera_stream_token(token):
  948. raise HTTPException(
  949. status_code=status.HTTP_401_UNAUTHORIZED,
  950. detail="Valid camera stream token required. Obtain one from POST /api/v1/printers/camera/stream-token",
  951. )
  952. return checker
  953. RequireCameraStreamTokenIfAuthEnabled = Depends(require_camera_stream_token_if_auth_enabled())
  954. def require_ownership_permission(
  955. all_permission: str | Permission,
  956. own_permission: str | Permission,
  957. ):
  958. """Dependency factory for ownership-based permission checks.
  959. - User with `all_permission` can modify any item
  960. - User with `own_permission` can only modify items where created_by_id == user.id
  961. - Ownerless items (created_by_id = null) require `all_permission`
  962. - API keys (via X-API-Key header or Bearer bb_xxx) get full access (can_modify_all=True)
  963. Returns:
  964. A dependency function that returns (user, can_modify_all).
  965. - can_modify_all=True: user can modify any item
  966. - can_modify_all=False: user can only modify their own items
  967. """
  968. all_perm = all_permission.value if isinstance(all_permission, Permission) else all_permission
  969. own_perm = own_permission.value if isinstance(own_permission, Permission) else own_permission
  970. async def checker(
  971. credentials: Annotated[HTTPAuthorizationCredentials | None, Depends(security)] = None,
  972. x_api_key: Annotated[str | None, Header(alias="X-API-Key")] = None,
  973. ) -> tuple[User | None, bool]:
  974. """Returns (user, can_modify_all).
  975. - can_modify_all=True: user can modify any item
  976. - can_modify_all=False: user can only modify their own items
  977. """
  978. async with async_session() as db:
  979. auth_enabled = await is_auth_enabled(db)
  980. if not auth_enabled:
  981. return None, True # Auth disabled, allow all
  982. # Check for API key first (X-API-Key header)
  983. if x_api_key:
  984. api_key = await _validate_api_key(db, x_api_key)
  985. if api_key:
  986. return None, True # API key valid, allow all
  987. # Check for Bearer token (could be JWT or API key)
  988. if credentials is not None:
  989. token = credentials.credentials
  990. # Check if it's an API key (starts with bb_)
  991. if token.startswith("bb_"):
  992. api_key = await _validate_api_key(db, token)
  993. if api_key:
  994. return None, True # API key valid, allow all
  995. raise HTTPException(
  996. status_code=status.HTTP_401_UNAUTHORIZED,
  997. detail="Invalid API key",
  998. headers={"WWW-Authenticate": "Bearer"},
  999. )
  1000. # Otherwise treat as JWT
  1001. try:
  1002. payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
  1003. username: str = payload.get("sub")
  1004. if username is None:
  1005. raise HTTPException(
  1006. status_code=status.HTTP_401_UNAUTHORIZED,
  1007. detail="Could not validate credentials",
  1008. headers={"WWW-Authenticate": "Bearer"},
  1009. )
  1010. jti: str | None = payload.get("jti")
  1011. if not jti or await is_jti_revoked(jti):
  1012. raise HTTPException(
  1013. status_code=status.HTTP_401_UNAUTHORIZED,
  1014. detail="Could not validate credentials",
  1015. headers={"WWW-Authenticate": "Bearer"},
  1016. )
  1017. iat: int | float | None = payload.get("iat")
  1018. except JWTError:
  1019. raise HTTPException(
  1020. status_code=status.HTTP_401_UNAUTHORIZED,
  1021. detail="Could not validate credentials",
  1022. headers={"WWW-Authenticate": "Bearer"},
  1023. )
  1024. user = await get_user_by_username(db, username)
  1025. if user is None or not user.is_active:
  1026. raise HTTPException(
  1027. status_code=status.HTTP_401_UNAUTHORIZED,
  1028. detail="Could not validate credentials",
  1029. headers={"WWW-Authenticate": "Bearer"},
  1030. )
  1031. if not _is_token_fresh(iat, user):
  1032. raise HTTPException(
  1033. status_code=status.HTTP_401_UNAUTHORIZED,
  1034. detail="Could not validate credentials",
  1035. headers={"WWW-Authenticate": "Bearer"},
  1036. )
  1037. if user.has_permission(all_perm):
  1038. return user, True
  1039. if user.has_permission(own_perm):
  1040. return user, False
  1041. raise HTTPException(
  1042. status_code=status.HTTP_403_FORBIDDEN,
  1043. detail=f"Missing permission: {own_perm} or {all_perm}",
  1044. )
  1045. # No credentials provided
  1046. raise HTTPException(
  1047. status_code=status.HTTP_401_UNAUTHORIZED,
  1048. detail="Authentication required",
  1049. headers={"WWW-Authenticate": "Bearer"},
  1050. )
  1051. return checker