ADR-031: External authentication: LDAP and OIDC next to the local password¶
Status: Accepted — design in external-auth design. Date: 2026-10-11
Builds on ADR-026 (ERP base path, host mode).
Problem¶
Staff already exist in a company directory (LLDAP, OpenLDAP, Active Directory) or an identity provider (Authentik, Google, Microsoft). Making them keep a second ERP password is a security and admin burden. But the ERP must never become unreachable because a directory or IdP is down or misconfigured, and a sign-in method must never be a way to take over another account.
Scope: the ERP staff login (/app/login) only. Website visitor accounts are unchanged. No SAML, no password change from the ERP, no background directory sync (roles are synced at login).
Decision¶
- One pipeline for every method. A method only authenticates and returns an
ExternalIdentity(Provider=ldaporoidc:<id>, stableSubject,Email,EmailVerified,DisplayName,Groups). The sharedProvisionerturns it into an ERP user in one transaction, thenIssueSessionForUser(the same code as a password login) mints the tokens. Roles, groups, permissions, refresh and logout are therefore identical for every method. - Identity links.
auth_identitylinks(provider, subject)to a user; a second unique index allows one link per provider per user. Provisioning order: the linked user; else an existing ERP user with the same email, only for a verified email, which is then linked; else a new user (locked local password, as for an admin-created user without one). Creation also requires a verified email — an unverified address must never create an account under someone else's email. A user already linked to another subject of the same provider is refused. - Roles follow the directory — for the roles it manages. Each method has its own rules: the
role_mappingrows whosemethodis empty (every method) or names it (ldap,oidc:<id>), plus its owndefault_role_id(ldap.default_role_id,oidc[].default_role_id). The roles those rows and that default name are the method's managed roles. At every login the user is granted the mapped roles of their groups (or the method's default role when no group matches), and loses the managed roles they no longer qualify for. Roles outside that set — assigned by hand — are never touched, so an administrator testing SSO on their own account keeps their admin role. Only live roles of the auth workspace are granted. The sign-in is refused (and rolled back) only when the user ends with no live role at all, or is soft-deleted/disabled. Example: mappingserp-staff → Staffanderp-admins → Admin; a user with a manualAccountingrole who is inerp-staffonly ends withStaff+Accounting(Admin removed if they had it). - LDAP (
POST /api/v1/auth/ldap/login {username, password}): service-account bind, search with the configured filter ({username}escaped per RFC 4515), exactly one entry required, then bind as that entry; the entry DN is the subject.ldaps://, orldap://+ StartTLS; plainldap://is refused outside loopback unless the admin ticks Allow an unencrypted connection (danger) (ldap.allow_plaintext). That opt-in exists because some directories (LLDAP) have no StartTLS and anldaps://certificate is not always available. With it on, the bind password and every user's password cross the network in clear, so it belongs only on a trusted network. The certificate ofldaps:/// StartTLS is verified against the system trust store and the URL's host name (an IP URL needs an IP SAN). LLDAP defaults are a preset, not a special case. Photo sync.ldap.photo_attr(the LLDAP defaults preset fillsjpegPhoto) names the entry's photo attribute, read as raw bytes. After each successful LDAP sign-in the photo becomes the user's avatar: a picture on the anchor(users, <user id>, avatar)with theusers.avatarflag set, replaced only when its bytes changed (the object key embeds their hash), cleared when the entry has no photo. Only JPEG/PNG up to 2 MB are taken; anything else is ignored with a warning. Sync runs only whenphoto_attris set (an empty attribute means "no sync", never "clear every avatar") and only with S3 configured. Its failures are logged and never block the sign-in. Avatars are served atGET /api/v1/avatars/:user(JWT, same tenant, staff only;Cache-Control: private, max-age=300; 404 when none). The anchor is reserved to this sync: the generic picture routes refuse to upload or delete it (403), so a holder ofpictures:pictures:writecan't replace a colleague's avatar. - OIDC — authorization code + PKCE (S256) + nonce, bouncing through the Next BFF; the ID token is verified by
go-oidc(signature,iss,aud,exp, nonce). Several providers can be configured. The redirect URI is built server-side from the settingerp_url(a bare https origin — plainhttp://is accepted for a loopback host only, for dev), never from a request header: register<erp_url>/api/auth/oidc/callbackat the IdP. A provider's optionalallowed_domainsadmits only identities whose verified email domain (after the last@, case-insensitive) is listed; anything else gets the generic 401 and a server-side log line. - Settings under
app_settingskeyauth.methods(site-wide slot of the auth workspace):defaultmethod,enabledalternatives,ldap(with itsdefault_role_id),oidc[](each with itsdefault_role_idandallowed_domains),role_mapping(group,role_id, optionalmethod),erp_url. A value stored before per-method defaults existed, with a top-leveldefault_role_id, is read as the LDAP default only — never an OIDC provider's — and dropped at the next save. LDAP needs a service account:bind_dnand a bind password are required. Secrets (LDAP bind password, OIDC client secrets) are AES-GCM encrypted under a key derived (HKDF) frommaster_keyand are write-only: the API only reportshas_*flags.GET|PUT /api/v1/settings/auth(settings:auth:read|write) andPOST …/auth/test(LDAP service bind, OIDC discovery). The auth workspace is the website tenant (website_tenant_id, else the database's single tenant). - Login screen. Anonymous
GET /api/v1/auth/methodslists the default and enabled alternatives (labels and kinds only, no URLs, no secrets). The default shows on top, alternatives below./app/login?method=localalways shows the local form, and a failing methods endpoint falls back to local, so a broken IdP can never lock administrators out.
sequenceDiagram
participant B as Browser
participant F as BFF (Next)
participant G as Go core
participant I as IdP
B->>F: GET /api/auth/oidc/:provider/start?next=
F->>F: state, nonce, PKCE verifier -> httpOnly 10 min cookie
F->>G: GET /auth/oidc/providers/:id/authorize (redirect_uri, state, nonce, challenge)
G-->>F: {url} (from the issuer's discovery document)
F-->>B: 302 to the IdP
B->>I: authenticate
I-->>B: 302 <erp_url>/api/auth/oidc/callback?code&state
B->>F: callback
F->>F: state must match the cookie
F->>G: POST /auth/oidc/providers/:id/exchange (code, verifier, nonce)
G->>I: token request, JWKS
G->>G: verify ID token, Provisioner, IssueSessionForUser
G-->>F: session tokens
F-->>B: session cookies, clear state cookie, 302 to next Consequences¶
- The directory/IdP is the source of truth for who belongs where; the ERP only caches it per login.
- Removing someone from the directory does not end their ERP access. Roles sync only at sign-in, and the refresh token slides for 7 days, so an open session keeps working (and keeps its roles) as long as it is refreshed at least once a week. To cut someone off, also disable their ERP user (soft delete): the next refresh is refused, and the current access token lives only until its short TTL. Known gap: external sessions have no absolute lifetime (forcing a fresh IdP sign-in every N hours) — a recommended follow-up.
- A user removed from a directory group loses the mapped role at their next sign-in.
settings:auth:writeis super-admin-equivalent. Whoever holds it can configure an IdP they control and a mapping to any role, then sign in as anyone (or as an admin). Grant it only to roles that already have full control.- Refused sign-ins are logged server-side (provider and subject, never secrets); clients only see the generic 401 "Invalid username or password.".
- Changing
master_keymakes stored secrets undecryptable: re-enter them.
Pitfalls¶
- LDAP requirement: users must not be able to write their own
mailattribute. A directory email is trusted as verified, so a user who could set an ERP administrator's address on their own entry would link to that account at their first LDAP login. Lock the attribute down in the directory ACLs. Likewise, OIDC links by email only when the IdP saysemail_verified. email_verifiedmust be a JSON boolean; the string"true"counts as unverified. An IdP that omits it (or sends a string) needs the provider's trust email switch for first sign-in.- Secrets are bound to their destination. An empty-secret save keeps the stored secret only while its destination is unchanged (LDAP
urlandbind_dn; OIDCissuerfor the same id). Otherwise the save is refused until the secret is re-entered — this stops an admin-level edit from exfiltrating a credential by pointing the config at another server. - A default role on a public IdP admits the whole internet. Google, Microsoft and similar providers sign in any account holder: a
default_role_id(or a mapping on a group name the user controls) on such a provider withoutallowed_domainslets every account of that IdP into the ERP. Setallowed_domainson every public provider, and scope mappings withmethodwhen group names could collide across providers. - The settings routes need the caller's workspace to equal the auth workspace (403) and answer 409 when there is none (several tenants and no
website_tenant_id). - LDAP: an empty password is refused before any bind (an anonymous bind must never succeed); 3+ matches and account-state bind errors give the same generic 401; a failing group search fails the login (500) instead of silently dropping roles. An unknown username (0 entries), 2+ matches or a size-limit result still costs one bind — the given password against the never-existing
cn=eerp-nonexistent-user,<base DN>— so response time does not reveal which usernames exist. - Stale links. Changing a saved OIDC provider's
issuersoft-deletes everyauth_identitylink ofoidc:<id>on save (the samesubat another issuer is another person); users relink by verified email at their next sign-in. The LDAP subject is the entry DN: a renamed DN leaves a stale link that refuses the login (the user's email is already linked to the old subject) until an admin removes it by hand:UPDATE auth_identity SET deleted_at = now() WHERE provider = 'ldap' AND subject = '<old DN>'; - Photos sync only at sign-in: a photo changed in the directory shows after the user's next LDAP login, and clearing
photo_attrleaves existing avatars in place. - Provisioning unique-violation races (two simultaneous first logins) end in the generic refusal; the user simply retries.
- Register the exact callback URL at each provider; in host mode (ADR-026)
erp_urlis the ERP host. - Optional live LDAP test: set
LDAP_TEST_URL,LDAP_TEST_BIND_DN,LDAP_TEST_BIND_PASSWORD,LDAP_TEST_BASE_DN,LDAP_TEST_USER,LDAP_TEST_PASSWORD(skipped otherwise; no LDAP container in compose).