Serializers

Authentication and user management serializers for DRF Auth Kit.

Login Serializers

auth_kit.serializers.login.get_login_serializer() type[Serializer]

Get the login serializer class based on current settings.

This function creates the serializer class dynamically by combining the appropriate request and response serializers based on current auth kit settings.

Returns:

The combined login serializer class

class auth_kit.serializers.login_factors.LoginRequestSerializer(*args, **kwargs)

Bases: Serializer

User authentication credentials.

__init__(*args: Any, **kwargs: Any) None

Initialize login serializer.

Parameters:
  • *args – Variable length argument list

  • **kwargs – Arbitrary keyword arguments

authenticate(**kwargs: Any) AbstractBaseUser | None

Authenticate user with provided credentials.

Parameters:

**kwargs – Authentication credentials

Returns:

Authenticated user instance or None

get_auth_user(username: str | None, email: str | None, password: str | None) AbstractBaseUser | None

Get authenticated user based on username/email and password.

Parameters:
  • username – Username for authentication

  • email – Email for authentication

  • password – Password for authentication

Returns:

Authenticated user instance or None

validate(attrs: dict[str, Any]) dict[str, Any]

Validate login credentials and user status.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes with user in context

Raises:

ValidationError – If authentication fails or user is inactive

validate_email_verification_status(user: AbstractBaseUser) None

Validate email verification status when required.

Parameters:

user – User instance to validate

Raises:

ValidationError – If email verification is mandatory but not completed

class auth_kit.serializers.login_factors.BaseLoginResponseSerializer(*args, **kwargs)

Bases: Serializer

Authentication response with user details.

get_user(obj: dict[str, Any]) dict[str, Any]

Get serialized user details for the response.

Parameters:

obj – Object containing user data

Returns:

Serialized user data dictionary

class auth_kit.serializers.login_factors.JWTResponseSerializer(*args, **kwargs)

Bases: JWTSerializer, BaseLoginResponseSerializer

JWT authentication tokens and user information.

validate(attrs: dict[str, Any]) dict[str, Any]

Generate JWT tokens for authenticated user.

Parameters:

attrs – Input attributes dictionary

Returns:

Dictionary containing user data and JWT tokens

class auth_kit.serializers.login_factors.TokenResponseSerializer(*args, **kwargs)

Bases: TokenSerializer, BaseLoginResponseSerializer

DRF token authentication and user information.

validate(attrs: dict[str, Any]) dict[str, Any]

Get or create DRF token for authenticated user.

Parameters:

attrs – Input attributes dictionary

Returns:

Dictionary containing user data and authentication token

auth_kit.serializers.login_factors.get_login_response_serializer() type[BaseLoginResponseSerializer]

Get the appropriate login response serializer based on current settings.

Returns:

The appropriate response serializer class

JWT Serializers

class auth_kit.serializers.jwt.CookieTokenRefreshSerializer(*args, **kwargs)

Bases: TokenRefreshSerializer, JWTSerializer

JWT token refresh with cookie and request data support.

__init__(*args: Any, **kwargs: Any) None

Initialize password change serializer.

Parameters:
  • *args – Variable length argument list

  • **kwargs – Arbitrary keyword arguments

extract_refresh_token() str

Extract refresh token from request data or cookies.

Returns:

The refresh token string

Raises:

InvalidToken – If no valid refresh token is found

validate(attrs: dict[str, Any]) dict[str, str]

Validate the refresh token from request data or cookies.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes with refresh token

class auth_kit.serializers.jwt.JWTSerializer(*args, **kwargs)

Bases: Serializer

JWT access and refresh tokens with expiration timestamps.

Token Serializers

class auth_kit.serializers.token.TokenSerializer(*args, **kwargs)

Bases: Serializer

DRF authentication token.

User Serializers

class auth_kit.serializers.user.UserSerializer(*args, **kwargs)

Bases: ModelSerializer

User profile information and updates.

validate_username(username: str) str

Validate and clean username using allauth adapter.

Parameters:

username – Username to validate

Returns:

Cleaned username

Registration Serializers

class auth_kit.serializers.RegisterSerializer(*args, **kwargs)

Bases: Serializer

User registration with email verification.

__init__(*args: Any, **kwargs: Any) None

Initialize login serializer.

Parameters:
  • *args – Variable length argument list

  • **kwargs – Arbitrary keyword arguments

