Skip to content

feat: link existing SimpleAuth users to their AD account (keep GUID and roles) - #81

Open
alghanim wants to merge 8 commits into
bodaay:masterfrom
alghanim:feat/link-local-users-to-directory
Open

alghanim wants to merge 8 commits into
bodaay:masterfrom
alghanim:feat/link-local-users-to-directory

Conversation

@alghanim

@alghanim alghanim commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A deployment that created its users in SimpleAuth first (a local username and a SimpleAuth password) and configured AD later cannot move those users to AD without losing their roles:

  • authenticateUser tries the SimpleAuth password, then AD. On AD success it finds the user by the ldap:<typed name> mapping only.
  • A user created in SimpleAuth has only local:<name>. Their first AD sign-in therefore JIT-provisions a new user, and SetIdentityMapping re-points local:<name> to it.
  • The original user, with its roles, permissions and app assignments, is left with no username at all. Its SimpleAuth password stops working.

I reproduced this against a Samba AD DC: the user signed in with the AD password and got a new GUID with no roles. The original user lost its local mapping, and its password then got 401.

The same split happens to any AD user who types their name in another letter case (JDOE for jdoe). AD matches names in any case, but mapping keys are case-sensitive. JIT then re-points both ldap: and local:<AD spelling> away from the existing user.

Change

API: POST /api/admin/ldap/link-users

Admin key. Takes guids (1..1000) or all: true, plus dry_run, remove_local_password and confirmed.

For each user with a SimpleAuth username and no AD link:

  • How it finds the AD account: the sign-in's own search, i.e. custom_filter if set, else the username attribute. It is limited to (objectClass=person), so a group named like a user never matches. One directory connection serves the whole request.
  • When it links: exactly one enabled entry matches, and no other user holds the AD account or AD's spelling of the name. It then writes ldap:<name> on the existing user, plus ldap:<AD spelling> when they differ, and syncs the profile, groups and sAMAccountName exactly as an AD sign-in does.
  • What is kept: the GUID, roles, permissions and app assignments.
  • The same name is not proof of the same person. When both sides have an email and the two differ, the result is email_differs and nothing changes, unless the GUID is also in confirmed. Results show both identities (display_name/email and directory_display_name/directory_email). The audit record keeps the previous display name and email, plus the AD DN.
  • Every other case is reported and left untouched: not_in_directory, disabled_in_directory, conflict (with conflict_guid), ambiguous, already_linked, skipped_app_local, skipped_merged, no_local_username, not_found. It never re-points a mapping another user holds.
  • spelling_differs flags names that AD spells differently. After linking, the samaccountname claim carries AD's spelling.
  • SimpleAuth password: kept by default. SA-7 still refuses it when the AD account is disabled, and once the user is linked it falls under directory_outage_policy.
    • remove_local_password: true removes it, either at link time or later: with the flag set, already-linked users that still have a password are in scope.
  • Errors stop the run and say why. The directory gives 502, a store failure 500, and a request that ended early (client gone, write timeout) 503. Each response includes the results so far. Store reads tell "absent" apart from "failed" through a new store.ErrNotFound, in both backends; existing messages are unchanged.
  • Stale writes are avoided: each write starts from a fresh read of the user, so a Disable or lock applied during the lookup is kept.

Sign-in fixes (authenticateUser)

  • After AD accepts the password, the user is looked up by ldap:<typed>, then by ldap:<AD spelling>, before anyone is created. So JDOE signs in to jdoe.
  • A JIT-provisioned user takes only mapping names nobody holds. A first AD sign-in no longer takes the username, and with it the only way in, from an existing SimpleAuth user.
  • A store error during the lookup now fails the sign-in instead of provisioning a duplicate.

Console: Users page

  • A checkbox column, with a floating bar for the ticked users: Link selected to AD.
  • Link to AD on each row and in the user's details.
  • Link all to AD in the header.
  • Remove SimpleAuth Password in a linked user's details.

Every action opens one dialog:

  • It shows the dry run user by user, with the SimpleAuth user and the AD account side by side.
  • A row with a different email can be linked only after its own Same person: link box is ticked.
  • The "Also remove their SimpleAuth password" box re-runs the dry run.
  • Confirming applies exactly the previewed users, in batches of 100.
  • If a run stops, the results returned so far are still shown.

Supporting changes

  • auth: a shared signInFilter (used by LDAPAuthenticate) and linkFilter; LDAPUserFinder (one connection, a size limit of 2, and ErrLDAPAmbiguous).
  • Test seams: h.ldapAuthenticate, h.ldapSearchUser (used by directoryAccountDisabled) and h.ldapUserFinder.
  • Docs:
    • docs/API.md: the endpoint;
    • docs/ACTIVE-DIRECTORY.md: "Moving Existing SimpleAuth Users to AD", including how to clean up duplicates made earlier;
    • CHANGELOG.md: Unreleased.

