aaugustin / django-sesame

"Magic Links" - URLs with authentication tokens for one-click login
https://django-sesame.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
983 stars 57 forks source link

Invalidate token #61

Closed thomascenni closed 4 years ago

thomascenni commented 4 years ago

Hi, is it possible to invalidate the token before its expiration ? My use case is that a user fill a form (wizard) and at the end of the wizard, if the data are valid, they are stored in the db and the token must be invalidated. Thanks.

aaugustin commented 4 years ago

Right now the only way to invalidate a token is to change the user's password. There's several ways to achieve this, which may or may not be practical:

Depending on how you're using django-sesame, you may also be able to do something with the recently introduced feature - scoped tokens. The workflow would look like:

Hope this helps!

thomascenni commented 4 years ago

Merci Aymeric! I have already set unusable passwords, I will do in this way. May be later I will evaluate your proposed workflow. Thanks!

aaugustin commented 4 years ago

Good, then it's as simple as:

assert user.has_usable_password()  # let's not destroy a password accidentally
user.set_unusable_password()  # change password to invalidate token
user.save()
Mapiarz commented 3 years ago

@aaugustin How would one invalidate a token programmatically without having the user give the password? Assuming the user has a usable password.

aaugustin commented 3 years ago

django-sesame doesn't provide a way to do this — because it doesn't store any state beyond what django.contrib.auth already stores, and there's no piece of state that you can alter to get this effect.