You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow users to regenerate or delete their own API token #2717
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.
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:
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.
Only the caller's own token, never a user_id from the request body — otherwise it is a
privilege escalation.
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.
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.
Activity
duy-dev-26 commentedon Nov 14, 2026
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:
POST /api/api-token-refresh/(name to your taste), requiring anauthenticated caller, that atomically deletes the caller's
Tokenrow and creates a newone, returning the new key. Session-authenticated callers only, so a token cannot be
rotated with the token that leaked.
user_idfrom the request body — otherwise it is aprivilege escalation.
obtain_auth_tokenroute stays as it is, so existing clients keep working; thisadds the ability to invalidate, it does not change how a token is first issued.
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'drather 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.