Skip to main content

AbstractBaseUser

This abstract base class provides the core implementation for a user model, including password management and authentication state tracking. It features built-in methods for password hashing, validation, and session security using HMAC-based hashes. The class establishes a foundation for custom user models by defining essential fields like password and last login while remaining flexible for specific application requirements.

Attributes

AttributeTypeDescription
passwordstringHashed password string used for user authentication and session verification.
last_logindatetimeThe date and time when the user last successfully authenticated into the system.
is_activeboolean = trueBoolean flag indicating whether the user account is considered active for authentication purposes.
REQUIRED_FIELDSlist = []A list of field names that will be prompted for when creating a user via the createsuperuser management command.

Constructor

Signature

def AbstractBaseUser(
*args: any,
**kwargs: any
) - > null

Parameters

NameTypeDescription
*argsanyPositional arguments passed to the model constructor.
**kwargsanyKeyword arguments representing field values and other model attributes.

Methods


save()

@classmethod
def save(
**kwargs: dict
) - > null

Saves the current user instance to the database and triggers password validation logic if the password was recently updated.

Parameters

NameTypeDescription
**kwargsdictArbitrary keyword arguments passed to the parent model's save method.

Returns

TypeDescription
nullNothing is returned.

get_username()

@classmethod
def get_username() - > string

Return the username for this User.

Returns

TypeDescription
stringThe value of the field defined as the USERNAME_FIELD.

clean()

@classmethod
def clean() - > null

Normalizes the username field value before the model is validated or saved.

Returns

TypeDescription
nullNothing is returned.

natural_key()

@classmethod
def natural_key() - > tuple

Returns a tuple containing the username, used for identifying the user during serialization.

Returns

TypeDescription
tupleA single-element tuple containing the user's username.

is_anonymous()

@classmethod
def is_anonymous() - > boolean

Always return False. This is a way of comparing User objects to anonymous users.

Returns

TypeDescription
booleanAlways False for a persisted user instance.

is_authenticated()

@classmethod
def is_authenticated() - > boolean

Always return True. This is a way to tell if the user has been authenticated in templates.

Returns

TypeDescription
booleanAlways True for a persisted user instance.

set_password()

@classmethod
def set_password(
raw_password: string
) - > null

Sets the user's password to a hashed version of the provided raw string and tracks the raw value for post-save validation.

Parameters

NameTypeDescription
raw_passwordstringThe plain-text password to be hashed and stored.

Returns

TypeDescription
nullNothing is returned.

check_password()

@classmethod
def check_password(
raw_password: string
) - > boolean

Return a boolean of whether the raw_password was correct. Handles hashing formats behind the scenes.

Parameters

NameTypeDescription
raw_passwordstringThe plain-text password to verify against the stored hash.

Returns

TypeDescription
booleanTrue if the password matches, False otherwise.

acheck_password()

@classmethod
def acheck_password(
raw_password: string
) - > boolean

See check_password().

Parameters

NameTypeDescription
raw_passwordstringThe plain-text password to verify asynchronously.

Returns

TypeDescription
booleanAn awaitable boolean indicating if the password matches.

set_unusable_password()

@classmethod
def set_unusable_password() - > null

Sets the password to an invalid hash that cannot be matched by any input, effectively disabling password login.

Returns

TypeDescription
nullNothing is returned.

has_usable_password()

@classmethod
def has_usable_password() - > boolean

Return False if set_unusable_password() has been called for this user.

Returns

TypeDescription
booleanTrue if the user has a valid password hash, False otherwise.

get_session_auth_hash()

@classmethod
def get_session_auth_hash() - > string

Return an HMAC of the password field.

Returns

TypeDescription
stringA hex-encoded HMAC hash used to invalidate sessions when the password changes.

get_session_auth_fallback_hash()

@classmethod
def get_session_auth_fallback_hash() - > generator

Generates session authentication hashes using fallback secret keys to maintain session validity during secret rotation.

Returns

TypeDescription
generatorA generator yielding hex-encoded HMAC hashes for each fallback secret.

get_email_field_name()

@classmethod
def get_email_field_name() - > string

Returns the name of the field designated as the email field for the user model.

Returns

TypeDescription
stringThe name of the email field, defaulting to 'email' if not explicitly defined.

normalize_username()

@classmethod
def normalize_username(
username: string
) - > string

Normalizes the username using NFKC Unicode normalization to ensure consistent storage and comparison.

Parameters

NameTypeDescription
usernamestringThe raw username string to be normalized.

Returns

TypeDescription
stringThe normalized username string.