Files
sure/app/models/auth_config.rb
T
Guillem Arias Fauste 344cf091e1 feat(auth): sign in with a passkey, without a password (#2911)
* feat(auth): sign in with a passkey, without a password

Passkeys could only ever replace the TOTP code: registration required 2FA
to already be on, and the WebAuthn ceremony was reachable only after
User.authenticate_by had succeeded. A registered passkey can now complete
sign-in on its own, from the login page.

The ceremony requests userVerification: "required", so the authenticator
has to confirm the person as well as the device. That makes a lone passkey
two independent factors, the same bar as the password plus TOTP flow it
replaces, which is why this path deliberately skips the TOTP step. A
credential that can only prove presence is rejected here and still works
as a second factor.

Sign-in is usernameless: no email is submitted, because the browser
returns the account handle with the assertion. Nothing on this path can be
probed to learn whether an account exists. Registration now asks for a
discoverable credential with residentKey: "preferred" so the key is
offered by the picker, while authenticators without a free resident-key
slot still register as a second factor.

Where conditional mediation is available, saved passkeys appear in the
email field's autofill menu; everywhere else the button covers it. The
automatic challenge request that conditional mediation makes on every page
load gets its own looser Rack::Attack budget, so ordinary page views can
no longer exhaust the limit that protects the MFA endpoints.

Set AUTH_PASSKEY_LOGIN_ENABLED=false to keep passkeys as a second factor
only. Passkey sign-in follows the same policy as local login, so it stays
closed to regular users when AUTH_LOCAL_LOGIN_ENABLED is false.

* refactor(auth): group the passkey button with the other sign-in methods

It sat directly under the password fields, so the forgot-password link
split it from the identical SSO buttons. It is an alternative to the
credential form rather than part of it.

* fix(auth): close the passkey challenge races and document the upgrade

Three review passes converged on the conditional-mediation flow. The
AbortController was created after `isConditionalMediationAvailable()`
resolved, so a button click or a Turbo disconnect landing in that window
found nothing to abort: the conditional task carried on, re-minted the
challenge, and the assertion the user was about to produce verified
against a challenge the server had already replaced.

It is created before the first await now, and held in a local, because
`abortConditionalMediation()` nulls the field. Checking that one signal
after each await covers both triggers, so no separate connected flag is
needed.

The same symptom had a second cause nobody flagged: `authenticate()` was
not re-entrant. A double-click minted a fresh challenge under an open
authenticator prompt and rejected a perfectly valid passkey, with no race
window at all — and it was live on the MFA step-up too, which shares the
method.

The conditional catch was silent for every failure, including a rejected
assertion the user had deliberately chosen from the autofill menu.
Splitting the try draws the line where it belongs: silence before the
user has been asked anything, feedback once they have picked a passkey.
Filtering on `error.name` cannot draw it, since `fetchOptions` and
`verifyCredential` both raise a plain Error.

Also documents the upgrade: passwordless is on by default and applies to
already-registered credentials, so a passkey added purely as a second
factor can now sign its owner in alone. Nothing in the schema marks a
credential discoverable — the authenticator decides — and the opt-out is
instance-wide.

The invitation test is a guard, not coverage for this change. The pending
token lives in the Rack session and `complete_sign_in` reads it right
after creating the session, so a `reset_session` dropped in between
strands the invitee in their own family, silently and with every existing
test still green.

* fix(auth): cancel the in-flight conditional options request

Aborting the conditional flow did not cancel its options request, because
`fetchOptions` never received the signal. A click landing while that POST
was in flight left it to finish, and its response could apply last.

The challenge rides in the session cookie, so "the server wrote it" only
counts if the Set-Cookie reaches the browser. Threading the signal means
an aborted request's response is discarded, which closes the window
without needing the server to hold two challenges open.

Also drops the absolute claim about which existing credentials gain
passwordless sign-in. `residentKey: "preferred"` is a request an
authenticator may decline, and nothing records what it decided, so the
honest statement is that password managers and platform authenticators
generally store discoverable credentials rather than always.
2026-08-12 20:35:13 +02:00

98 lines
3.6 KiB
Ruby

# frozen_string_literal: true
class AuthConfig
class << self
def local_login_enabled?
# Default to true if not configured to preserve existing behavior.
value = Rails.configuration.x.auth.local_login_enabled
value.nil? ? true : !!value
end
def local_admin_override_enabled?
!!Rails.configuration.x.auth.local_admin_override_enabled
end
# Whether a registered passkey may be used to sign in on its own, skipping
# both the password and the TOTP step. Defaults to true when unconfigured.
def passkey_login_enabled?
value = Rails.configuration.x.auth.passkey_login_enabled
value.nil? ? true : !!value
end
# When the local login form should be visible on the login page.
# - true when local login is enabled for everyone
# - true when admin override is enabled (super-admin only backend guard)
# - false only in pure SSO-only mode
def local_login_form_visible?
local_login_enabled? || local_admin_override_enabled?
end
# When password-related features (e.g., password reset link) should be
# visible. These are disabled whenever local login is turned off, even if
# an admin override is configured.
def password_features_enabled?
local_login_enabled?
end
# Backend check to determine if a given user is allowed to authenticate via
# local email/password credentials.
#
# - If local login is enabled, all users may authenticate locally (even if
# the email does not map to a user, preserving existing error semantics).
# - If local login is disabled but admin override is enabled, only
# super-admins may authenticate locally.
# - If both are disabled, local login is blocked for everyone.
def local_login_allowed_for?(user)
# When local login is globally enabled, everyone can attempt to log in
# and we fall back to invalid credentials for bad email/password combos.
return true if local_login_enabled?
# From here on, local login is disabled except for potential overrides.
return false unless user
return user.super_admin? if local_admin_override_enabled?
false
end
def jit_link_only?
Rails.configuration.x.auth.jit_mode.to_s == "link_only"
end
def allowed_oidc_domains
Rails.configuration.x.auth.allowed_oidc_domains || []
end
# Returns true if the given email is allowed for JIT SSO account creation
# under the configured domain restrictions.
#
# - If no domains are configured, all emails are allowed (current behavior).
# - If domains are configured and email is blank, we treat it as not
# allowed for creation to avoid silently creating accounts without a
# verifiable domain.
def allowed_oidc_domain?(email)
domains = allowed_oidc_domains
return true if domains.empty?
return false if email.blank?
domain = email.split("@").last.to_s.downcase
domains.map(&:downcase).include?(domain)
end
def sso_providers
if FeatureFlags.db_sso_providers?
# After boot, OmniAuth registers successfully configured providers into
# Rails.configuration.x.auth.sso_providers. Prefer that filtered list
# so we never render login buttons for providers that couldn't be
# registered (e.g., missing required fields in YAML fallback).
# Fall back to ProviderLoader for pre-boot contexts.
registered = Rails.configuration.x.auth.sso_providers
registered&.any? ? registered : ProviderLoader.load_providers
else
Rails.configuration.x.auth.sso_providers || []
end
end
end
end