fix: revoke api tokens and mcp oauth grants on leave_instance

Merged
RU
6d ago
+61-13

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 calls revoke_mcp_oauth_grants(.., None) and runs DELETE FROM token WHERE email = $1 inside its existing transaction, before it deletes the password row. The order matches delete_user: the revocation takes the password row lock first, as docs/auth-surface.md requires 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_user already uses.
  • Updated the comment in remote_deploy.rs and two paragraphs of docs/auth-surface.md that described leave_instance as leaving its tokens.

Test plan

  • backend/tests/leave_instance_tokens.rs: after leave_instance, the account has no token rows, 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 password row, logged in, created an API token (whoami → 200), called leave_instance. Afterwards the account had 0 token rows and 0 password rows, and both whoami and refresh_token with the API token → 401.
  • A token that was used just before leaving keeps answering from AUTH_CACHE until the TTL runs out. This is the existing documented behaviour for non-session labels, and delete_user behaves 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 shared revoke_mcp_oauth_grants helper that delete_user already 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_instance now calls revoke_mcp_oauth_grants and deletes all token rows for the email inside its existing transaction, before deleting the password row.
  • Adds a test pinning that leaving the instance removes the account's tokens and invalidates its API token.
  • Updates docs/auth-surface.md and a comment in remote_deploy.rs to reflect the new behavior.