custom_signup(request: Request, user: AbstractBaseUser) None

Perform custom signup logic.

Override this method to add custom registration logic.

Parameters:
  • request – The HTTP request object

  • user – The newly created user instance

get_cleaned_data() dict[str, Any]

Get cleaned registration data.

Returns:

Dictionary of cleaned registration data

save(**kwargs: Any) AbstractBaseUser

Save the new user account.

Parameters:

**kwargs – Additional keyword arguments

Returns:

The newly created user instance

Raises:

ValidationError – If password validation fails

validate(attrs: dict[str, Any]) dict[str, Any]

Validate registration data including password confirmation.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes

Raises:

ValidationError – If passwords don’t match

validate_email(email: str) str

Validate and clean email address.

Parameters:

email – Email address to validate

Returns:

Cleaned email address

Raises:

ValidationError – If email is already registered

validate_password1(password: str) str

Validate and clean password.

Parameters:

password – Password to validate

Returns:

Cleaned password

validate_username(username: str) str

Validate and clean username.

Parameters:

username – Username to validate

Returns:

Cleaned username

class auth_kit.serializers.VerifyEmailSerializer(*args, **kwargs)

Bases: Serializer

Email address verification with confirmation key.

class auth_kit.serializers.ResendEmailVerificationSerializer(*args, **kwargs)

Bases: Serializer

Request new email verification message.

Password Serializers

class auth_kit.serializers.PasswordChangeSerializer(*args, **kwargs)

Bases: Serializer

Password change for authenticated users.

__init__(*args: Any, **kwargs: Any) None

Initialize password change serializer.

Parameters:
  • *args – Variable length argument list

  • **kwargs – Arbitrary keyword arguments

custom_validation(attrs: dict[str, Any]) None

Perform custom validation on password change data.

Override this method to add custom validation logic.

Parameters:

attrs – Attributes dictionary to validate

save(**kwargs: Any) AbstractBaseUser

Save the new password for the user.

Parameters:

**kwargs – Additional keyword arguments

Returns:

The user instance with updated password

set_password_form = None
set_password_form_class

alias of SetPasswordForm

validate(attrs: dict[str, Any]) dict[str, Any]

Validate password change data.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes

Raises:

ValidationError – If password validation fails

validate_old_password(value: str) str

Validate the old password if required.

Parameters:

value – Old password value to validate

Returns:

Validated old password

Raises:

ValidationError – If old password is incorrect

class auth_kit.serializers.PasswordResetSerializer(*args, **kwargs)

Bases: Serializer

Password reset request with email verification.

get_email_options() dict[str, Any]

Get email options for password reset.

Override this method to change default email options.

Returns:

Dictionary of email options

reset_form = None
save(**kwargs: Any) dict[str, Any]

Save password reset request and send email.

Parameters:

**kwargs – Additional keyword arguments

Returns:

Dictionary containing the email address

validate_email(value: str) str

Validate email address using password reset form.

Parameters:

value – Email address to validate

Returns:

Validated email address

Raises:

ValidationError – If email validation fails

class auth_kit.serializers.PasswordResetConfirmSerializer(*args, **kwargs)

Bases: Serializer

Password reset confirmation with new password.

custom_validation(attrs: dict[str, Any]) None

Perform custom validation on password reset data.

Override this method to add custom validation logic.

Parameters:

attrs – Attributes dictionary to validate

save(**kwargs: Any) AbstractBaseUser

Save the new password for the user.

Parameters:

**kwargs – Additional keyword arguments

Returns:

The user instance with updated password

set_password_form = None
set_password_form_class

alias of SetPasswordForm

user = None
validate(attrs: dict[str, Any]) dict[str, Any]

Validate password reset confirmation data.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes

Raises:

ValidationError – If UID or token validation fails

Logout Serializers

class auth_kit.serializers.logout.AuthKitLogoutSerializer(*args, **kwargs)

Bases: Serializer

Logout confirmation for token-based authentication.

Custom Fields

class auth_kit.serializer_fields.UnquoteStringField(*args, **kwargs)

Bases: CharField

CharField that automatically URL-decodes input values.

Useful for handling URL-encoded strings in API requests, particularly for email verification tokens and similar data.

to_internal_value(data: str) str

Convert URL-encoded string to internal representation.

Parameters:

data – URL-encoded string input

Returns:

URL-decoded string