feat: allow OIDC-only sign-in #66

Merged
jfro merged 4 commits from jtk/oidc-only into main 2026-10-18 22:48:45 +00:00
Owner

Adds OIDC-only sign-in: password and magic link login, and local registration, can be turned off so everyone signs in through OIDC.

Setting

  • OIDC_ONLY=true|false env var (parsed like REGISTRATION_ENABLED; anything else raises at boot). When unset, a new toggle on Admin → Users decides (default off). The toggle is locked when the env var is set.
  • It only applies while OIDC is configured (OIDC_CLIENT_ID, OIDC_CLIENT_SECRET, OIDC_BASE_URL), so a missing OIDC config can't lock everyone out: local login stays on and a warning is logged once at boot. The admin toggle can't be turned on without OIDC configured. OIDC_ONLY=false is the way back in if OIDC breaks after turning the setting on.
  • AdminSettings.local_login_enabled?/0 is the one check; Accounts.oidc_configured?/0 replaces the login template's private helper.

When it's on

  • Login page shows only "Sign in with OIDC" (no email/password or magic link forms, no Register link in the page or navbar).
  • POST /users/log-in (password, magic link request, magic link token) and GET /users/log-in/:token redirect to the login page with an error and a warning log. Magic links issued earlier aren't consumed.
  • /users/register redirects to the login page; accounts are created on first OIDC login as before.
  • Settings hide the password form, and update_password is refused (email change stays).
  • GET /auth/oidc moved out of the redirect-if-signed-in scope, so a signed-in user can re-authenticate through OIDC for sudo mode (settings). Before this, OIDC users had no way back into settings once sudo mode lapsed.

Lockout guard (from Nye's review)

  • Existing password/magic-link accounts are linked to the OIDC identity on their first OIDC sign-in when the provider says email_verified: true (provider + sub saved on the account, logged with the user id). Unverified emails are still refused. ⚠️ This changes the rule from #37, which never linked local accounts; it's what lets existing users and admins keep their accounts under OIDC-only.
  • The Admin → Users toggle won't turn OIDC-only on unless the admin flipping it has an OIDC identity linked (hint shown, button disabled, refused server-side too).
  • At boot, a one-time check warns when OIDC-only is on but OIDC isn't configured, or when no active admin has OIDC linked (with the OIDC_ONLY=false recovery hint). The per-request warning is gone.
  • Re-authenticating while signed in sends prompt=login, and if the IdP comes back as a different user the switch is refused (flash + log with both user ids) instead of silently signing the browser in as them.

Documented in .env.sample (including "Locked out? Set OIDC_ONLY=false and restart"). Full OIDC round-trip tests (token endpoint stubbed with Req.Test, HS256 ID token) cover verified linking, unverified refusal, a local admin's settings reauth, and the refused account switch. Tests cover the setting precedence, the unconfigured fallback, each refused path, the settings page, re-auth via OIDC, and the admin toggle (on/off, env-locked, unconfigured).

Fixes #52

Policy change vs #37: a local account is now linked to an OIDC login when the provider marks the email as verified (email_verified), whether or not OIDC-only is on. This reverses #37's "a local account is never linked" rule; it trusts the IdP's verified email, and it's what lets an existing local admin move to OIDC-only without being locked out.

Adds OIDC-only sign-in: password and magic link login, and local registration, can be turned off so everyone signs in through OIDC. **Setting** - `OIDC_ONLY=true|false` env var (parsed like `REGISTRATION_ENABLED`; anything else raises at boot). When unset, a new toggle on Admin → Users decides (default off). The toggle is locked when the env var is set. - It only applies while OIDC is configured (`OIDC_CLIENT_ID`, `OIDC_CLIENT_SECRET`, `OIDC_BASE_URL`), so a missing OIDC config can't lock everyone out: local login stays on and a warning is logged once at boot. The admin toggle can't be turned on without OIDC configured. `OIDC_ONLY=false` is the way back in if OIDC breaks after turning the setting on. - `AdminSettings.local_login_enabled?/0` is the one check; `Accounts.oidc_configured?/0` replaces the login template's private helper. **When it's on** - Login page shows only "Sign in with OIDC" (no email/password or magic link forms, no Register link in the page or navbar). - `POST /users/log-in` (password, magic link request, magic link token) and `GET /users/log-in/:token` redirect to the login page with an error and a warning log. Magic links issued earlier aren't consumed. - `/users/register` redirects to the login page; accounts are created on first OIDC login as before. - Settings hide the password form, and `update_password` is refused (email change stays). - `GET /auth/oidc` moved out of the redirect-if-signed-in scope, so a signed-in user can re-authenticate through OIDC for sudo mode (settings). Before this, OIDC users had no way back into settings once sudo mode lapsed. **Lockout guard (from Nye's review)** - Existing password/magic-link accounts are **linked** to the OIDC identity on their first OIDC sign-in when the provider says `email_verified: true` (provider + sub saved on the account, logged with the user id). Unverified emails are still refused. ⚠️ This changes the rule from #37, which never linked local accounts; it's what lets existing users and admins keep their accounts under OIDC-only. - The Admin → Users toggle won't turn OIDC-only on unless the admin flipping it has an OIDC identity linked (hint shown, button disabled, refused server-side too). - At boot, a one-time check warns when OIDC-only is on but OIDC isn't configured, or when no active admin has OIDC linked (with the `OIDC_ONLY=false` recovery hint). The per-request warning is gone. - Re-authenticating while signed in sends `prompt=login`, and if the IdP comes back as a different user the switch is refused (flash + log with both user ids) instead of silently signing the browser in as them. Documented in `.env.sample` (including "Locked out? Set OIDC_ONLY=false and restart"). Full OIDC round-trip tests (token endpoint stubbed with Req.Test, HS256 ID token) cover verified linking, unverified refusal, a local admin's settings reauth, and the refused account switch. Tests cover the setting precedence, the unconfigured fallback, each refused path, the settings page, re-auth via OIDC, and the admin toggle (on/off, env-locked, unconfigured). Fixes #52 **Policy change vs #37:** a local account is now linked to an OIDC login when the provider marks the email as verified (`email_verified`), whether or not OIDC-only is on. This reverses #37's "a local account is never linked" rule; it trusts the IdP's verified email, and it's what lets an existing local admin move to OIDC-only without being locked out.
jfro added this to the 0.6.2 milestone 2026-10-18 22:03:52 +00:00
feat: allow OIDC-only sign-in
All checks were successful
CI / test (pull_request) Successful in 1m45s
18288d0744
Author
Owner

