oidc_provider.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from sqlalchemy import Boolean, CheckConstraint, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint, func
  4. from sqlalchemy.orm import Mapped, mapped_column, relationship
  5. from backend.app.core.database import Base
  6. from backend.app.core.encryption import mfa_decrypt, mfa_encrypt
  7. class OIDCProvider(Base):
  8. """OpenID Connect provider configuration.
  9. Supports any standards-compliant OIDC provider such as PocketID,
  10. Authentik, Keycloak, Authelia, Google, etc.
  11. The issuer_url must point to the root issuer (e.g. ``https://id.example.com``).
  12. The OIDC discovery document is fetched from
  13. ``{issuer_url}/.well-known/openid-configuration`` at runtime.
  14. """
  15. __tablename__ = "oidc_providers"
  16. __table_args__ = (
  17. # DB-level enforcement of SEC-1/SEC-6: auto_link is only safe when
  18. # require_email_verified=True AND email_claim='email'. Enforced on new
  19. # installations; existing tables get this via the PostgreSQL-only migration.
  20. CheckConstraint(
  21. "auto_link_existing_accounts = FALSE OR (require_email_verified = TRUE AND email_claim = 'email')",
  22. name="ck_auto_link_requires_verified_email_claim",
  23. ),
  24. )
  25. id: Mapped[int] = mapped_column(primary_key=True)
  26. # Human-readable name shown on the login button (e.g. "PocketID", "Google")
  27. name: Mapped[str] = mapped_column(String(100), unique=True)
  28. # Full OIDC issuer URL (e.g. "https://id.example.com")
  29. issuer_url: Mapped[str] = mapped_column(String(500))
  30. client_id: Mapped[str] = mapped_column(String(255))
  31. # Encrypted at rest when MFA_ENCRYPTION_KEY is set.
  32. # Use .client_secret / .client_secret setter rather than _client_secret_enc directly.
  33. _client_secret_enc: Mapped[str] = mapped_column("client_secret", String(512))
  34. @property
  35. def client_secret(self) -> str:
  36. return mfa_decrypt(self._client_secret_enc)
  37. @client_secret.setter
  38. def client_secret(self, value: str) -> None:
  39. self._client_secret_enc = mfa_encrypt(value)
  40. # Space-separated scopes; must include "openid"
  41. scopes: Mapped[str] = mapped_column(String(500), default="openid email profile")
  42. is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
  43. # When True, a new local user is created automatically on first OIDC login
  44. auto_create_users: Mapped[bool] = mapped_column(Boolean, default=False)
  45. # When True, an existing local user whose email matches the OIDC claim is
  46. # automatically linked on first SSO login. Default is False (conservative):
  47. # operators must explicitly opt-in to prevent an attacker-controlled IdP from
  48. # silently hijacking local accounts via email matching (M-2 fix).
  49. auto_link_existing_accounts: Mapped[bool] = mapped_column(Boolean, default=False)
  50. # JWT claim name used as the email identity (default "email").
  51. # Set to "preferred_username" or "upn" for Azure Entra ID, which does not send
  52. # email_verified — using a custom claim skips the email_verified check entirely
  53. # and is the recommended Azure configuration.
  54. # Has no interaction with require_email_verified when set to a non-"email" value:
  55. # custom claims never perform an email_verified check regardless of that setting.
  56. email_claim: Mapped[str] = mapped_column(String(64), default="email")
  57. # When True (default), the "email" claim is only trusted when email_verified=True.
  58. # Set to False to accept the email even when email_verified is absent — required
  59. # for providers like Azure Entra ID that never send email_verified and where a
  60. # custom claim (email_claim != "email") is not preferred.
  61. # Has no effect when email_claim is not "email": the custom-claim path never
  62. # performs an email_verified check regardless of this setting.
  63. require_email_verified: Mapped[bool] = mapped_column(Boolean, default=True)
  64. # Optional icon URL (SVG/PNG) shown on the login button
  65. icon_url: Mapped[str | None] = mapped_column(Text, nullable=True, default=None)
  66. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  67. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  68. # Relationship to linked user accounts
  69. user_links: Mapped[list[UserOIDCLink]] = relationship(
  70. "UserOIDCLink",
  71. back_populates="provider",
  72. cascade="all, delete-orphan",
  73. )
  74. def __repr__(self) -> str:
  75. return f"<OIDCProvider {self.name!r}>"
  76. class UserOIDCLink(Base):
  77. """Links a local Bambuddy user account to an identity at an OIDC provider."""
  78. __tablename__ = "user_oidc_links"
  79. __table_args__ = (
  80. # T2: Prevent duplicate OIDC identities and duplicate provider links.
  81. # (provider_id, provider_user_id) — one OIDC sub per provider maps to at most one local user.
  82. UniqueConstraint("provider_id", "provider_user_id", name="uq_oidc_link_provider_sub"),
  83. # (user_id, provider_id) — one local user can link to each provider at most once.
  84. UniqueConstraint("user_id", "provider_id", name="uq_oidc_link_user_provider"),
  85. )
  86. id: Mapped[int] = mapped_column(primary_key=True)
  87. user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id", ondelete="CASCADE"), index=True)
  88. provider_id: Mapped[int] = mapped_column(Integer, ForeignKey("oidc_providers.id", ondelete="CASCADE"), index=True)
  89. # The "sub" claim from the OIDC ID token — stable identifier for the user
  90. provider_user_id: Mapped[str] = mapped_column(String(500))
  91. # Email returned by the provider (informational; may differ from local email)
  92. provider_email: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None)
  93. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  94. provider: Mapped[OIDCProvider] = relationship("OIDCProvider", back_populates="user_links")
  95. def __repr__(self) -> str:
  96. return f"<UserOIDCLink user_id={self.user_id} provider_id={self.provider_id}>"