summaryrefslogtreecommitdiff
path: root/django/contrib/auth/tokens.py
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2008-08-05 17:15:33 +0000
committerJustin Bronn <jbronn@gmail.com>2008-08-05 17:15:33 +0000
commitaa239e3e5405933af6a29dac3cf587b59a099927 (patch)
treeea2cbd139c9a8cf84c09e0b2008bff70e05927ef /django/contrib/auth/tokens.py
parent45b73c9a4685809236f84046cc7ffd32a50db958 (diff)
gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.archive/attic/gis
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8215 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/contrib/auth/tokens.py')
-rw-r--r--django/contrib/auth/tokens.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/django/contrib/auth/tokens.py b/django/contrib/auth/tokens.py
new file mode 100644
index 0000000000..c9b353583c
--- /dev/null
+++ b/django/contrib/auth/tokens.py
@@ -0,0 +1,66 @@
+from datetime import date
+from django.conf import settings
+from django.utils.http import int_to_base36, base36_to_int
+
+class PasswordResetTokenGenerator(object):
+ """
+ Stratgy object used to generate and check tokens for the password
+ reset mechanism.
+ """
+ def make_token(self, user):
+ """
+ Returns a token that can be used once to do a password reset
+ for the given user.
+ """
+ return self._make_token_with_timestamp(user, self._num_days(self._today()))
+
+ def check_token(self, user, token):
+ """
+ Check that a password reset token is correct for a given user.
+ """
+ # Parse the tokem
+ try:
+ ts_b36, hash = token.split("-")
+ except ValueError:
+ return False
+
+ try:
+ ts = base36_to_int(ts_b36)
+ except ValueError:
+ return False
+
+ # Check that the timestamp/uid has not been tampered with
+ if self._make_token_with_timestamp(user, ts) != token:
+ return False
+
+ # Check the timestamp is within limit
+ if (self._num_days(self._today()) - ts) > settings.PASSWORD_RESET_TIMEOUT_DAYS:
+ return False
+
+ return True
+
+ def _make_token_with_timestamp(self, user, timestamp):
+ # timestamp is number of days since 2001-1-1. Converted to
+ # base 36, this gives us a 3 digit string until about 2121
+ ts_b36 = int_to_base36(timestamp)
+
+ # By hashing on the internal state of the user and using state
+ # that is sure to change (the password salt will change as soon as
+ # the password is set, at least for current Django auth, and
+ # last_login will also change), we produce a hash that will be
+ # invalid as soon as it is used.
+ # We limit the hash to 20 chars to keep URL short
+ from django.utils.hashcompat import sha_constructor
+ hash = sha_constructor(settings.SECRET_KEY + unicode(user.id) +
+ user.password + unicode(user.last_login) +
+ unicode(timestamp)).hexdigest()[::2]
+ return "%s-%s" % (ts_b36, hash)
+
+ def _num_days(self, dt):
+ return (dt - date(2001,1,1)).days
+
+ def _today(self):
+ # Used for mocking in tests
+ return date.today()
+
+default_token_generator = PasswordResetTokenGenerator()