Skip to content

Drop Django 3.1 related code #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions mailauth/contrib/user/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.db import models
Expand Down Expand Up @@ -75,29 +74,12 @@ class Meta(AbstractUser.Meta):
("anonymize", "Can anonymize user"),
]

def _legacy_get_session_auth_hash(self):
# RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid.
key_salt = "mailauth.contrib.user.models.EmailUserManager.get_session_auth_hash"
if not self.session_salt:
raise ValueError("'session_salt' must be set")
return salted_hmac(key_salt, self.session_salt, algorithm="sha1").hexdigest()

def get_session_auth_hash(self):
"""Return an HMAC of the :attr:`.session_salt` field."""
key_salt = "mailauth.contrib.user.models.EmailUserManager.get_session_auth_hash"
if not self.session_salt:
raise ValueError("'session_salt' must be set")
algorithm = getattr(settings, "DEFAULT_HASHING_ALGORITHM", None)
if algorithm is None:
return salted_hmac(key_salt, self.session_salt).hexdigest()
return salted_hmac(
key_salt,
self.session_salt,
# RemovedInDjango40Warning: when the deprecation ends, replace
# with:
# algorithm='sha256',
algorithm=algorithm,
).hexdigest()
return salted_hmac(key_salt, self.session_salt, algorithm="sha256").hexdigest()

def anonymize(self, commit=True):
"""
Expand Down
25 changes: 1 addition & 24 deletions tests/contrib/auth/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,10 @@ def test_get_session_auth_hash__unique(self, db):

assert spiderman.get_session_auth_hash() != ironman.get_session_auth_hash()

def test_legacy_get_session_auth_hash__default(self, db):
user = EmailUser(email="spiderman@avengers.com")

assert user.session_salt
assert user._legacy_get_session_auth_hash()

def test_legacy_get_session_auth_hash__value_error(self, db):
user = EmailUser(email="spiderman@avengers.com", session_salt=None)

with pytest.raises(ValueError) as e:
user._legacy_get_session_auth_hash()

assert "'session_salt' must be set" in str(e.value)

def test_legacy_get_session_auth_hash__unique(self, db):
spiderman = EmailUser(email="spiderman@avengers.com")
ironman = EmailUser(email="ironman@avengers.com")

assert (
spiderman._legacy_get_session_auth_hash()
!= ironman._legacy_get_session_auth_hash()
)

def test_password_field(self):
user = EmailUser(email="spiderman@avengers.com")
with pytest.raises(FieldDoesNotExist):
user.password
assert user.password


class TestEmailUserManager:
Expand Down