Tests

  • internal/handler/ldap_link_test.go (20 tests). Each fix from the independent review was first written as a failing test.
    • The main case: link a local user that has a role, then sign in with the AD password → same GUID and roles, and no new user is created.
    • Sign-in typed in another case (JDOE/JDoe): lands on the linked user, mappings intact. Before the fix, this created a new user and stranded the linked one.
    • First AD sign-in of an unlinked user: the existing user keeps its username and its password.
    • Link behaviour:
      • dry run writes nothing;
      • password removal works both at link time and later;
      • AD's spelling;
      • every status that changes nothing (mappings compared before and after);
      • conflict on AD's spelling;
      • all scoping;
      • email difference → email_differs without confirmation, linked with it, and the audit record keeps the old profile.
    • Connections: one directory connection per run, and none when there is nothing to look up.
    • Error handling:
      • an ambiguous name;
      • a failed directory connection or search → 502;
      • store failures in GetUser or ResolveMapping → 500, nothing linked;
      • a cancelled request stops at once;
      • an admin's Disable made during the lookup is kept.
  • internal/auth/ldap_filter_test.go: sign-in and link filters (default, uid, custom_filter, escaping).
  • internal/store/notfound_test.go: ErrNotFound from GetUser and ResolveMapping, messages unchanged.
  • Checks: go vet ./... and gofmt are clean. go test -race ./...: auth, handler, migrate, store all ok (Go 1.26.8)
  • Lab (a Samba AD DC and this branch behind nginx/TLS). The console was checked in Chrome, in light and dark:
    • the row, ticked and all actions;
    • AD sign-in lands on the original users with their roles;
    • sign-ins as JDOE/jSmItH land on the linked users, with no new users;
    • an AD group named like a local user is not matched;
    • two different people both named ahmed (different emails) are held back until confirmed, and the audit keeps the previous profile;
    • after "remove passwords later", the old passwords get 401.

Not in this PR (noticed while working)

  • Linking and a concurrent first AD sign-in for the same user are not one atomic step: the mapping write is a check and then a set. Atomicity would need a set-if-absent store call.
  • GET /api/admin/users returns each user's password_hash (bcrypt) to the console.
  • docs/ACTIVE-DIRECTORY.md "User Identity" says AD users are mapped as ldap:{objectGUID}, but the code maps the username.

🤖 Generated with Claude Code

https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP

Your Name and others added 7 commits November 12, 2026 20:03
POST /api/admin/ldap/link-users (admin key): guids or all, dry_run,
remove_local_password. A user with only a local mapping got a second,
JIT-provisioned user at their first AD sign-in, and local:<name> moved
to it; the original user and its roles were left behind. Linking writes
ldap:<name> (and ldap:<AD spelling>) on the existing user, after
checking that exactly one enabled AD account matches and no other user
holds it, and syncs the profile as an AD sign-in does. Every other case
is reported and left untouched; a directory error stops the run (502).
The SimpleAuth password stays unless removed, now or later.

The sign-in path, directoryAccountDisabled and the link path call the
directory through h.ldapSearchUser / h.ldapAuthenticate so tests can
stand in for AD.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Per row (Link to AD), for ticked users (floating bar: Link selected to
AD), for everyone (Link all to AD), and Remove SimpleAuth Password in a
linked user's details. One dialog shows the dry run per user, re-runs it
when the remove-password box changes, and applies exactly the previewed
users in batches of 1000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
GetUser and ResolveMapping return an error matching store.ErrNotFound
(errors.Is) when the user or mapping does not exist, in both backends;
their messages are unchanged. Callers can now treat any other error as a
store failure instead of as "absent".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Sign-in (authenticateUser):
- after AD accepts the password, look the user up by ldap:<typed>, then
  by ldap:<AD spelling>, before provisioning anyone: a name typed in
  another case (JDOE for jdoe) created a second user and re-pointed the
  first user's mappings to it;
- a JIT-provisioned user takes only mapping names nobody holds, so a
  first AD sign-in no longer takes an existing user's username;
- a store error during the lookup fails the sign-in instead of
  provisioning a duplicate.

Link (review fixes):
- find the AD account with the sign-in's own filter (custom_filter, else
  username_attr), person entries only, exactly one match (auth.linkFilter,
  LDAPUserFinder, ErrLDAPAmbiguous), over one connection per request;
- when both sides have an email and they differ, link only GUIDs listed
  in "confirmed" (status email_differs); results show both identities and
  the audit record keeps the previous profile and the AD DN;
- store failures stop the run (500) instead of reading as absent or free;
  a request that ended stops it (503);
- writes start from a fresh read of the user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Rows show the SimpleAuth user and the AD account side by side; a row
with a different email in AD needs its own "Same person: link" tick.
Only the latest preview's answer is shown; a stopped run's results are
kept; the header checkbox leaves ticked users hidden by the search
alone; apply sends batches of 100.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Comment thread internal/handler/auth.go Fixed
Comment thread internal/handler/auth.go Fixed
Comment thread internal/handler/auth.go Fixed
Log the store's GUID instead of the request's, labels instead of the
request's flags and the password field, and names with line breaks
removed (logSafe). gofmt on the new test file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014xHaYdBuvR8tJcURffAjtP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants