Skip to content

Allow users to regenerate or delete their own API token #2717

Open

Description

@harshitAgr

Users authenticate API clients with a DRF token obtained by POSTing credentials to /api/api-token-auth/. In src/apps/api/urls.py this route is the stock rest_framework.authtoken.views.obtain_auth_token, which uses Token.objects.get_or_create(user=user) and therefore always returns the same token. No other route creates, replaces or deletes a token (delete_account/ removes the whole account).

As a result, if a token is exposed, its owner cannot invalidate it. Only a platform administrator can delete the Token row. Administrators have confirmed that there is currently no way for users to delete their own token and suggested opening an issue to track it.

Possible approach (maintainers to decide):

  • An authenticated endpoint that deletes the caller's token, or replaces it with a new one.
  • A button on the profile/settings page that calls it.

Activity

  1. added
    Enhancement
    on Nov 13, 2026
  2. duy-dev-26 commented on Nov 14, 2026

    @duy-dev-26

    Hi! I'd like to work on this one if the approach you have in mind matches theirs.

    What I'd do, following the two options in the issue, and picking one unless you say otherwise:

    1. A replace endpoint, POST /api/api-token-refresh/ (name to your taste), requiring an
      authenticated caller, that atomically deletes the caller's Token row and creates a new
      one, returning the new key. Session-authenticated callers only, so a token cannot be
      rotated with the token that leaked.
    2. Only the caller's own token, never a user_id from the request body — otherwise it is a
      privilege escalation.
    3. The stock obtain_auth_token route stays as it is, so existing clients keep working; this
      adds the ability to invalidate, it does not change how a token is first issued.
    4. Tests next to the existing API tests: replacing returns a different key, the old key is
      rejected afterwards, an unauthenticated call gets 401/403, and a caller cannot touch
      another user's token.

    One thing I'd like your call on before I write anything: the issue also suggests a button on
    the profile/settings page. That is a frontend change in a different app (src/apps/), and I'd
    rather land the small API piece on its own than mix the two in one PR. If you want both in one
    go, say so and I'll look at where the settings page lives.

    I'm a master's student on an open-source software course, so I'll keep the change small and
    follow whatever the repo already does for API views and tests. Happy to be pointed at a
    different approach if the maintainers have already decided on one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions