Social Authentication

Social authentication integration with Django Allauth.

Views

Login Views

class auth_kit.social.views.login.SocialLoginView(**kwargs: Any)

Bases: LoginView

API view for social authentication login.

Handles OAuth-based authentication flows for social providers. Extends the base LoginView to provide social-specific functionality.

__init__(**kwargs: Any) None

Initialize the social login view.

Parameters:

**kwargs – Arbitrary keyword arguments

Raises:

ValueError – If adapter_class is not defined on the view

adapter_class: type[OAuth2Adapter]
get_serializer_class() type[Serializer]

Get the serializer class for social login.

Returns:

The dynamically generated social login serializer class

post(request: Request, *args: Any, **kwargs: Any) Response

Authenticate via social provider OAuth token.

Parameters:
  • request – The HTTP request containing OAuth authorization code/token

  • *args – Variable length argument list

  • **kwargs – Arbitrary keyword arguments

Returns:

HTTP response with user details and authentication tokens

Connection Views

class auth_kit.social.views.connect.SocialConnectView(**kwargs)

Bases: GenericAPIView

API view for connecting social accounts to existing user accounts.

Allows authenticated users to link their account with social authentication providers like Google, Facebook, GitHub, etc.

get_serializer_class() type[SocialConnectSerializer]

Get the serializer class for social account connection.

Returns:

The SocialConnectSerializer class

permission_classes = (<class 'rest_framework.permissions.IsAuthenticated'>,)
post(request: Request, *args: Any, **kwargs: Any) Response

Connect a social account to the current user’s account.

Parameters:
  • request – The HTTP request containing OAuth authorization code

  • *args – Variable length argument list

  • **kwargs – Arbitrary keyword arguments

Returns:

HTTP response confirming successful account connection

Account Management Views

class auth_kit.social.views.account.SocialAccountViewSet(**kwargs)

Bases: ListModelMixin, DestroyModelMixin, GenericViewSet

ViewSet for managing social account connections.

Provides endpoints for authenticated users to: - List their connected social accounts - Remove/disconnect social accounts

destroy(request, *args, **kwargs)
get_queryset() QuerySet

Get social accounts for the current authenticated user.

Returns:

QuerySet of SocialAccount objects for the current user

list(request, *args, **kwargs)
perform_destroy(instance: SocialAccount) None

Handle social account disconnection.

Sends the social_account_removed signal before deleting the account to notify other parts of the application about the disconnection.

Parameters:

instance – The SocialAccount instance to remove

permission_classes = (<class 'rest_framework.permissions.IsAuthenticated'>,)
serializer_class

alias of SocialAccountSerializer

UI Views

Serializers

Login Serializers

auth_kit.social.serializers.login.get_social_login_serializer(provider_name: str = '') type[Serializer]

Get the social login serializer class based on current settings.

Creates a serializer class dynamically by combining the appropriate request and response serializers based on current auth kit settings.

Parameters:

provider_name – The name of the social provider (e.g., “Google”, “Github”)

Returns:

The combined social login serializer class

class auth_kit.social.serializers.login.SocialLoginWithTokenRequestSerializer(*args, **kwargs)

Bases: Serializer

Base serializer for social login using access tokens.

Handles OAuth flows where the client already has an access token from the social provider (e.g., from client-side OAuth flows).

check_social_login_account(login: SocialLogin) None

Validate the social login account before processing.

Checks if a user with the same email already exists and handles auto-connection based on configuration settings.

Parameters:

login – The SocialLogin instance to validate

Raises:

ValidationError – If user already exists and auto-connect is disabled

get_login_from_token(tokens_to_parse: dict[str, Any]) SocialLogin

Create a SocialLogin from OAuth tokens.

Parameters:

tokens_to_parse – Dictionary containing OAuth tokens

Returns:

SocialLogin instance for the authenticated user

get_social_login(adapter: Any, app: SocialApp, token: SocialToken, response: dict[str, Any]) SocialLogin

Create a SocialLogin instance from the OAuth token.

