fix: revoke api tokens and mcp oauth grants on leave_instance
Summary
POST /users/leave_instance deleted the caller's password row and drafts but kept every token row for that email. After leaving, those API tokens still authenticated (GET /users/whoami) and could mint sessions through GET /users/refresh_token. If the address was later re-created, they would authenticate as the new account. delete_user already revokes tokens and MCP OAuth grants. This PR makes leave_instance do the same.
Fixes WIN-2800.
Changes
leave_instance(backend/windmill-api-users/src/users.rs) now callsrevoke_mcp_oauth_grants(.., None)and runsDELETE FROM token WHERE email = $1inside its existing transaction, before it deletes thepasswordrow. The order matchesdelete_user: the revocation takes thepasswordrow lock first, asdocs/auth-surface.mdrequires of any path that revokes.- No new SQL, so the sqlx offline cache needs no update. The token delete is the same query text
delete_useralready uses. - Updated the comment in
remote_deploy.rsand two paragraphs ofdocs/auth-surface.mdthat describedleave_instanceas leaving its tokens.
Test plan
-
backend/tests/leave_instance_tokens.rs: afterleave_instance, the account has notokenrows, a second token of that account ā 401, and another user's token still works. Confirmed it fails with the fix reverted. - On the running dev backend: planted a non-superadmin
passwordrow, logged in, created an API token (whoamiā 200), calledleave_instance. Afterwards the account had 0tokenrows and 0passwordrows, and bothwhoamiandrefresh_tokenwith the API token ā 401. - A token that was used just before leaving keeps answering from
AUTH_CACHEuntil the TTL runs out. This is the existing documented behaviour for non-session labels, anddelete_userbehaves the same way, so this PR does not change it. - Not exercised: a live MCP OAuth grant, because the dev backend is built without
mcp. The grant revocation goes through the sharedrevoke_mcp_oauth_grantshelper thatdelete_useralready uses.
Left in draft: this touches an auth/token path and should get a human look before ready.
š¤ Generated with Claude Code
Summary by cubic
Fixes leave_instance so it revokes API tokens and MCP OAuth grants, matching delete_user behavior. Previously, leaving an instance left token rows active, so they kept authenticating (whoami, refresh_token) and would authenticate as a re-created account with the same email.
leave_instancenow callsrevoke_mcp_oauth_grantsand deletes alltokenrows for the email inside its existing transaction, before deleting thepasswordrow.- Adds a test pinning that leaving the instance removes the account's tokens and invalidates its API token.
- Updates
docs/auth-surface.mdand a comment inremote_deploy.rsto reflect the new behavior.