Skip to content

Commit

Permalink
feat: make the password reset token generator configurable
Browse files Browse the repository at this point in the history
This adds facilities to override the default
EmailAwarePasswordResetTokenGenrator for users who wish to have custom
behaviour in this flow. Prior to this, the TokenForm class hardcoded
only one option, and even subclassing the view was not sufficient to
change the behaviour.
  • Loading branch information
Jay Crumb authored and pennersr committed Jul 6, 2023
1 parent 7d71855 commit 4cc792c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
Note worthy changes
-------------------

- Introduced a new setting ``ACCOUNT_PASSWORD_RESET_TOKEN_GENERATOR`` that
allows you to specify the token generator for password resets.

- Dropped support for Django 2.x and 3.0.

- Officially support Django 4.2.
Expand Down
12 changes: 12 additions & 0 deletions allauth/account/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,18 @@ def USERNAME_VALIDATORS(self):
ret = []
return ret

@property
def PASSWORD_RESET_TOKEN_GENERATOR(self):
from allauth.account.forms import EmailAwarePasswordResetTokenGenerator
from allauth.utils import import_attribute

token_generator_path = self._setting("PASSWORD_RESET_TOKEN_GENERATOR", None)
if token_generator_path is not None:
token_generator = import_attribute(token_generator_path)
else:
token_generator = EmailAwarePasswordResetTokenGenerator
return token_generator


# Ugly? Guido recommends this himself ...
# http://mail.python.org/pipermail/python-ideas/2012-May/014969.html
Expand Down
2 changes: 1 addition & 1 deletion allauth/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _make_hash_value(self, user, timestamp):
return ret


default_token_generator = EmailAwarePasswordResetTokenGenerator()
default_token_generator = app_settings.PASSWORD_RESET_TOKEN_GENERATOR()


class PasswordVerificationMixin(object):
Expand Down
6 changes: 6 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ ACCOUNT_LOGOUT_REDIRECT_URL (=`settings.LOGOUT_REDIRECT_URL or "/"`)
ACCOUNT_PASSWORD_INPUT_RENDER_VALUE (=False)
``render_value`` parameter as passed to ``PasswordInput`` fields.

ACCOUNT_PASSWORD_RESET_TOKEN_GENERATOR (=allauth.account.forms.EmailAwarePasswordResetTokenGenerator)
A string pointing to a custom token generator
(e.g. 'myapp.auth.CustomTokenGenerator') for password resets. This class
should implement the same methods as
``django.contrib.auth.tokens.PasswordResetTokenGenerator`` or subclass it.

ACCOUNT_PRESERVE_USERNAME_CASING (=True)
This setting determines whether the username is stored in lowercase
(``False``) or whether its casing is to be preserved (``True``). Note that when
Expand Down

0 comments on commit 4cc792c

Please sign in to comment.