2 Based on :mod:`django.contrib.auth.tokens`. Supports the following settings:
4 :setting:`WALDO_REGISTRATION_TIMEOUT_DAYS`
5 The number of days a registration link will be valid before expiring. Default: 1.
7 :setting:`WALDO_EMAIL_TIMEOUT_DAYS`
8 The number of days an email change link will be valid before expiring. Default: 1.
12 from hashlib import sha1
13 from datetime import date
15 from django.conf import settings
16 from django.utils.http import int_to_base36, base36_to_int
17 from django.contrib.auth.tokens import PasswordResetTokenGenerator
20 REGISTRATION_TIMEOUT_DAYS = getattr(settings, 'WALDO_REGISTRATION_TIMEOUT_DAYS', 1)
21 EMAIL_TIMEOUT_DAYS = getattr(settings, 'WALDO_EMAIL_TIMEOUT_DAYS', 1)
24 class RegistrationTokenGenerator(PasswordResetTokenGenerator):
25 """Strategy object used to generate and check tokens for the user registration mechanism."""
27 def check_token(self, user, token):
28 """Check that a registration token is correct for a given user."""
29 # If the user is active, the hash can't be valid.
35 ts_b36, hash = token.split('-')
40 ts = base36_to_int(ts_b36)
44 # Check that the timestamp and uid have not been tampered with.
45 if self._make_token_with_timestamp(user, ts) != token:
48 # Check that the timestamp is within limit
49 if (self._num_days(self._today()) - ts) > REGISTRATION_TIMEOUT_DAYS:
54 def _make_token_with_timestamp(self, user, timestamp):
55 ts_b36 = int_to_base36(timestamp)
57 # By hashing on the internal state of the user and using state that is
58 # sure to change, we produce a hash that will be invalid as soon as it
60 hash = sha1(settings.SECRET_KEY + unicode(user.id) + unicode(user.is_active) + user.last_login.strftime('%Y-%m-%d %H:%M:%S') + unicode(timestamp)).hexdigest()[::2]
61 return '%s-%s' % (ts_b36, hash)
64 registration_token_generator = RegistrationTokenGenerator()
67 class EmailTokenGenerator(PasswordResetTokenGenerator):
68 """Strategy object used to generate and check tokens for a user email change mechanism."""
70 def make_token(self, user, email):
71 """Returns a token that can be used once to do an email change for the given user and email."""
72 return self._make_token_with_timestamp(user, email, self._num_days(self._today()))
74 def check_token(self, user, email, token):
75 if email == user.email:
80 ts_b36, hash = token.split('-')
85 ts = base36_to_int(ts_b36)
89 # Check that the timestamp and uid have not been tampered with.
90 if self._make_token_with_timestamp(user, email, ts) != token:
93 # Check that the timestamp is within limit
94 if (self._num_days(self._today()) - ts) > EMAIL_TIMEOUT_DAYS:
99 def _make_token_with_timestamp(self, user, email, timestamp):
100 ts_b36 = int_to_base36(timestamp)
102 hash = sha1(settings.SECRET_KEY + unicode(user.id) + user.email + email + unicode(timestamp)).hexdigest()[::2]
103 return '%s-%s' % (ts_b36, hash)
106 email_token_generator = EmailTokenGenerator()