Review from Nye (changes requested) — PR #66 (fixes #52) at 18288d0

Static review. Bypasses: none found. Reauth move: no session fixation. Lockout: one blocker.

Checked: plug :require_local_login runs before every UserSessionController create/3 clause (magic-link token, password, magic-link request) and confirm/2, so all POST /users/log-in shapes and GET /users/log-in/:token are refused, including links sent before the switch. Registration new/create refused. update_password refused server-side, not just hidden. No other local-auth entry points. Env parsing and env-over-setting precedence are right. The toggle can't be turned on without OIDC configured, and the env lock is enforced in handle_event, not only in the disabled attribute. Logger context is good.

Reauth move: fine on fixation. log_in_user → create_or_extend_session → renew_session clears and renews the session whenever the OIDC user differs from current_scope.user. State/nonce/PKCE still come only from our session. The new token's authenticated_at is now, so sudo mode works after an OIDC reauth.

BLOCKER: turning OIDC-only on locks out every existing local account, including the admin who turns it on. In Accounts.find_or_create_oidc_user/2, when no user matches the sub, a user found by email with provider nil falls to {:error, :email_taken}. Results: (1) right after a local admin flips the toggle, they can't reauth for /users/settings. (2) When session/remember-me tokens expire, every pre-existing local user is locked out until OIDC_ONLY=false is set and the app restarts. oidc_configured?/0 only checks that config is present.
Fix (with tests): (a) Preferred: when the IdP's email_verified is true, link a local account (provider nil) to the OIDC identity on first OIDC login and log it; keep refusing unverified emails. (b) Or refuse the toggle while any active admin has no OIDC link.
Tests: a local user with a verified OIDC email can sign in under OIDC-only; an unverified one is still refused; a local admin can still reauth for settings after the toggle.

