oidc_provider.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from sqlalchemy import (
  4. Boolean,
  5. CheckConstraint,
  6. DateTime,
  7. ForeignKey,
  8. Integer,
  9. LargeBinary,
  10. String,
  11. Text,
  12. UniqueConstraint,
  13. func,
  14. )
  15. from sqlalchemy.orm import Mapped, mapped_column, relationship
  16. from backend.app.core.database import Base
  17. from backend.app.core.encryption import mfa_decrypt, mfa_encrypt
  18. class OIDCProvider(Base):
  19. """OpenID Connect provider configuration.
  20. Supports any standards-compliant OIDC provider such as PocketID,
  21. Authentik, Keycloak, Authelia, Google, etc.
  22. The issuer_url must point to the root issuer (e.g. ``https://id.example.com``).
  23. The OIDC discovery document is fetched from
  24. ``{issuer_url}/.well-known/openid-configuration`` at runtime.
  25. """
  26. __tablename__ = "oidc_providers"
  27. __table_args__ = (
  28. # DB-level enforcement of SEC-1: blocks only Fall B (email_claim='email' + require_ev=False).
  29. # Fall C (custom claim) is safe — no email_verified gate on that path.
  30. # Enforced on new installations; existing tables updated via _migrate_update_auto_link_constraint.
  31. CheckConstraint(
  32. "auto_link_existing_accounts = FALSE OR email_claim != 'email' OR require_email_verified = TRUE",
  33. name="ck_auto_link_requires_verified_email_claim",
  34. ),
  35. # All-or-nothing icon-cache record (#1333). The application keeps the
  36. # triplet consistent via _fetch_icon_or_400 + DELETE /icon, but a CHECK
  37. # constraint at the DB layer prevents drift from raw SQL maintenance
  38. # scripts, manual UPDATEs during incident recovery, etc.
  39. # Fresh installs (SQLite + PostgreSQL) get this via metadata.create_all.
  40. # Stale PostgreSQL installs get it via ALTER TABLE ADD CONSTRAINT in
  41. # run_migrations. SQLite cannot ADD CONSTRAINT to an existing table —
  42. # stale SQLite installs rely on the application layer, the same
  43. # trade-off documented for the default_group_id FK ON DELETE SET NULL.
  44. CheckConstraint(
  45. "(icon_data IS NULL) = (icon_content_type IS NULL) AND (icon_content_type IS NULL) = (icon_etag IS NULL)",
  46. name="ck_oidc_icon_triplet_co_null",
  47. ),
  48. )
  49. id: Mapped[int] = mapped_column(primary_key=True)
  50. # Human-readable name shown on the login button (e.g. "PocketID", "Google")
  51. name: Mapped[str] = mapped_column(String(100), unique=True)
  52. # Full OIDC issuer URL (e.g. "https://id.example.com")
  53. issuer_url: Mapped[str] = mapped_column(String(500))
  54. client_id: Mapped[str] = mapped_column(String(255))
  55. # Encrypted at rest when MFA_ENCRYPTION_KEY is set.
  56. # Use .client_secret / .client_secret setter rather than _client_secret_enc directly.
  57. _client_secret_enc: Mapped[str] = mapped_column("client_secret", String(512))
  58. @property
  59. def client_secret(self) -> str:
  60. return mfa_decrypt(self._client_secret_enc)
  61. @client_secret.setter
  62. def client_secret(self, value: str) -> None:
  63. self._client_secret_enc = mfa_encrypt(value)
  64. # Space-separated scopes; must include "openid"
  65. scopes: Mapped[str] = mapped_column(String(500), default="openid email profile")
  66. is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
  67. # When True, a new local user is created automatically on first OIDC login
  68. auto_create_users: Mapped[bool] = mapped_column(Boolean, default=False)
  69. # When True, an existing local user whose email matches the OIDC claim is
  70. # automatically linked on first SSO login. Default is False (conservative):
  71. # operators must explicitly opt-in to prevent an attacker-controlled IdP from
  72. # silently hijacking local accounts via email matching (M-2 fix).
  73. auto_link_existing_accounts: Mapped[bool] = mapped_column(Boolean, default=False)
  74. # JWT claim name used as the email identity (default "email").
  75. # Set to "preferred_username" or "upn" for Azure Entra ID, which does not send
  76. # email_verified — using a custom claim skips the email_verified check entirely
  77. # and is the recommended Azure configuration.
  78. # Has no interaction with require_email_verified when set to a non-"email" value:
  79. # custom claims never perform an email_verified check regardless of that setting.
  80. email_claim: Mapped[str] = mapped_column(String(64), default="email")
  81. # When True (default), the "email" claim is only trusted when email_verified=True.
  82. # Set to False to accept the email even when email_verified is absent — required
  83. # for providers like Azure Entra ID that never send email_verified and where a
  84. # custom claim (email_claim != "email") is not preferred.
  85. # Has no effect when email_claim is not "email": the custom-claim path never
  86. # performs an email_verified check regardless of this setting.
  87. require_email_verified: Mapped[bool] = mapped_column(Boolean, default=True)
  88. # Nullable FK — configurable default group for auto-created OIDC users.
  89. # Falls back to "Viewers" when None. ON DELETE SET NULL fires on PostgreSQL;
  90. # SQLite ignores it (no PRAGMA foreign_keys=ON), so runtime resolution handles dangling refs.
  91. default_group_id: Mapped[int | None] = mapped_column(
  92. Integer, ForeignKey("groups.id", ondelete="SET NULL"), nullable=True, default=None
  93. )
  94. # Optional icon URL the admin entered. The actual image bytes are fetched
  95. # server-side and cached in icon_data — the SPA never hotlinks this URL
  96. # (would require loosening img-src CSP; see PR #1333 / issue #1333).
  97. icon_url: Mapped[str | None] = mapped_column(Text, nullable=True, default=None)
  98. # Cached icon bytes (PNG/JPEG/WebP/GIF). Marked deferred=True so that
  99. # list-style queries (`GET /oidc/providers`) don't pull the BLOB on every
  100. # login-page render — only the GET /icon endpoint un-defers it via
  101. # `select(...).options(undefer(...))`.
  102. icon_data: Mapped[bytes | None] = mapped_column(LargeBinary, nullable=True, default=None, deferred=True)
  103. # MIME type derived from the fetched icon (e.g. "image/png"). Also serves
  104. # as the "has-icon" indicator — checked instead of icon_data so we never
  105. # accidentally trigger an async lazy-load on the deferred BLOB column.
  106. # Width 20 is plenty: the longest whitelisted value is "image/jpeg" (10
  107. # chars). Tighter than 50 so the schema documents the intent.
  108. icon_content_type: Mapped[str | None] = mapped_column(String(20), nullable=True, default=None)
  109. # SHA-256 hex of icon_data, served as the ETag header so clients can
  110. # revalidate via If-None-Match and receive 304 Not Modified.
  111. icon_etag: Mapped[str | None] = mapped_column(String(64), nullable=True, default=None)
  112. @property
  113. def has_icon(self) -> bool:
  114. """True when cached icon bytes exist. Reads the non-deferred
  115. ``icon_content_type`` column so accessing this never triggers an
  116. async lazy-load on the deferred ``icon_data`` BLOB."""
  117. return self.icon_content_type is not None
  118. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  119. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  120. # Relationship to linked user accounts
  121. user_links: Mapped[list[UserOIDCLink]] = relationship(
  122. "UserOIDCLink",
  123. back_populates="provider",
  124. cascade="all, delete-orphan",
  125. )
  126. def __repr__(self) -> str:
  127. return f"<OIDCProvider {self.name!r}>"
  128. class UserOIDCLink(Base):
  129. """Links a local Bambuddy user account to an identity at an OIDC provider."""
  130. __tablename__ = "user_oidc_links"
  131. __table_args__ = (
  132. # T2: Prevent duplicate OIDC identities and duplicate provider links.
  133. # (provider_id, provider_user_id) — one OIDC sub per provider maps to at most one local user.
  134. UniqueConstraint("provider_id", "provider_user_id", name="uq_oidc_link_provider_sub"),
  135. # (user_id, provider_id) — one local user can link to each provider at most once.
  136. UniqueConstraint("user_id", "provider_id", name="uq_oidc_link_user_provider"),
  137. )
  138. id: Mapped[int] = mapped_column(primary_key=True)
  139. user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id", ondelete="CASCADE"), index=True)
  140. provider_id: Mapped[int] = mapped_column(Integer, ForeignKey("oidc_providers.id", ondelete="CASCADE"), index=True)
  141. # The "sub" claim from the OIDC ID token — stable identifier for the user
  142. provider_user_id: Mapped[str] = mapped_column(String(500))
  143. # Email returned by the provider (informational; may differ from local email)
  144. provider_email: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None)
  145. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  146. provider: Mapped[OIDCProvider] = relationship("OIDCProvider", back_populates="user_links")
  147. def __repr__(self) -> str:
  148. return f"<UserOIDCLink user_id={self.user_id} provider_id={self.provider_id}>"