Repository navigation
feat(auth): staff accounts that can impersonate any user - #415
Conversation
users.is_staff hides an account from every user-facing query; the two impersonation tables record each staff switch-in and every write made while switched in. No FKs on the audit tables so the trail outlives the users it names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every pool but donors' hardcoded 5432, so a suite could only ever run against the shared dev database. Reading DB_PORT (default unchanged) lets a run point at a throwaway Postgres instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A hidden staff account (users.is_staff) holds no rights of its own: the RBAC gate denies it everything but impersonation:*. It starts a session against any non-staff user, then sends X-Impersonate-Session beside its own Bearer token. authenticateRequest swaps in the target's identity and memberships, so policy, scoping and entered_by all see only the target. - read_only sessions get a 403 on every non-GET; full sessions act as the target. Every non-GET impersonated request, refused or not, is recorded in branch.impersonation_actions. - MFA routes are blocked while impersonating: they act on the caller's own Cognito session, which is then the staff account's. - Staff are filtered from every user list, count, roster, picker and member validation, and the column is never returned. A staff id answers like a missing one. - make create-staff provisions one (AdminCreateUser + createStaffUser); no route can set is_staff. - X-Impersonate-Session added to CORS in the lambdas and API Gateway. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This PR contains a database migration(no newly added migration files in this PR) It will be applied to the production database automatically when this PR merges, before the new lambda code is deployed. Please confirm before requesting review:
This PR also changes 66 files outside |
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
1 similar comment
|
Database Types Check Complete The database schema files were modified, but the regenerated TypeScript types are identical to the existing ones. No changes were needed and the type definitions are already up to date. |
RbacSubject.isStaff is required as of this PR; the frontend fixture builder failed typecheck without it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Terraform Plan 📖
|
Part 2 of 3, stacked on the schema PR. It needs that PR's migrations live before it deploys.
How it works
A staff account keeps its own Cognito token.
POST /auth/impersonation {targetUserId, mode}creates a server-side session, and the browser then sendsX-Impersonate-Session: <id>alongside the Bearer token.authenticateRequestchecks four things:is_staffIf all four hold, it returns the target's identity and memberships, with
user.impersonatornaming the staff account. The RBAC subject, list scoping,/auth/meandentered_bytherefore all see only the target, with no per-lambda code.RbacSubject.isStaff. One gate inauthorizeAnydenies a staff subject everything exceptimpersonation:console/impersonation:start. While switched in, the subject is the target's, so a switch can't be chained.impersonation: 'blocked'(the MFA routes, because they act on the caller's own Cognito session, which is the staff account's) refuse impersonated requests in both modesonImpersonatedWrite, which calls@branch/store'srecordImpersonationAction/users(the list and its count),GET/PATCH/DELETE /users/:id(a staff id gets the same 404 as a missing one),assignable-staff, admin rosters and headcounts, and project member validationGET /auth/impersonation/targets?search=,POST /auth/impersonation,DELETE /auth/impersonation/:sessionId./auth/menow also returnsisStaffandimpersonation.make create-staff EMAIL=… NAME=…(inapps/backend) runsAdminCreateUserand thencreateStaffUser. No route can setis_staff.X-Impersonate-Sessionis added to thejson()helper and toinfrastructure/{aws,preview}/api_gateway.tf. API Gateway answers preflights itself, so the terraform apply must land before the frontend PR ships. Existing preview stacks only pick this up after the label is re-added.Also adds
test(lambdas): read DB_PORT in every e2e fixture poolas its own commit. The default is unchanged. It's what let the e2e suites run against a throwaway Postgres instead of the shared dev DB.Verification
I ran these locally, against a throwaway Postgres 16 built from the migrations:
fetchlocalhost:3000, and another process held that port on my machine. CI runs its own dev-server there, so they should pass in CI.auth/test/impersonation.e2e.test.tscovers the whole flow through the realauthenticateRequestand dispatch: staff sees only the console, a staff target gets 404, read-only refuses writes and is audited, ending a session gives 401IMPERSONATION_EXPIRED, no chaining, MFA is blocked, another staff account's session can't be used or ended, and a non-staff caller sending the header gets 403.Not done
MFA_SETUPchallenge. Staff can enrol on/profile. Enforcing it needs a separate design, probably a Cognito pre-auth check or making MFA required pool-wide.🤖 Generated with Claude Code