LOW 1, silent account switch on reauth: if A is signed in and the IdP session is B, reauth signs the browser in as B and sends them to A's return_to. Suggest refusing (flash, log both ids) or logging out first, and prompt: "login" (or max_age: 0) when current_scope is set.
LOW 2, log spam: local_login_enabled?/0 logs a warning on every call when OIDC-only is on but OIDC isn't configured. Log once instead.
NIT: in .env.sample, document recovery: "Locked out? Set OIDC_ONLY=false and restart."

Approve once the blocker is fixed; the lows can be follow-ups.

Review from Nye (changes requested) — PR #66 (fixes #52) at 18288d0 Static review. Bypasses: none found. Reauth move: no session fixation. Lockout: one blocker. **Checked:** `plug :require_local_login` runs before every UserSessionController `create/3` clause (magic-link token, password, magic-link request) and `confirm/2`, so all POST /users/log-in shapes and GET /users/log-in/:token are refused, including links sent before the switch. Registration new/create refused. `update_password` refused server-side, not just hidden. No other local-auth entry points. Env parsing and env-over-setting precedence are right. The toggle can't be turned on without OIDC configured, and the env lock is enforced in handle_event, not only in the disabled attribute. Logger context is good. **Reauth move:** fine on fixation. log_in_user → create_or_extend_session → renew_session clears and renews the session whenever the OIDC user differs from current_scope.user. State/nonce/PKCE still come only from our session. The new token's authenticated_at is now, so sudo mode works after an OIDC reauth. **BLOCKER:** turning OIDC-only on locks out every existing local account, including the admin who turns it on. In `Accounts.find_or_create_oidc_user/2`, when no user matches the sub, a user found by email with provider nil falls to `{:error, :email_taken}`. Results: (1) right after a local admin flips the toggle, they can't reauth for /users/settings. (2) When session/remember-me tokens expire, every pre-existing local user is locked out until OIDC_ONLY=false is set and the app restarts. `oidc_configured?/0` only checks that config is present. Fix (with tests): (a) Preferred: when the IdP's `email_verified` is true, link a local account (provider nil) to the OIDC identity on first OIDC login and log it; keep refusing unverified emails. (b) Or refuse the toggle while any active admin has no OIDC link. Tests: a local user with a verified OIDC email can sign in under OIDC-only; an unverified one is still refused; a local admin can still reauth for settings after the toggle. **LOW 1, silent account switch on reauth:** if A is signed in and the IdP session is B, reauth signs the browser in as B and sends them to A's return_to. Suggest refusing (flash, log both ids) or logging out first, and `prompt: "login"` (or `max_age: 0`) when current_scope is set. **LOW 2, log spam:** `local_login_enabled?/0` logs a warning on every call when OIDC-only is on but OIDC isn't configured. Log once instead. **NIT:** in .env.sample, document recovery: "Locked out? Set OIDC_ONLY=false and restart." Approve once the blocker is fixed; the lows can be follow-ups.
fix: link verified OIDC emails to local accounts so OIDC-only can't lock users out
All checks were successful
CI / test (pull_request) Successful in 1m47s
117d941731
style: theme classes on the OIDC-only toggle (matches #16)
All checks were successful
CI / test (pull_request) Successful in 2m1s
e59d26841c
Author
Owner

Review from Nye (approved) — PR #66 (fixes #52) re-review at 117d941

Static review (no Elixir runtime here). The blocker is fixed; the two lows are handled too.

Blocker (lockout), fixed: find_or_create_oidc_user now has a %User{provider: nil} clause. On email_verified true/"true" it links via link_oidc_identity/4 -> User.oidc_link_changeset (casts provider/provider_uid/provider_token, required + unique_constraint backed by the existing unique_index(:users, [:provider, :provider_uid])); unverified emails still return :email_taken with a warning. The password hash and role stay, so a local admin keeps admin after linking. Disabled local users are linked but still refused by the Accounts.active? check in the callback. The admin toggle refuses to turn on unless the acting admin has OIDC linked (re-read from the DB, so linking in another tab counts), and the button is disabled with a hint. The OIDC_ONLY env path isn't guarded, but the boot check warns when no active admin is linked and .env.sample has the recovery note. Nothing else used oidc_user?, so linked local users don't lose any password/settings UI.

Lows, fixed: authorize adds prompt=login for signed-in users (Keyword.update on authorization_params, which runtime.exs always sets). The callback refuses when the OIDC user differs from current_scope.user and logs both ids. The per-call warning is gone; log_oidc_only_status/0 runs once at boot as a temporary Task after Repo, rescues DB errors, and is off in test. Tests cover a verified link, an unverified refusal, the local admin reauth path, the no-switch case, the toggle guard and the boot check.

LOW (follow-up, not blocking): the "different account" check runs after find_or_create_oidc_user, so its side effects already happened. If A is signed in and the IdP returns B, B's account may already have been created, or a local account with B's verified email linked, before the refusal. That's harmless under the new linking policy, but you could look up get_user_by_provider first when current_scope is set and refuse before creating or linking anything.
NOTE (policy): local accounts are now linked on a verified email whether or not OIDC-only is on. That reverses #37's "a local account is never linked". It's the standard trade-off (you trust the IdP's email_verified), and it's what makes OIDC-only safe, so it's worth a line in the PR body/changelog.

Approved. Merge once CI is green on 117d941.

(Delta: Nye also checked e59d268, the theme classes on the toggle, and confirmed it's fine.)

Review from Nye (approved) — PR #66 (fixes #52) re-review at 117d941 Static review (no Elixir runtime here). The blocker is fixed; the two lows are handled too. Blocker (lockout), fixed: find_or_create_oidc_user now has a `%User{provider: nil}` clause. On email_verified true/"true" it links via link_oidc_identity/4 -> User.oidc_link_changeset (casts provider/provider_uid/provider_token, required + unique_constraint backed by the existing unique_index(:users, [:provider, :provider_uid])); unverified emails still return :email_taken with a warning. The password hash and role stay, so a local admin keeps admin after linking. Disabled local users are linked but still refused by the Accounts.active? check in the callback. The admin toggle refuses to turn on unless the acting admin has OIDC linked (re-read from the DB, so linking in another tab counts), and the button is disabled with a hint. The OIDC_ONLY env path isn't guarded, but the boot check warns when no active admin is linked and .env.sample has the recovery note. Nothing else used oidc_user?, so linked local users don't lose any password/settings UI. Lows, fixed: authorize adds prompt=login for signed-in users (Keyword.update on authorization_params, which runtime.exs always sets). The callback refuses when the OIDC user differs from current_scope.user and logs both ids. The per-call warning is gone; log_oidc_only_status/0 runs once at boot as a temporary Task after Repo, rescues DB errors, and is off in test. Tests cover a verified link, an unverified refusal, the local admin reauth path, the no-switch case, the toggle guard and the boot check. LOW (follow-up, not blocking): the "different account" check runs after find_or_create_oidc_user, so its side effects already happened. If A is signed in and the IdP returns B, B's account may already have been created, or a local account with B's verified email linked, before the refusal. That's harmless under the new linking policy, but you could look up get_user_by_provider first when current_scope is set and refuse before creating or linking anything. NOTE (policy): local accounts are now linked on a verified email whether or not OIDC-only is on. That reverses #37's "a local account is never linked". It's the standard trade-off (you trust the IdP's email_verified), and it's what makes OIDC-only safe, so it's worth a line in the PR body/changelog. Approved. Merge once CI is green on 117d941. (Delta: Nye also checked e59d268, the theme classes on the toggle, and confirmed it's fine.)
Merge remote-tracking branch 'origin/main' into jtk/oidc-only
Some checks failed
CI / test (pull_request) Failing after 36s
a87a4e118c
# Conflicts:
#	lib/fuzzy_catalog_web/live/admin_users_live.ex
jfro force-pushed jtk/oidc-only from a87a4e118c
Some checks failed
CI / test (pull_request) Failing after 36s
to 2a58db71ce
All checks were successful
CI / test (pull_request) Successful in 46s
2026-10-18 22:43:00 +00:00
Compare
jfro merged commit 2688fff804 into main 2026-10-18 22:48:45 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/fuzzy_catalog!66
No description provided.