2 Based on django.contrib.auth.tokens
6 from datetime import date
7 from django.conf import settings
8 from django.utils.http import int_to_base36, base36_to_int
9 from django.contrib.auth.tokens import PasswordResetTokenGenerator
12 REGISTRATION_TIMEOUT_DAYS = getattr(settings, 'WALDO_REGISTRATION_TIMEOUT_DAYS', 1)
13 EMAIL_TIMEOUT_DAYS = getattr(settings, 'WALDO_EMAIL_TIMEOUT_DAYS', 1)
16 class RegistrationTokenGenerator(PasswordResetTokenGenerator):
18 Strategy object used to generate and check tokens for the user registration mechanism.
20 def check_token(self, user, token):
22 Check that a registration token is correct for a given user.
24 # If the user is active, the hash can't be valid.
30 ts_b36, hash = token.split('-')
35 ts = base36_to_int(ts_b36)
39 # Check that the timestamp and uid have not been tampered with.
40 if self._make_token_with_timestamp(user, ts) != token:
43 # Check that the timestamp is within limit
44 if (self._num_days(self._today()) - ts) > REGISTRATION_TIMEOUT_DAYS:
49 def _make_token_with_timestamp(self, user, timestamp):
50 ts_b36 = int_to_base36(timestamp)
52 # By hashing on the internal state of the user and using state that is
53 # sure to change, we produce a hash that will be invalid as soon as it
55 from django.utils.hashcompat import sha_constructor
56 hash = sha_constructor(settings.SECRET_KEY + unicode(user.id) + unicode(user.is_active) + user.last_login.strftime('%Y-%m-%d %H:%M:%S') + unicode(timestamp)).hexdigest()[::2]
57 return '%s-%s' % (ts_b36, hash)
60 registration_token_generator = RegistrationTokenGenerator()
63 class EmailTokenGenerator(PasswordResetTokenGenerator):
65 Strategy object used to generate and check tokens for a user email change mechanism.
67 def make_token(self, user, email):
69 Returns a token that can be used once to do an email change for the given user and email.
71 return self._make_token_with_timestamp(user, email, self._num_days(self._today()))
73 def check_token(self, user, email, token):
74 if email == user.email:
79 ts_b36, hash = token.split('-')
84 ts = base36_to_int(ts_b36)
88 # Check that the timestamp and uid have not been tampered with.
89 if self._make_token_with_timestamp(user, email, ts) != token:
92 # Check that the timestamp is within limit
93 if (self._num_days(self._today()) - ts) > EMAIL_TIMEOUT_DAYS:
98 def _make_token_with_timestamp(self, user, email, timestamp):
99 ts_b36 = int_to_base36(timestamp)
101 from django.utils.hashcompat import sha_constructor
102 hash = sha_constructor(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()