feat: allow OIDC-only sign-in #66
No reviewers
Labels
No labels
bug
critical
ebook-branch
high
low
main
medium
release-blocker
security
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
jfro/fuzzy_catalog!66
Loading…
Reference in a new issue
No description provided.
Delete branch "jtk/oidc-only"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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|falseenv var (parsed likeREGISTRATION_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.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=falseis the way back in if OIDC breaks after turning the setting on.AdminSettings.local_login_enabled?/0is the one check;Accounts.oidc_configured?/0replaces the login template's private helper.When it's on
POST /users/log-in(password, magic link request, magic link token) andGET /users/log-in/:tokenredirect to the login page with an error and a warning log. Magic links issued earlier aren't consumed./users/registerredirects to the login page; accounts are created on first OIDC login as before.update_passwordis refused (email change stays).GET /auth/oidcmoved 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)
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.OIDC_ONLY=falserecovery hint). The per-request warning is gone.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.Review from Nye (changes requested) — PR #66 (fixes #52) at
18288d0Static review. Bypasses: none found. Reauth move: no session fixation. Lockout: one blocker.
Checked:
plug :require_local_loginruns before every UserSessionControllercreate/3clause (magic-link token, password, magic-link request) andconfirm/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_passwordrefused 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?/0only checks that config is present.Fix (with tests): (a) Preferred: when the IdP's
email_verifiedis 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"(ormax_age: 0) when current_scope is set.LOW 2, log spam:
local_login_enabled?/0logs 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 (approved) — PR #66 (fixes #52) re-review at
117d941Static 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.)a87a4e118c2a58db71ce