diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py
index 09db690b5c..c2409d1690 100644
a
|
b
|
def login(request, user, backend=None):
|
91 | 91 | the anonymous session is retained when the user logs in. |
92 | 92 | """ |
93 | 93 | session_auth_hash = '' |
| 94 | session_auth_hash_legacy = '' |
94 | 95 | if user is None: |
95 | 96 | user = request.user |
96 | 97 | if hasattr(user, 'get_session_auth_hash'): |
97 | 98 | session_auth_hash = user.get_session_auth_hash() |
| 99 | if hasattr(user, '_get_session_auth_hash_legacy'): |
| 100 | session_auth_hash_legacy = user._get_session_auth_hash_legacy() |
98 | 101 | |
99 | 102 | if SESSION_KEY in request.session: |
100 | | if _get_user_session_key(request) != user.pk or ( |
| 103 | if _get_user_session_key(request) != user.pk or (( |
101 | 104 | session_auth_hash and |
102 | | not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash)): |
| 105 | not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash)) and ( |
| 106 | session_auth_hash_legacy and |
| 107 | not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash_legacy))): |
103 | 108 | # To avoid reusing another user's session, create a new, empty |
104 | 109 | # session if the existing session corresponds to a different |
105 | 110 | # authenticated user. |
diff --git a/django/contrib/auth/base_user.py b/django/contrib/auth/base_user.py
index f39c12a350..13fa59785c 100644
a
|
b
|
class AbstractBaseUser(models.Model):
|
120 | 120 | """ |
121 | 121 | return is_password_usable(self.password) |
122 | 122 | |
| 123 | def _get_session_auth_hash_legacy(self): |
| 124 | """ |
| 125 | Return an HMAC of the password field. |
| 126 | """ |
| 127 | key_salt = "django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash" |
| 128 | return salted_hmac(key_salt, self.password, algorithm='sha1').hexdigest() |
| 129 | |
123 | 130 | def get_session_auth_hash(self): |
124 | 131 | """ |
125 | 132 | Return an HMAC of the password field. |
126 | 133 | """ |
127 | 134 | key_salt = "django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash" |
128 | | return salted_hmac(key_salt, self.password).hexdigest() |
| 135 | return salted_hmac(key_salt, self.password, algorithm='sha256').hexdigest() |
129 | 136 | |
130 | 137 | @classmethod |
131 | 138 | def get_email_field_name(cls): |