1+ from django .conf import settings
12from django .contrib .auth .base_user import BaseUserManager
23from django .contrib .auth .models import AbstractUser
34from django .db import models
@@ -37,6 +38,10 @@ def create_superuser(self, email, **extra_fields):
3738 return self ._create_user (email , ** extra_fields )
3839
3940
41+ def _get_session_salt ():
42+ return get_random_string (12 )
43+
44+
4045class AbstractEmailUser (AbstractUser ):
4146 EMAIL_FIELD = 'email'
4247 USERNAME_FIELD = 'email'
@@ -51,7 +56,7 @@ class AbstractEmailUser(AbstractUser):
5156 # Salt for the session hash replacing the password in this function.
5257 session_salt = models .CharField (
5358 max_length = 12 , editable = False ,
54- default = get_random_string ,
59+ default = _get_session_salt ,
5560 )
5661
5762 def has_usable_password (self ):
@@ -62,12 +67,29 @@ def has_usable_password(self):
6267 class Meta (AbstractUser .Meta ):
6368 abstract = True
6469
70+ def _legacy_get_session_auth_hash (self ):
71+ # RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid.
72+ key_salt = "mailauth.contrib.user.models.EmailUserManager.get_session_auth_hash"
73+ if not self .session_salt :
74+ raise ValueError ("'session_salt' must be set" )
75+ return salted_hmac (key_salt , self .session_salt , algorithm = 'sha1' ).hexdigest ()
76+
6577 def get_session_auth_hash (self ):
6678 """Return an HMAC of the :attr:`.session_salt` field."""
6779 key_salt = "mailauth.contrib.user.models.EmailUserManager.get_session_auth_hash"
6880 if not self .session_salt :
6981 raise ValueError ("'session_salt' must be set" )
70- return salted_hmac (key_salt , self .session_salt ).hexdigest ()
82+ algorithm = getattr (settings , 'DEFAULT_HASHING_ALGORITHM' , None )
83+ if algorithm is None :
84+ return salted_hmac (key_salt , self .session_salt ).hexdigest ()
85+ return salted_hmac (
86+ key_salt ,
87+ self .session_salt ,
88+ # RemovedInDjango40Warning: when the deprecation ends, replace
89+ # with:
90+ # algorithm='sha256',
91+ algorithm = algorithm ,
92+ ).hexdigest ()
7193
7294
7395delattr (AbstractEmailUser , 'password' )
0 commit comments