Parameters:
  • adapter – The OAuth adapter for the provider

  • app – The social application configuration

  • token – The OAuth token

  • response – Additional OAuth response data

Returns:

SocialLogin instance for the authenticated user

Raises:

ValidationError – If token validation or user info retrieval fails

handle_social_login(request: Request, login: SocialLogin) None

Process the social login and handle account connection.

Parameters:
  • request – The DRF request object

  • login – The SocialLogin instance to process

set_login_user(login: SocialLogin) None

Set the appropriate user for the social login.

If a user with the same email exists, use that user. Otherwise, ensure the new user has a proper username.

Parameters:

login – The SocialLogin instance to configure

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

Validate the social login with access token.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes dictionary

class auth_kit.social.serializers.login.SocialLoginWithCodeRequestSerializer(*args, **kwargs)

Bases: SocialLoginWithTokenRequestSerializer

Serializer for social login using OAuth authorization codes.

Handles the standard OAuth authorization code flow where the client receives an authorization code and exchanges it for access tokens.

access_token = None
get_callback_url(request: Request, view: APIView, social_app: SocialApp) str

Get the OAuth callback URL for this login flow.

Parameters:
  • request – The DRF request object

  • view – The API view handling the request

  • social_app – The social application configuration

Returns:

OAuth callback URL for social login

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

Validate the social login with authorization code.

Exchanges the authorization code for access tokens and processes the social login.

Parameters:

attrs – Input attributes dictionary

Returns:

Validated attributes dictionary

Raises:

ValidationError – If code exchange or login processing fails

Account Serializers

class auth_kit.social.serializers.account.SocialAccountSerializer(*args, **kwargs)

Bases: ModelSerializer

Serializer for SocialAccount instances.

Provides a REST API representation of django-allauth SocialAccount objects, including provider information and connection metadata.

Connection Serializers

class auth_kit.social.serializers.connect.SocialConnectSerializer(*args, **kwargs)

Bases: SocialLoginWithCodeRequestSerializer

Serializer for connecting social accounts to existing user accounts.

Handles OAuth authorization code exchange and connects the social account to the currently authenticated user’s account.

check_social_login_account(login: SocialLogin) None

Validate social account before connection.

Enforces email matching if configured to ensure the social account email matches the current user’s email address.

Parameters:

login – The SocialLogin instance to validate

Raises:

ValidationError – If email validation fails

get_callback_url(request: Request, view: APIView, social_app: SocialApp) str

Get the OAuth callback URL for account connection.

Parameters:
  • request – The DRF request object

  • view – The API view handling the request

  • social_app – The social application configuration

Returns:

OAuth callback URL for social account connection

set_login_user(login: SocialLogin) None

Set the user for the social login to the current authenticated user.

Parameters:

login – The SocialLogin instance to configure

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

Validate the social account connection request.

Parameters:

attrs – Input attributes dictionary

Returns:

Dictionary containing success message

Utilities

auth_kit.social.utils.normalize_app_name(social_app: SocialApp) tuple[str, str]

Normalize social app name into clean app_name and app_slug.

Parameters:

social_app – The social application configuration

Returns:

  • app_name: PascalCase name with spaces removed

  • app_slug: lowercase name with dashes converted to underscores

Return type:

Tuple of (app_name, app_slug) where

auth_kit.social.utils.get_social_login_callback_url(request: Request, view: APIView | None, social_app: SocialApp) str

Generate OAuth callback URL for social login workflow.

Parameters:
  • request – The DRF request object

  • view – The API view handling the request (optional)

  • social_app – The social application configuration

Returns:

Complete OAuth callback URL for social login

auth_kit.social.utils.get_social_connect_callback_url(request: Request, view: APIView | None, social_app: SocialApp) str

Generate OAuth callback URL for social account connection workflow.

Parameters:
  • request – The DRF request object

  • view – The API view handling the request (optional)

  • social_app – The social application configuration

Returns:

Complete OAuth callback URL for social account connection