2015-01-28 13:35:27 +01:00
|
|
|
from datetime import date, timedelta
|
2010-10-10 03:06:42 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
2011-07-13 11:35:51 +02:00
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
2010-10-10 03:06:42 +02:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
class TokenGeneratorTest(TestCase):
|
|
|
|
|
|
|
|
def test_make_token(self):
|
|
|
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
|
|
|
self.assertTrue(p0.check_token(user, tk1))
|
|
|
|
|
|
|
|
def test_10265(self):
|
|
|
|
"""
|
2016-10-27 09:53:39 +02:00
|
|
|
The token generated for a user created in the same request
|
2010-10-10 03:06:42 +02:00
|
|
|
will work correctly.
|
|
|
|
"""
|
|
|
|
# See ticket #10265
|
|
|
|
user = User.objects.create_user('comebackkid', 'test3@example.com', 'testpw')
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
|
|
|
reload = User.objects.get(username='comebackkid')
|
|
|
|
tk2 = p0.make_token(reload)
|
|
|
|
self.assertEqual(tk1, tk2)
|
|
|
|
|
|
|
|
def test_timeout(self):
|
|
|
|
"""
|
2016-10-27 09:53:39 +02:00
|
|
|
The token is valid after n days, but no greater.
|
2010-10-10 03:06:42 +02:00
|
|
|
"""
|
|
|
|
# Uses a mocked version of PasswordResetTokenGenerator so we can change
|
|
|
|
# the value of 'today'
|
|
|
|
class Mocked(PasswordResetTokenGenerator):
|
|
|
|
def __init__(self, today):
|
|
|
|
self._today_val = today
|
2013-10-22 12:21:07 +02:00
|
|
|
|
2010-10-10 03:06:42 +02:00
|
|
|
def _today(self):
|
|
|
|
return self._today_val
|
|
|
|
|
|
|
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
2017-09-25 14:51:02 +02:00
|
|
|
p1 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS))
|
2010-10-10 03:06:42 +02:00
|
|
|
self.assertTrue(p1.check_token(user, tk1))
|
2017-09-25 14:51:02 +02:00
|
|
|
p2 = Mocked(date.today() + timedelta(settings.PASSWORD_RESET_TIMEOUT_DAYS + 1))
|
2010-10-10 03:06:42 +02:00
|
|
|
self.assertFalse(p2.check_token(user, tk1))
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-14 22:54:30 +02:00
|
|
|
|
2017-01-13 15:17:54 +01:00
|
|
|
def test_check_token_with_nonexistent_token_and_user(self):
|
|
|
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
|
|
|
self.assertIs(p0.check_token(None, tk1), False)
|
|
|
|
self.assertIs(p0.check_token(user, None), False)
|
2017-05-26 13:37:36 +02:00
|
|
|
|
|
|
|
def test_token_with_different_secret(self):
|
|
|
|
"""
|
|
|
|
A valid token can be created with a secret other than SECRET_KEY by
|
|
|
|
using the PasswordResetTokenGenerator.secret attribute.
|
|
|
|
"""
|
|
|
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
|
|
|
new_secret = 'abcdefghijkl'
|
|
|
|
# Create and check a token with a different secret.
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
p0.secret = new_secret
|
|
|
|
tk0 = p0.make_token(user)
|
|
|
|
self.assertTrue(p0.check_token(user, tk0))
|
|
|
|
# Create and check a token with the default secret.
|
|
|
|
p1 = PasswordResetTokenGenerator()
|
|
|
|
self.assertEqual(p1.secret, settings.SECRET_KEY)
|
|
|
|
self.assertNotEqual(p1.secret, new_secret)
|
|
|
|
tk1 = p1.make_token(user)
|
|
|
|
# Tokens created with a different secret don't validate.
|
|
|
|
self.assertFalse(p0.check_token(user, tk1))
|
|
|
|
self.assertFalse(p1.check_token(user, tk0))
|