Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-026 — Second factor decided per tenant

Date: 2026-11-16 Status: Accepted Extends: ADR-016 (TOTP second factor), ADR-023 (hierarchical settings). Supersedes in part: ADR-010 — a token refresh no longer resets to the default tenant. Library side: DevPack ADR-017 (IMfaPolicyProvider, MfaSettingDefinitions, tenant-bound challenge, 10.28.0). Origin: Azure DevOps US 10332; plan docs/plans/2026-11-16-feat-mfa-por-tenant-plano.md.

Context​

The second-factor mode (Disabled · Optional · Required) and its cutover (EnforceFrom) were one decision for the whole deployment (ADR-016). A customer that requires MFA forced it on every other tenant sharing the deployment.

Decision​

The mode and the cutover are settings Mfa.Mode and Mfa.EnforceFrom (DevPack MfaSettingDefinitions, levels Global and Tenant, EditableBy = System). Without an override the value is Authentication:Mfa:Mode / EnforceFrom — the configuration layer of those keys — so a deployment that sets nothing behaves as before. They show up in the system settings screens with their origin; there is no new screen.

  • One rule for entering a tenant (ISecondFactorGate): a local account holding a factor is asked for it in every mode but Disabled; one without a factor is sent to enrol under Required once the tenant's cutover passed; an external-provider account (Entra) owes nothing — its provider owns the factor. Every path that changes the active tenant applies it: password sign-in, tenant switch (both channels), cookie refresh and token refresh.
  • The tenant comes before the factor. PasswordSignInFlow resolves the tenant (IActiveTenantSelector), decides under that tenant's policy and binds the challenge to it (MfaChallenge.TenantId); answering signs into that tenant. An account without a membership is decided under the deployment-wide policy, so it is still asked to enrol before it learns it has no tenant (ADR-016's order).
  • A session remembers that it answered the factor: the cookie's .devpack.session.second_factor_at stamp, the access token's amr=mfa claim and the refresh token's SecondFactorAt column. A session that answered it is not asked again when it switches into another tenant that requires it.
  • Switching into a tenant that requires a factor the session never answered answers like the sign-in: 401 Auth.MfaRequired / Auth.MfaEnrollmentRequired with the challenge (cookie for the browser, body for Bearer). The current session is kept; answering at /session/mfa, /session/mfa/enrollment or /token/mfa signs into the target tenant. The SPA asks for it in a dialog from the tenant switcher.
  • A token refresh stays in the token's tenant (RefreshTokens.TenantId) while the user is still a member, instead of resetting to the smallest-id tenant. A refresh — token or cookie — that lands in another tenant (membership removed) is refused with Auth.SessionPredatesMfa (sign in again — no challenge is opened on a refresh) when that tenant requires a factor the session never answered. A refresh token minted before this release records no tenant, so it faces the policy of whatever tenant it lands in: in a deployment with the factor on, an enrolled user's old token is refused once and the app signs in again.
  • The cutover is per tenant and spares sessions that answered the factor: after the tenant's EnforceFrom, a cookie or refresh token opened before it and without the second-factor stamp is refused. Before the date nothing drops. Sessions minted before this release have no stamp and follow ADR-016's rule exactly.
  • Session stamps are read only from the caller's own cookie. A request can carry a Bearer principal and somebody else's cookie; the opening and second-factor stamps are taken from the cookie only when its sub is the caller.
  • A tenant switch carries where the session started. The Bearer re-issue stamps the new refresh token with the access token's iat, not now — before, a switch after the cutover minted a "post-cutover" pair renewable forever.
  • Key ring: turning the factor on (any level) outside Development without DataProtection:BlobUri is refused at write time (MfaSettingsValidator, settings.group_invalid on Mfa.Mode); the boot guard keeps covering the configuration.

Consequences​

  • Migration MfaPerTenant: identity.MfaChallenges.TenantId, identity.RefreshTokens.TenantId and SecondFactorAt (nullable, no backfill). MfaChallenge and RefreshTokenEntity join the allowlist of AssertEveryMultiTenantEntityIsFiltered: their TenantId is the tenant a sign-in or session is for, read before any ambient tenant exists.
  • A policy change reaches other replicas within the settings L1 window (30 s). Access tokens already issued stay valid until they expire (minutes).
  • /me's canEnrollSecondFactor and the profile's self-enrolment follow the active tenant's policy.
  • Issuer, lifetimes, attempts and retention stay deployment-wide (Authentication:Mfa).
  • Required with no EnforceFrom drops no live session — ADR-016's rule, now one click away in a screen: whoever signs in or switches is asked for the factor, but cookies already open keep sliding and refresh tokens keep rotating. To reach live sessions, set a date (Mfa.EnforceFrom, which may be now).
  • Mfa.Mode and Mfa.EnforceFrom resolve each from its own layer. A tenant that overrides only the mode inherits the date from the global override or the server configuration; the settings screen shows the origin of each.
  • A stored value that no longer fits (InvalidStoredValue = Throw) fails the read — sign-in and cookie validation of that tenant answer 500 rather than fall back to a weaker policy. Values are validated on write.

Alternatives considered​

  • Ask for the factor on every switch into a tenant that requires it. No session state, but a token refresh would have to refuse every session of such a tenant, and the mobile app would sign in again at every expiry.
  • Keep ADR-010's reset to the default tenant on refresh and refuse instead. Simpler, but a mobile client signed in to a tenant with the factor off whose default tenant requires it would never refresh.
  • Definitions in the template. Every DevPack consumer with Settings and MFA wants the same pair of keys (G-01).