auth.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. import re
  2. from typing import Literal
  3. from pydantic import BaseModel, Field, field_validator, model_validator
  4. def _validate_password_complexity(v: str) -> str:
  5. """Enforce minimum password complexity (M-C).
  6. Requires at least one uppercase letter, one lowercase letter, one digit,
  7. and one special character in addition to the min_length=8 Field constraint.
  8. """
  9. if not re.search(r"[A-Z]", v):
  10. raise ValueError("Password must contain at least one uppercase letter")
  11. if not re.search(r"[a-z]", v):
  12. raise ValueError("Password must contain at least one lowercase letter")
  13. if not re.search(r"\d", v):
  14. raise ValueError("Password must contain at least one digit")
  15. if not re.search(r"[^A-Za-z0-9]", v):
  16. raise ValueError("Password must contain at least one special character")
  17. return v
  18. class GroupBrief(BaseModel):
  19. """Brief group info for embedding in user responses."""
  20. id: int
  21. name: str
  22. class Config:
  23. from_attributes = True
  24. class LoginRequest(BaseModel):
  25. username: str = Field(..., max_length=150)
  26. password: str = Field(..., max_length=256)
  27. class LoginResponse(BaseModel):
  28. access_token: str | None = None
  29. token_type: str = "bearer"
  30. user: "UserResponse | None" = None
  31. # Set when 2FA is required; the frontend must call /auth/2fa/verify
  32. requires_2fa: bool = False
  33. pre_auth_token: str | None = None
  34. two_fa_methods: list[str] = []
  35. class UserCreate(BaseModel):
  36. username: str = Field(..., max_length=150)
  37. password: str | None = Field(default=None, max_length=256) # M-NEW-4: cap before pbkdf2
  38. email: str | None = Field(default=None, max_length=254) # L-NEW-5: RFC 5321 max
  39. role: str = "user"
  40. group_ids: list[int] | None = None
  41. @field_validator("password")
  42. @classmethod
  43. def validate_password(cls, v: str | None) -> str | None:
  44. if v is not None:
  45. _validate_password_complexity(v)
  46. return v
  47. class UserUpdate(BaseModel):
  48. username: str | None = Field(default=None, max_length=150)
  49. password: str | None = Field(default=None, max_length=256) # M-NEW-4: cap before pbkdf2
  50. email: str | None = Field(default=None, max_length=254) # L-NEW-5: RFC 5321 max
  51. role: str | None = None
  52. is_active: bool | None = None
  53. group_ids: list[int] | None = None
  54. @field_validator("password")
  55. @classmethod
  56. def validate_password(cls, v: str | None) -> str | None:
  57. if v is not None:
  58. _validate_password_complexity(v)
  59. return v
  60. class UserResponse(BaseModel):
  61. id: int
  62. username: str
  63. email: str | None = None
  64. role: str # Deprecated, kept for backward compatibility
  65. is_active: bool
  66. is_admin: bool # Computed from role and group membership
  67. auth_source: str = "local" # "local" or "ldap"
  68. groups: list[GroupBrief] = []
  69. permissions: list[str] = [] # All permissions from groups
  70. created_at: str
  71. class Config:
  72. from_attributes = True
  73. class LDAPSearchResultResponse(BaseModel):
  74. """One match from GET /auth/ldap/search — surfaced in the admin UI."""
  75. username: str
  76. email: str | None = None
  77. display_name: str | None = None
  78. dn: str
  79. already_provisioned: bool = False # True if this username already exists as a BamBuddy user
  80. class LDAPProvisionRequest(BaseModel):
  81. """Body for POST /auth/ldap/provision. Username is re-resolved via the
  82. service-account bind, so the request only carries the directory username
  83. the admin picked from the search results."""
  84. username: str = Field(..., max_length=150)
  85. class ChangePasswordRequest(BaseModel):
  86. current_password: str = Field(..., max_length=256) # M-NEW-3: cap before pbkdf2
  87. new_password: str = Field(..., min_length=8, max_length=256)
  88. @field_validator("new_password")
  89. @classmethod
  90. def validate_new_password(cls, v: str) -> str:
  91. return _validate_password_complexity(v)
  92. class SetupRequest(BaseModel):
  93. auth_enabled: bool
  94. admin_username: str | None = Field(default=None, max_length=150)
  95. admin_password: str | None = Field(default=None, max_length=256)
  96. # Password complexity is NOT validated at the schema layer. When re-enabling auth
  97. # with an existing admin user (or when LDAP is the auth backend), the frontend
  98. # still sends whatever is in the password field but the route ignores it.
  99. # Enforcing complexity here would reject those legitimate flows. The route body
  100. # applies the check only when a brand-new local admin is actually being created.
  101. class SetupResponse(BaseModel):
  102. auth_enabled: bool
  103. admin_created: bool | None = None
  104. class ForgotPasswordRequest(BaseModel):
  105. email: str = Field(..., max_length=254) # L-NEW-1: RFC 5321 max; caps memory/CPU before lookup
  106. class ForgotPasswordConfirmRequest(BaseModel):
  107. token: str = Field(..., max_length=128)
  108. new_password: str = Field(..., min_length=8, max_length=256)
  109. @field_validator("new_password")
  110. @classmethod
  111. def validate_new_password(cls, v: str) -> str:
  112. return _validate_password_complexity(v)
  113. class ForgotPasswordResponse(BaseModel):
  114. message: str
  115. class ResetPasswordRequest(BaseModel):
  116. user_id: int
  117. class ResetPasswordResponse(BaseModel):
  118. message: str
  119. class SMTPSettings(BaseModel):
  120. smtp_host: str
  121. smtp_port: int
  122. smtp_username: str | None = None # Optional when auth is disabled
  123. smtp_password: str | None = None # Optional for read operations or when auth is disabled
  124. smtp_security: str = "starttls" # 'starttls', 'ssl', 'none'
  125. smtp_auth_enabled: bool = True
  126. smtp_from_email: str
  127. smtp_from_name: str = "BamBuddy"
  128. # Deprecated field for backward compatibility
  129. smtp_use_tls: bool | None = None
  130. class TestSMTPRequest(BaseModel):
  131. test_recipient: str
  132. class TestSMTPResponse(BaseModel):
  133. success: bool
  134. message: str
  135. # ---------------------------------------------------------------------------
  136. # 2FA / MFA schemas
  137. # ---------------------------------------------------------------------------
  138. class TwoFAStatusResponse(BaseModel):
  139. totp_enabled: bool
  140. email_otp_enabled: bool
  141. backup_codes_remaining: int
  142. class TOTPSetupResponse(BaseModel):
  143. """Returned when a user initiates TOTP setup. The frontend should display
  144. the QR code image (base64 PNG) and ask the user to scan it, then call
  145. /auth/2fa/totp/enable with a valid code to confirm."""
  146. secret: str # base32 secret (shown as fallback text)
  147. qr_code_b64: str # base64-encoded PNG of the QR code
  148. issuer: str
  149. class TOTPSetupRequest(BaseModel):
  150. """Optional body for POST /auth/2fa/totp/setup.
  151. Only required when re-initialising setup while an active TOTP record exists.
  152. Provide the current TOTP code (from the existing authenticator app) to
  153. confirm intent — mirrors the verification requirement in disable_totp.
  154. """
  155. code: str | None = Field(default=None, max_length=8) # L-NEW-2: bound before pyotp
  156. class TOTPEnableRequest(BaseModel):
  157. code: str # 6-digit TOTP code from the authenticator app
  158. @field_validator("code")
  159. @classmethod
  160. def validate_code(cls, v: str) -> str:
  161. v = v.strip()
  162. if not v.isdigit() or len(v) != 6:
  163. raise ValueError("TOTP code must be exactly 6 digits")
  164. return v
  165. class TOTPEnableResponse(BaseModel):
  166. message: str
  167. backup_codes: list[str] # plain-text codes shown once; user must save them
  168. class TOTPDisableRequest(BaseModel):
  169. """Requires a valid TOTP code OR a backup code to disable TOTP."""
  170. code: str = Field(..., max_length=128)
  171. class BackupCodesResponse(BaseModel):
  172. backup_codes: list[str]
  173. message: str
  174. class EmailOTPEnableRequest(BaseModel):
  175. """No body required — email is taken from the authenticated user's profile."""
  176. pass
  177. class TwoFAVerifyRequest(BaseModel):
  178. pre_auth_token: str = Field(..., max_length=128)
  179. # TOTP/email codes are 6 digits; backup codes are 8 uppercase alphanumeric chars.
  180. # max_length=8 prevents excessively long inputs from reaching pbkdf2/pyotp.
  181. code: str = Field(..., min_length=6, max_length=8)
  182. method: Literal["totp", "email", "backup"] = "totp"
  183. @field_validator("code")
  184. @classmethod
  185. def validate_code_format(cls, v: str) -> str:
  186. v = v.strip()
  187. if not re.match(r"^[A-Za-z0-9]{6,8}$", v):
  188. raise ValueError("Code must be 6–8 alphanumeric characters")
  189. return v.upper() # normalise backup codes to uppercase
  190. class TwoFAVerifyResponse(BaseModel):
  191. access_token: str
  192. token_type: str = "bearer"
  193. user: "UserResponse"
  194. class EmailOTPSendRequest(BaseModel):
  195. pre_auth_token: str = Field(..., max_length=128)
  196. class EmailOTPEnableConfirmRequest(BaseModel):
  197. """Body for the second step of email OTP enable: verify the proof-of-possession code."""
  198. setup_token: str = Field(..., max_length=128)
  199. # L-NEW-3: email OTP setup codes are always exactly 6 digits; reject anything else.
  200. code: str = Field(..., min_length=6, max_length=6)
  201. @field_validator("code")
  202. @classmethod
  203. def validate_code_digits(cls, v: str) -> str:
  204. v = v.strip()
  205. if not v.isdigit() or len(v) != 6:
  206. raise ValueError("Email OTP setup code must be exactly 6 digits")
  207. return v
  208. class EmailOTPDisableRequest(BaseModel):
  209. """Requires the account password to disable email OTP."""
  210. password: str = Field(..., max_length=256)
  211. class AdminDisable2FARequest(BaseModel):
  212. """Admin must supply their own password as re-auth before disabling 2FA for another user.
  213. OIDC/LDAP-only admins (no local password_hash) are exempt from this check.
  214. """
  215. admin_password: str | None = Field(default=None, max_length=256)
  216. # ---------------------------------------------------------------------------
  217. # OIDC schemas
  218. # ---------------------------------------------------------------------------
  219. AUTO_LINK_REQUIREMENTS_ERROR = (
  220. "auto_link_existing_accounts requires require_email_verified=True when email_claim='email'"
  221. )
  222. def _validate_email_claim_name(v: str) -> str:
  223. # Accepts only alphanumeric/underscore/hyphen claim names starting with a letter —
  224. # prevents log injection and limits the attack surface of operator-supplied claim names.
  225. if not re.fullmatch(r"[a-zA-Z][a-zA-Z0-9_\-]{0,63}", v):
  226. raise ValueError("Invalid claim name")
  227. return v
  228. def _validate_icon_url(v: str | None) -> str | None:
  229. """Reject non-HTTPS icon URLs and SSRF-unsafe hosts.
  230. Delegates to the runtime SSRF guard ``assert_safe_public_https_url``
  231. so the Pydantic layer enforces the same allowlist as the fetcher —
  232. no policy drift between schema validation and SSRF check. Without
  233. this delegation the validator covered only ``is_private | is_loopback
  234. | is_link_local`` while the runtime additionally rejected numeric-
  235. encoded IPs, cloud-metadata endpoints, multicast, unspecified, and
  236. IPv4-mapped IPv6.
  237. Lazy-imported because ``_oidc_helpers`` lives under ``api/routes/``
  238. and schemas avoid top-level imports from that layer (matches the
  239. existing pattern in ``_validate_issuer_url`` which lazy-imports
  240. ``ipaddress``).
  241. """
  242. if v is None:
  243. return v
  244. if not v.startswith("https://"):
  245. # Surface the same wording the runtime guard would use, but pre-
  246. # checked here so the user-facing error doesn't depend on the
  247. # runtime call path.
  248. raise ValueError("icon_url must start with https://")
  249. from backend.app.api.routes._oidc_helpers import assert_safe_public_https_url
  250. try:
  251. assert_safe_public_https_url(v)
  252. except ValueError as exc:
  253. raise ValueError(f"icon_url: {exc}") from exc
  254. return v
  255. def _validate_issuer_url(v: str | None) -> str | None:
  256. """Reject non-HTTPS issuer URLs and SSRF-unsafe hosts.
  257. An OIDC provider must be reachable over TLS on the public internet, so
  258. this uses the public-internet policy: private, loopback and link-local
  259. addresses are all rejected.
  260. Delegates to the runtime guard ``assert_safe_public_https_url`` for the
  261. same reason ``_validate_icon_url`` does — no policy drift between the
  262. schema layer and the fetcher. The hand-rolled version this replaced
  263. checked only ``is_private | is_loopback | is_link_local``, which left
  264. numeric-encoded IPs (``https://2130706433/``), IPv4-mapped IPv6
  265. (``https://[::ffff:127.0.0.1]/``), multicast and unspecified addresses
  266. able to express a target the policy meant to forbid. The guard's
  267. docstring already claimed the two were consistent; now they are.
  268. Lazy-imported because ``_oidc_helpers`` lives under ``api/routes/`` and
  269. schemas avoid top-level imports from that layer.
  270. """
  271. if v is None:
  272. return v
  273. if not v.startswith("https://"):
  274. raise ValueError("issuer_url must start with https://")
  275. from backend.app.api.routes._oidc_helpers import assert_safe_public_https_url
  276. try:
  277. assert_safe_public_https_url(v)
  278. except ValueError as exc:
  279. # The guard's messages say "icon URL" — rewrite for this field so the
  280. # user sees the setting they actually submitted.
  281. detail = str(exc).replace("icon URL", "issuer_url")
  282. raise ValueError(detail) from exc
  283. return v
  284. def _validate_scopes(v: str | None) -> str | None:
  285. """Nit5: Require that the 'openid' scope is present.
  286. The OpenID Connect spec mandates the 'openid' scope; without it the
  287. response is plain OAuth2, not OIDC, and claims like sub/email are not
  288. guaranteed.
  289. """
  290. if v is None:
  291. return v
  292. scope_list = v.split()
  293. if "openid" not in scope_list:
  294. raise ValueError("scopes must include 'openid'")
  295. return v
  296. class OIDCProviderCreate(BaseModel):
  297. name: str = Field(..., max_length=100) # L-NEW-4
  298. issuer_url: str
  299. client_id: str = Field(..., max_length=256) # L-NEW-4
  300. client_secret: str = Field(..., max_length=512) # L-NEW-4: Fernet input bounded
  301. scopes: str = Field(default="openid email profile", max_length=256) # L-NEW-4
  302. is_enabled: bool = True
  303. auto_create_users: bool = False
  304. auto_link_existing_accounts: bool = False # M-2: conservative default, opt-in only
  305. email_claim: str = Field(default="email", max_length=64)
  306. require_email_verified: bool = True
  307. icon_url: str | None = None
  308. default_group_id: int | None = None
  309. is_autologin: bool = False # #1589 — at most one provider may carry this
  310. @field_validator("issuer_url")
  311. @classmethod
  312. def validate_issuer_url(cls, v: str) -> str:
  313. result = _validate_issuer_url(v)
  314. if result is None:
  315. raise ValueError("issuer_url is required")
  316. return result
  317. @field_validator("scopes")
  318. @classmethod
  319. def validate_scopes(cls, v: str) -> str:
  320. result = _validate_scopes(v)
  321. if result is None:
  322. raise ValueError("scopes is required")
  323. return result
  324. @field_validator("email_claim")
  325. @classmethod
  326. def validate_email_claim(cls, v: str) -> str:
  327. return _validate_email_claim_name(v)
  328. @field_validator("icon_url")
  329. @classmethod
  330. def validate_icon_url(cls, v: str | None) -> str | None:
  331. return _validate_icon_url(v)
  332. # SEC-1: auto_link with email_claim='email' requires require_email_verified=True.
  333. # Fall B (require_email_verified=False + email_claim='email') accepts absent email_verified → account-takeover risk.
  334. # Fall C (custom claim != 'email') is safe: no email_verified gate on that path regardless of require_email_verified.
  335. @model_validator(mode="after")
  336. def check_auto_link_requires_verified(self) -> "OIDCProviderCreate":
  337. if self.auto_link_existing_accounts and self.email_claim == "email" and not self.require_email_verified:
  338. raise ValueError(AUTO_LINK_REQUIREMENTS_ERROR)
  339. return self
  340. class OIDCProviderUpdate(BaseModel):
  341. name: str | None = Field(default=None, max_length=100)
  342. issuer_url: str | None = None
  343. @field_validator("issuer_url")
  344. @classmethod
  345. def validate_issuer_url(cls, v: str | None) -> str | None:
  346. return _validate_issuer_url(v)
  347. client_id: str | None = Field(default=None, max_length=256)
  348. client_secret: str | None = Field(default=None, max_length=512)
  349. scopes: str | None = Field(default=None, max_length=256)
  350. is_enabled: bool | None = None
  351. auto_create_users: bool | None = None
  352. auto_link_existing_accounts: bool | None = None
  353. email_claim: str | None = Field(default=None, max_length=64)
  354. require_email_verified: bool | None = None
  355. icon_url: str | None = None
  356. default_group_id: int | None = None
  357. is_autologin: bool | None = None # #1589
  358. @field_validator("scopes")
  359. @classmethod
  360. def validate_scopes(cls, v: str | None) -> str | None:
  361. return _validate_scopes(v)
  362. @field_validator("email_claim")
  363. @classmethod
  364. def validate_email_claim(cls, v: str | None) -> str | None:
  365. if v is None:
  366. return None
  367. return _validate_email_claim_name(v)
  368. @field_validator("icon_url")
  369. @classmethod
  370. def validate_icon_url(cls, v: str | None) -> str | None:
  371. return _validate_icon_url(v)
  372. # SEC-1 (schema-level): blocks only when auto_link=True + email_claim='email' + require_email_verified=False
  373. # arrive in the same request. email_claim=None means the request leaves it unchanged (still 'email' by default),
  374. # so that is also treated as 'email'. Partial updates spanning two requests are caught by the
  375. # Combined-State-Guard in the route handler after the setattr loop.
  376. @model_validator(mode="after")
  377. def check_auto_link_requires_verified(self) -> "OIDCProviderUpdate":
  378. if (
  379. self.auto_link_existing_accounts is True
  380. and self.require_email_verified is False
  381. and (self.email_claim is None or self.email_claim == "email")
  382. ):
  383. raise ValueError(AUTO_LINK_REQUIREMENTS_ERROR)
  384. return self
  385. class OIDCProviderResponse(BaseModel):
  386. id: int
  387. name: str
  388. issuer_url: str
  389. client_id: str
  390. scopes: str
  391. is_enabled: bool
  392. auto_create_users: bool
  393. auto_link_existing_accounts: bool = False
  394. email_claim: str = "email"
  395. require_email_verified: bool = True
  396. icon_url: str | None = None
  397. default_group_id: int | None = None
  398. is_autologin: bool = False # #1589
  399. # #2593 — the UI renders this provider read-only; without the flag it would
  400. # offer editable fields whose writes the API then refuses with 409.
  401. is_env_managed: bool = False
  402. # Set explicitly in the route handler from `icon_content_type is not None`
  403. # rather than `@computed_field` (project policy) or `icon_data is not None`
  404. # (would trigger an async lazy-load on the deferred BLOB column).
  405. # Required (no default) so Pydantic fails loudly if any code path skips
  406. # `_build_provider_response` and tries `model_validate(provider)` directly.
  407. has_icon: bool
  408. class Config:
  409. from_attributes = True
  410. class OIDCAuthorizeResponse(BaseModel):
  411. auth_url: str
  412. class OIDCExchangeRequest(BaseModel):
  413. oidc_token: str = Field(..., max_length=128)
  414. class OIDCLinkResponse(BaseModel):
  415. id: int
  416. provider_id: int
  417. provider_name: str
  418. provider_email: str | None = None
  419. created_at: str
  420. class EncryptionRowCounts(BaseModel):
  421. oidc_providers: int
  422. user_totp: int
  423. class EncryptionStatusResponse(BaseModel):
  424. key_configured: bool
  425. key_source: Literal["env", "file", "generated", "none"]
  426. legacy_plaintext_rows: EncryptionRowCounts
  427. encrypted_rows: EncryptionRowCounts
  428. # B4: filled by the endpoint after a sample-decrypt of one encrypted row,
  429. # so a wrong-key state (where key_configured=True but rows decrypt to junk)
  430. # is detected, not just the no-key case.
  431. decryption_broken: bool = False
  432. # B2: number of rows skipped during the last legacy re-encryption migration.
  433. # Filled from backend.app.core.database.get_migration_error_count().
  434. migration_error_count: int = 0