| 1 |
""" |
| 2 |
This module allows importing AbstractBaseUser even when django.contrib.auth is |
| 3 |
not in INSTALLED_APPS. |
| 4 |
""" |
| 5 |
|
| 6 |
import unicodedata |
| 7 |
|
| 8 |
from django.conf import settings |
| 9 |
from django.contrib.auth import password_validation |
| 10 |
from django.contrib.auth.hashers import ( |
| 11 |
acheck_password, |
| 12 |
check_password, |
| 13 |
is_password_usable, |
| 14 |
make_password, |
| 15 |
) |
| 16 |
from django.db import models |
| 17 |
from django.utils.crypto import salted_hmac |
| 18 |
from django.utils.translation import gettext_lazy as _ |
| 19 |
|
| 20 |
|
| 21 |
class BaseUserManager(models.Manager): |
| 22 |
@classmethod |
| 23 |
def normalize_email(cls, email): |
| 24 |
""" |
| 25 |
Normalize the email address by lowercasing the domain part of it. |
| 26 |
""" |
| 27 |
email = email or "" |
| 28 |
try: |
| 29 |
email_name, domain_part = email.strip().rsplit("@", 1) |
| 30 |
except ValueError: |
| 31 |
pass |
| 32 |
else: |
| 33 |
email = email_name + "@" + domain_part.lower() |
| 34 |
return email |
| 35 |
|
| 36 |
def get_by_natural_key(self, username): |
| 37 |
return self.get(**{self.model.USERNAME_FIELD: username}) |
| 38 |
|
| 39 |
async def aget_by_natural_key(self, username): |
| 40 |
return await self.aget(**{self.model.USERNAME_FIELD: username}) |
| 41 |
|
| 42 |
|
| 43 |
class AbstractBaseUser(models.Model): |
| 44 |
password = models.CharField(_("password"), max_length=128) |
| 45 |
last_login = models.DateTimeField(_("last login"), blank=True, null=True) |
| 46 |
|
| 47 |
is_active = True |
| 48 |
|
| 49 |
REQUIRED_FIELDS = [] |
| 50 |
|
| 51 |
# Stores the raw password if set_password() is called so that it can |
| 52 |
# be passed to password_changed() after the model is saved. |
| 53 |
_password = None |
| 54 |
|
| 55 |
class Meta: |
| 56 |
abstract = True |
| 57 |
|
| 58 |
def __str__(self): |
| 59 |
return self.get_username() |
| 60 |
|
| 61 |
# RemovedInDjango60Warning: When the deprecation ends, replace with: |
| 62 |
# def save(self, **kwargs): |
| 63 |
# super().save(**kwargs) |
| 64 |
def save(self, *args, **kwargs): |
| 65 |
super().save(*args, **kwargs) |
| 66 |
if self._password is not None: |
| 67 |
password_validation.password_changed(self._password, self) |
| 68 |
self._password = None |
| 69 |
|
| 70 |
def get_username(self): |
| 71 |
"""Return the username for this User.""" |
| 72 |
return getattr(self, self.USERNAME_FIELD) |
| 73 |
|
| 74 |
def clean(self): |
| 75 |
setattr(self, self.USERNAME_FIELD, self.normalize_username(self.get_username())) |
| 76 |
|
| 77 |
def natural_key(self): |
| 78 |
return (self.get_username(),) |
| 79 |
|
| 80 |
@property |
| 81 |
def is_anonymous(self): |
| 82 |
""" |
| 83 |
Always return False. This is a way of comparing User objects to |
| 84 |
anonymous users. |
| 85 |
""" |
| 86 |
return False |
| 87 |
|
| 88 |
@property |
| 89 |
def is_authenticated(self): |
| 90 |
""" |
| 91 |
Always return True. This is a way to tell if the user has been |
| 92 |
authenticated in templates. |
| 93 |
""" |
| 94 |
return True |
| 95 |
|
| 96 |
def set_password(self, raw_password): |
| 97 |
self.password = make_password(raw_password) |
| 98 |
self._password = raw_password |
| 99 |
|
| 100 |
def check_password(self, raw_password): |
| 101 |
""" |
| 102 |
Return a boolean of whether the raw_password was correct. Handles |
| 103 |
hashing formats behind the scenes. |
| 104 |
""" |
| 105 |
|
| 106 |
def setter(raw_password): |
| 107 |
self.set_password(raw_password) |
| 108 |
# Password hash upgrades shouldn't be considered password changes. |
| 109 |
self._password = None |
| 110 |
self.save(update_fields=["password"]) |
| 111 |
|
| 112 |
return check_password(raw_password, self.password, setter) |
| 113 |
|
| 114 |
async def acheck_password(self, raw_password): |
| 115 |
"""See check_password().""" |
| 116 |
|
| 117 |
async def setter(raw_password): |
| 118 |
self.set_password(raw_password) |
| 119 |
# Password hash upgrades shouldn't be considered password changes. |
| 120 |
self._password = None |
| 121 |
await self.asave(update_fields=["password"]) |
| 122 |
|
| 123 |
return await acheck_password(raw_password, self.password, setter) |
| 124 |
|
| 125 |
def set_unusable_password(self): |
| 126 |
# Set a value that will never be a valid hash |
| 127 |
self.password = make_password(None) |
| 128 |
|
| 129 |
def has_usable_password(self): |
| 130 |
""" |
| 131 |
Return False if set_unusable_password() has been called for this user. |
| 132 |
""" |
| 133 |
return is_password_usable(self.password) |
| 134 |
|
| 135 |
def get_session_auth_hash(self): |
| 136 |
""" |
| 137 |
Return an HMAC of the password field. |
| 138 |
""" |
| 139 |
return self._get_session_auth_hash() |
| 140 |
|
| 141 |
def get_session_auth_fallback_hash(self): |
| 142 |
for fallback_secret in settings.SECRET_KEY_FALLBACKS: |
| 143 |
yield self._get_session_auth_hash(secret=fallback_secret) |
| 144 |
|
| 145 |
def _get_session_auth_hash(self, secret=None): |
| 146 |
key_salt = "django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash" |
| 147 |
return salted_hmac( |
| 148 |
key_salt, |
| 149 |
self.password, |
| 150 |
secret=secret, |
| 151 |
algorithm="sha256", |
| 152 |
).hexdigest() |
| 153 |
|
| 154 |
@classmethod |
| 155 |
def get_email_field_name(cls): |
| 156 |
try: |
| 157 |
return cls.EMAIL_FIELD |
| 158 |
except AttributeError: |
| 159 |
return "email" |
| 160 |
|
| 161 |
@classmethod |
| 162 |
def normalize_username(cls, username): |
| 163 |
return ( |
| 164 |
unicodedata.normalize("NFKC", username) |
| 165 |
if isinstance(username, str) |
| 166 |
else username |
| 167 |
) |