Skip to content

feat(api): let an api token revoke itself via /api/v1/users/me/api-tokens/current/ (FOSS-835) - #60

Merged
UsamaSadiq merged 4 commits into
foss-sandboxfrom
usama/foss-835-api-token-self-revoke
Nov 24, 2026
Merged

UsamaSadiq merged 4 commits into
foss-sandboxfrom
usama/foss-835-api-token-self-revoke

Conversation

@UsamaSadiq

Copy link
Copy Markdown
Collaborator

Description

  • Closes FOSS-835: this is the Plane half of the self-revoke routes. The Outline half, feat(api): let an api key delete itself via apiKeys.delete (FOSS-835) outline#43, is already merged.
  • The askii access-agent will mint a per-user Plane token labelled askii-access:<grant_id>. When the grant is revoked, the agent holds only that token and no session. Today /api/users/api-tokens/<pk>/ is session-only and /api/v1 has no token endpoint, so the agent cannot delete the token.

apps/api/plane/api/views/user.py adds CurrentAPITokenEndpoint, served at DELETE /api/v1/users/me/api-tokens/current/:

  • It reads X-Api-Key itself and uses no authentication class. Holding the key is enough to destroy it, so a token that is expired or inactive, or whose user is deactivated, can still be revoked. If that row stayed live, it would start working again once the user was reactivated.

  • The revoke is a single soft-delete statement with no Celery cascade:

    APIToken.objects.filter(
        token=key, user_type=0, importer__isnull=True, integrations__isnull=True
    ).update(deleted_at=timezone.now())
  • The response is 204 if a row changed. Otherwise it is 401 with WWW-Authenticate: ApiKey realm="api".

  • Only personal tokens with no dependents qualify. Bot tokens and tokens referenced by an Importer or WorkspaceIntegration get 401 and stay live; they keep using the session route and its cascade. The OpenAPI description says so.

  • This is the only /api/v1 route that answers a rejected key with 401 rather than 403. The OpenAPI description documents it.

apps/api/plane/api/rate_limit.py adds ApiKeyRevocationRateThrottle:

  • It keys on IP at 600/min and replaces the per-key api_key throttle on this route.
  • With the per-key throttle, a busy or leaked key could be locked out of revoking itself.
  • The 429 response and its Retry-After header are documented.

apps/api/plane/api/urls/user.py and views/__init__.py add the route (DELETE only) and the export.

Testing

  • 52/52 passed across plane/tests/contract/api/test_current_api_token.py (14 tests) and the existing key-auth and api-token suites: contract/api/test_authentication.py, contract/app/test_api_token.py and unit/api/test_authentication_order.py. These ran against throwaway Postgres, Valkey and RabbitMQ containers.
  • The new tests cover these cases:
    • A token deletes itself (204, soft-deleted), and its next use gets 401 here and 403 on /api/v1/users/me/.
    • An expired token, an inactive token, and a deactivated user's token all revoke with 204.
    • The same user's other token and another user's token survive.
    • A key sent alongside another user's session revokes only the key owner's token.
    • A bot token gets 401 and stays live, as does a token used by a WorkspaceIntegration.
    • An unknown key, a missing key, or a session with no key gets 401.
    • A saturated per-key throttle still allows 204.
    • GET gets 405.
  • The Importer exclusion has no test of its own, because it needs a project fixture. It uses the same filter shape as the WorkspaceIntegration case, which is tested.
  • contract/api and unit show 14 failures on both this branch and untouched foss-sandbox. They are 429s on the shared fixture token and are unrelated to this change.
  • ruff check and ruff format --check are clean, and the OpenAPI schema generates without warnings.

AI disclosure

  • Prepared with Anthropic Claude (Claude Code).
  • Usama Sadiq reviewed the diff and this description and can answer for the change.

@UsamaSadiq
UsamaSadiq merged commit 2bac13a into foss-sandbox Nov 24, 2026
2 checks passed
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