feat(auth): add OIDC/SSO login support (e.g. Authentik)

KE
1mo ago
+545-63

Summary

Adds OpenID Connect (OIDC) login as an alternative/companion to the existing local email+password auth, so ConvertX can be deployed behind an identity provider such as Authentik, Keycloak, Zitadel, or any standards-compliant OIDC issuer — useful for orgs that want centralized login/SSO instead of managing separate ConvertX accounts.

It uses the standard authorization-code flow with PKCE (via openid-client) and slots in alongside the existing session system: a successful OIDC login mints the exact same JWT cookie that password login does, so every existing authenticated route (history, conversions, account, etc.) works completely unchanged.

How it works

  • GET /login/oidc — starts the flow (PKCE verifier/state/nonce stored in a short-lived, scoped cookie) and redirects to the provider.
  • GET /login/oidc/callback — exchanges the code, validates state/nonce, reads the ID token claims (falling back to the userinfo endpoint if the email claim isn't in the ID token), then:
    • looks up the local user by OIDC sub,
    • auto-links to an existing local account with a matching email if one exists,
    • otherwise auto-provisions a new local user (with an unusable random password, since local password login stays disabled for that account).
  • The /login page shows a "Login with {OIDC_NAME}" button below the existing form (separated by a divider) whenever OIDC is configured.

Configuration

All new behavior is opt-in and controlled entirely by environment variables — nothing changes for existing deployments that don't set them.

NameDefaultDescription
OIDC_ISSUERThe provider's issuer URL, e.g. https://authentik.example.com/application/o/convertx/
OIDC_CLIENT_IDOAuth2/OIDC client ID
OIDC_CLIENT_SECRETOAuth2/OIDC client secret
OIDC_REDIRECT_URIPublic callback URL registered with the provider, e.g. https://convertx.example.com/login/oidc/callback
OIDC_SCOPESopenid profile emailScopes requested from the provider
OIDC_NAMESSOButton label, e.g. Authentik
OIDC_ONLYfalseHides local email/password login and registration entirely, so OIDC is the only way in

Setting OIDC_ISSUER, OIDC_CLIENT_ID, OIDC_CLIENT_SECRET, and OIDC_REDIRECT_URI together enables the button; nothing else needs to change.

Example (Authentik)

  1. Create an OAuth2/OpenID Provider in Authentik (confidential client), set its redirect URI to https://convertx.example.com/login/oidc/callback, and bind it to an Application.
  2. Set the env vars above using the client ID/secret from that provider and the provider's issuer URL.
  3. Restart ConvertX — the login page now shows a "Login with Authentik" button.

Full walkthrough and the env var table are added to the README.

Database

Adds a nullable oidc_sub column to the users table (schema version bumped to 2, with an automatic migration for existing installs — no action needed on upgrade).

Testing

  • tsc --noEmit / eslint — no new errors introduced (verified against the existing pre-existing warnings/errors baseline).
  • Built and ran the full Docker image locally against a real Authentik instance: verified OIDC discovery succeeds at startup, GET /login/oidc redirects to Authentik with correct client_id/PKCE challenge/state/nonce/redirect_uri, and a full login round-trip (including auto-provisioning the first user) works end-to-end.
  • Verified the OIDC_ONLY gate hides local login/registration and bypasses the first-run local setup page, and that the hybrid case (local form + SSO button, separated by a divider) renders correctly.

Notes

  • Local password login is unaffected when OIDC isn't configured.
  • When OIDC_ONLY is not set, existing local accounts keep working alongside SSO, and accounts are linked by email on first SSO login.

Summary by cubic

Add OpenID Connect (OIDC) SSO login alongside email/password using Authorization Code + PKCE via openid-client. It issues the same JWT cookie, auto-migrates the DB to v3, and adds a prod compose + .env.example for simpler config.

  • New Features

    • OIDC routes: GET /login/oidc and /login/oidc/callback.
    • Login page shows “Login with {OIDC_NAME}”; OIDC_ONLY=true hides the local form and Register links.
    • Account handling: match by (issuer, sub); auto-link by verified email; auto-provision on first SSO login.
    • Deployment: added .env.example and docker-compose.prod.yml; README documents OIDC setup and env vars.
  • Bug Fixes

    • Fix redirect_uri mismatches behind reverse proxies by building the callback URL from OIDC_REDIRECT_URI.
    • Make OIDC discovery non-blocking with auto-retry; only show the SSO button when discovery succeeds.
    • Hardened auth: safe cookie format/validation for the OIDC flow; link by verified email only; prevent duplicate first-logins with a unique (issuer, sub) identity and ON CONFLICT insert.
    • Correct TZ example in compose.yaml (Asia/Kolkata) to avoid runtime errors.