-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imp/password reset improvements (#942)
* password-reset * imp/pw-reset-email-invalidation * mutations * pw token gen is sync * dependency updates
- Loading branch information
Showing
11 changed files
with
642 additions
and
541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.contrib.auth.tokens import PasswordResetTokenGenerator | ||
from users.models import User | ||
|
||
|
||
class GraiPasswordResetGenerator(PasswordResetTokenGenerator): | ||
def _make_hash_value(self, user: User, timestamp): | ||
email_field = user.get_email_field_name() | ||
email = getattr(user, email_field, "") or "" | ||
|
||
last_reset = user.last_pw_reset() | ||
if last_reset is None: | ||
raise Exception("Cannot generate password reset without request attempt") | ||
|
||
reset_timestamp = "" if last_reset is None else last_reset.created_at.replace(microsecond=0, tzinfo=None) | ||
|
||
return f"{user.pk}::{user.password}::{timestamp}::{reset_timestamp}::{email}" | ||
|
||
|
||
password_reset_generator = GraiPasswordResetGenerator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from auth.password_reset import password_reset_generator | ||
from users.models import User, AuditEvents, Audit | ||
from datetime import datetime | ||
import pytest | ||
from uuid import uuid4 | ||
|
||
TIMESTAMP = datetime.now().replace(microsecond=0, tzinfo=None) | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.fixture | ||
def user(): | ||
user = User(username=f"[email protected]") | ||
user.save() | ||
return user | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.fixture | ||
def audit(user): | ||
audit = Audit(user_id=user.id, event=AuditEvents.PASSWORD_RESET.name) | ||
audit.save() | ||
return audit | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.xfail | ||
def test_pw_reset_hash_no_pw_reset(): | ||
password_reset_generator._make_hash_value(User(username=f"[email protected]"), TIMESTAMP) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_pw_reset_hash(user, audit): | ||
hash_str = password_reset_generator._make_hash_value(user, TIMESTAMP) | ||
pk, pw, ts, r_ts, email = hash_str.split("::") | ||
|
||
assert pk == str(user.pk) | ||
assert pw == user.password | ||
assert ts == str(TIMESTAMP) | ||
assert r_ts == str(audit.created_at.replace(microsecond=0, tzinfo=None)) | ||
assert email == user.username |
Oops, something went wrong.