Skip to content

Commit 92faffb

Browse files
authored
Merge pull request aaronn#75 from thetarby/dev
Callback token key changes after invalidating it. (after every update actually)
2 parents 83f9ddc + 09e6981 commit 92faffb

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

drfpasswordless/signals.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ def check_unique_tokens(sender, instance, **kwargs):
2828
"""
2929
Ensures that mobile and email tokens are unique or tries once more to generate.
3030
"""
31-
if isinstance(instance, CallbackToken):
32-
if CallbackToken.objects.filter(key=instance.key, is_active=True).exists():
33-
instance.key = generate_numeric_token()
31+
if instance._state.adding:
32+
# save is called on a token to create it in the db
33+
# before creating check whether a token with the same key exists
34+
if isinstance(instance, CallbackToken):
35+
if CallbackToken.objects.filter(key=instance.key, is_active=True).exists():
36+
instance.key = generate_numeric_token()
37+
else:
38+
# save is called on an already existing token to update it. Such as invalidating it.
39+
# in that case there is no need to check for the key. This way we both avoid an unneccessary db hit
40+
# and avoid to change key field of used tokens.
41+
pass
42+
3443

3544

3645
User = get_user_model()

0 commit comments

Comments
 (0)