2019-08-23 15:14:07 +00:00
|
|
|
from datetime import datetime, timedelta
|
2010-10-10 01:06:42 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2011-07-13 09:35:51 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
2010-10-10 01:06:42 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
2019-11-13 09:31:41 +00:00
|
|
|
class MockedPasswordResetTokenGenerator(PasswordResetTokenGenerator):
|
|
|
|
def __init__(self, now):
|
|
|
|
self._now_val = now
|
|
|
|
|
|
|
|
def _now(self):
|
|
|
|
return self._now_val
|
|
|
|
|
|
|
|
|
2010-10-10 01:06:42 +00:00
|
|
|
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)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p0.check_token(user, tk1), True)
|
2010-10-10 01:06:42 +00:00
|
|
|
|
|
|
|
def test_10265(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The token generated for a user created in the same request
|
2010-10-10 01:06:42 +00:00
|
|
|
will work correctly.
|
|
|
|
"""
|
|
|
|
user = User.objects.create_user('comebackkid', 'test3@example.com', 'testpw')
|
2019-11-13 09:33:32 +00:00
|
|
|
user_reload = User.objects.get(username='comebackkid')
|
|
|
|
p0 = MockedPasswordResetTokenGenerator(datetime.now())
|
2010-10-10 01:06:42 +00:00
|
|
|
tk1 = p0.make_token(user)
|
2019-11-13 09:33:32 +00:00
|
|
|
tk2 = p0.make_token(user_reload)
|
2010-10-10 01:06:42 +00:00
|
|
|
self.assertEqual(tk1, tk2)
|
|
|
|
|
|
|
|
def test_timeout(self):
|
2019-08-23 15:14:07 +00:00
|
|
|
"""The token is valid after n seconds, but no greater."""
|
2010-10-10 01:06:42 +00:00
|
|
|
# Uses a mocked version of PasswordResetTokenGenerator so we can change
|
2019-08-23 15:14:07 +00:00
|
|
|
# the value of 'now'.
|
2010-10-10 01:06:42 +00:00
|
|
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
2020-03-12 09:56:38 +00:00
|
|
|
now = datetime.now()
|
|
|
|
p0 = MockedPasswordResetTokenGenerator(now)
|
2010-10-10 01:06:42 +00:00
|
|
|
tk1 = p0.make_token(user)
|
2019-11-13 09:31:41 +00:00
|
|
|
p1 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT)
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p1.check_token(user, tk1), True)
|
2019-11-13 09:31:41 +00:00
|
|
|
p2 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1))
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p2.check_token(user, tk1), False)
|
2019-08-23 15:14:07 +00:00
|
|
|
with self.settings(PASSWORD_RESET_TIMEOUT=60 * 60):
|
2019-11-13 09:31:41 +00:00
|
|
|
p3 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT)
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p3.check_token(user, tk1), True)
|
2019-11-13 09:31:41 +00:00
|
|
|
p4 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1))
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p4.check_token(user, tk1), False)
|
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 20:54:30 +00:00
|
|
|
|
2017-01-13 14:17:54 +00: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 11:37:36 +00: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)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p0.check_token(user, tk0), True)
|
2017-05-26 11:37:36 +00:00
|
|
|
# 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.
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p0.check_token(user, tk1), False)
|
|
|
|
self.assertIs(p1.check_token(user, tk0), False)
|
2020-01-17 09:09:55 +00:00
|
|
|
|
|
|
|
def test_legacy_token_validation(self):
|
|
|
|
# RemovedInDjango40Warning: pre-Django 3.1 tokens will be invalid.
|
|
|
|
user = User.objects.create_user('tokentestuser', 'test2@example.com', 'testpw')
|
|
|
|
p_old_generator = PasswordResetTokenGenerator()
|
|
|
|
p_old_generator.algorithm = 'sha1'
|
|
|
|
p_new_generator = PasswordResetTokenGenerator()
|
|
|
|
|
|
|
|
legacy_token = p_old_generator.make_token(user)
|
|
|
|
self.assertIs(p_old_generator.check_token(user, legacy_token), True)
|
|
|
|
self.assertIs(p_new_generator.check_token(user, legacy_token), True)
|