Skip to content

Instantly share code, notes, and snippets.

@goneri
Created April 30, 2024 12:00
Show Gist options
  • Save goneri/78e7d3a41427b4608b6de688fadcee75 to your computer and use it in GitHub Desktop.
Save goneri/78e7d3a41427b4608b6de688fadcee75 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from datetime import timedelta
from django.utils import timezone
from oauth2_provider.models import AccessToken
from oauthlib.common import generate_token
def rotate_token(token_string):
current_token = AccessToken.objects.filter(token=token_string).first()
if not current_token:
print("No token found")
return
new_token = AccessToken.objects.create(
token=generate_token(),
user=current_token.user,
scope=current_token.scope,
expires=timezone.now() + timedelta(days=700),
)
current_token.revoke()
print(new_token)
@goneri
Copy link
Author

goneri commented Apr 30, 2024

Usage:

In [28]: rotate_token("BCA2TkHE1omfd6bouS2GSfvKYa1767")
new token bcuZ8MeugcaFYtviSxS6AEjW9JjeOq

In [29]: rotate_token("BCA2TkHE1omfd6bouS2GSfvKYa1767")
No token found

In [30]: rotate_token("bcuZ8MeugcaFYtviSxS6AEjW9JjeOq")
new token nvhqYE6Id8G0EVgOBXd7ELD3bj5hv1

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