Skip to content

feat(identity): admin link-existing + unlink credential — fixes Ahmed + Pia (#291) - #3744

Merged
bokelley merged 3 commits into
mainfrom
bokelley/admin-link-unlink-cred
May 1, 2026
Merged

bokelley merged 3 commits into
mainfrom
bokelley/admin-link-unlink-cred

Conversation

@bokelley

@bokelley bokelley commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Three new admin endpoints + UI on `/admin/people` detail panel that close the gap exposed by Brian's first attempt to fix Ahmed: WorkOS rejected `createUser` for `[email protected]` with 400 BadRequest because the prior April merge's delete-secondary call had silently failed and the gmail WorkOS user is still alive (sign-in count 5, last sign-in Apr 27). The new "Link existing WorkOS user" path bypasses `createUser` entirely.

Also closes Pia's escalation #291 ("ability to unlink/remove a linked email") via the DELETE endpoint, and addresses #3719 (admin consolidate-existing flow).

What changes

`server/src/routes/admin/users.ts`

  • `GET /api/admin/users/:userId/credentials` — lists bindings for the host's identity, with email + name from local users.
  • `POST /api/admin/users/:userId/credentials` with `{ workos_user_id }` — binds an EXISTING WorkOS user under the host's identity. If the credential isn't in our local users table, fetches via `workos.userManagement.getUser` and upserts (Phase 1 trigger creates the singleton identity, mergeUsers re-points it). Idempotent on already-bound. Returns 400 on self-bind / malformed id, 404 on missing host or missing WorkOS user.
  • `DELETE /api/admin/users/:userId/credentials/:credentialId` — unbinds a non-primary credential and gives it a fresh singleton identity locally (the WorkOS user stays alive). Refuses on the primary credential. Audit row tagged `unbind_credential`. Calls `invalidateSessionsForUsers` so stale id-swap caches drop.
  • `POST /linked-emails` error handler now surfaces the real WorkOS message (including 400 BadRequest "Could not create user") and points at the new "Link existing" tool.

`server/public/admin-people.html` detail panel

  • Lists bound credentials (email + workos_user_id + primary badge) with per-row Remove buttons.
  • Two action buttons: "+ Add sign-in email" (existing) and "+ Link existing WorkOS user" (new).
  • Both UI flows require typing the host's primary email to confirm — same defense-in-depth as the bind tool.

How this fixes Ahmed

  1. WorkOS Dashboard → search `[email protected]` → grab `user_01KG39QA...`
  2. `/admin/people` → Ahmed → "+ Link existing WorkOS user"
  3. Paste the WorkOS user id, type `[email protected]` to confirm
  4. mergeUsers re-points the gmail WorkOS user under Ahmed's identity (any data on it moves to Ahmed; expected zero rows)
  5. Ahmed signs in with gmail magic-link → swap → Kyber1 workspace

How this fixes Pia

`/admin/people` → Pia → her credential row → Remove → confirm. Email is no longer associated with her account; the WorkOS user remains so she can use it for a fresh AAO account if desired.

Test plan

  • New: `admin-link-unlink-credential.test.ts` (13 tests) — happy path bind, fetch-on-missing, idempotent re-bind, self-bind 400, missing host 404, WorkOS 404, list, unbind + new singleton identity, audit row, primary-removal refusal, cross-host 404.
  • All identity tests pass: `identity-layer` (7), `merge-bind-not-delete` (6), `admin-bind-email` (7), `admin-link-unlink-credential` (13). 33/33 across the suite.
  • `tsc --noEmit` clean.

Step 2 (separate PR, deferred per Brian)

Rename `/admin/users` → `/admin/credentials` and re-cleave the admin nav so identity/credential management lives in one place and `/admin/people` becomes purely the relationship view. Tracked separately.

🤖 Generated with Claude Code

Three new admin operations on /api/admin/users/:userId/credentials:

  GET    /credentials               — list bindings for this user's identity
  POST   /credentials               — bind an EXISTING WorkOS user by id
  DELETE /credentials/:credentialId — unbind (creates fresh singleton identity)

POST bypasses createUser entirely, which fixes the case Brian hit on Ahmed:
WorkOS still had user_01KG39QAWFMHE22MVT3XB6289S alive at [email protected]
(the April merge's delete-secondary call had silently failed), so createUser
returned 400 "Could not create user." Workflow is now: find the existing
WorkOS user id in the WorkOS Dashboard, paste into "Link existing WorkOS user."

DELETE is the unbind half — non-destructive (WorkOS user stays alive). Closes
the Pia escalation (#291: "I want to remove a linked email"). Refuses on the
primary credential to keep the host signed-in.

UI on /admin/people detail panel now shows the bound credentials list with
per-row Remove buttons (non-primary only), plus a "+ Link existing WorkOS
user" action alongside "+ Add sign-in email."

Existing /linked-emails (create-and-bind) error handler now surfaces the
real WorkOS error message instead of a generic "502 upstream" — including
the 400-BadRequest-on-already-exists case — and points admins at the new
"Link existing" tool.

Closes #3719 (admin consolidate-existing path).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
csrfProtection: (_req: any, _res: any, next: any) => next(),
}));

import { initializeDatabase, closeDatabase, getPool } from '../../src/db/client.js';
bokelley and others added 2 commits May 1, 2026 08:57
Security review caught three improvements (no blockers):

M1 — silent consolidation foot-gun. POST /credentials would happily move
all of a credential's app-state (org memberships, working-group memberships,
certification work, community points) onto the host without distinguishing
"fresh credential" from "real account being absorbed." Now refuses with
409 + consolidate_confirmation_required when cred has app-state, requiring
{"consolidate": true} in the body to proceed. UI escalates the prompt and
makes admin type CONSOLIDATE in caps to confirm.

M2 — audit forensics. unbind_credential audit row now records
acting_workos_user_id (the actual auth credential, separate from the
post-id-swap canonical id) in details so admins with multiple bound
credentials are distinguishable. Issue #3717 still tracks the broader
audit-log overhaul; this is the local fix.

L1 — unbind UI confirmation now requires typing the credential's email
exactly, matching the strength of the link confirmation. Defends against
admin accidentally removing the wrong credential.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Semgrep flagged the (err as Error).message echo in the link/unlink error
paths — defense-in-depth concern noted in the security review (L2).
Removed; the full error is still logged via logger.error.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit e810d9a into main May 1, 2026
13 checks passed
@bokelley
bokelley deleted the bokelley/admin-link-unlink-cred branch May 1, 2026 13:07
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.

1 participant