Skip to main content

Overview

The allauth.idp.oidc package provides a production-ready OpenID Connect (OIDC) provider for your Django application. It allows other applications to authenticate users against your Django app using the industry-standard OIDC protocol.

Supported Grant Types

The OIDC provider supports the following OAuth 2.0 grant types:
  • Authorization Code Grant: The recommended flow for web applications with a backend
  • Client Credentials Grant: For server-to-server authentication without user interaction
  • Implicit Grant: Legacy flow for single-page applications (not recommended for new applications)
  • Device Authorization Grant: For devices with limited input capabilities (TVs, IoT devices, etc.)
  • Refresh Token Grant: For obtaining new access tokens without re-authenticating the user
Password grant is intentionally not supported as it’s a legacy flow that lacks support for modern security features like MFA.

Installation

1. Install Dependencies

Install django-allauth with the OpenID Connect IDP extra:

2. Configure Django Settings

First, ensure you have allauth.account configured in your project. Then add the OIDC provider app to your INSTALLED_APPS:
settings.py

3. Generate Private Key

The OIDC provider requires a private key for signing tokens. Generate one using OpenSSL:
Then add the private key to your settings:
settings.py
Never commit your private key to version control. Use environment variables or a secure secrets management system in production.

4. Configure URLs

Add the IDP URLs to your project’s urls.py:
urls.py
This will enable the following endpoints:
  • /.well-known/openid-configuration - Provider configuration metadata
  • /.well-known/jwks.json - JSON Web Key Set for token verification
  • /identity/o/authorize - Authorization endpoint
  • /identity/o/api/token - Token endpoint
  • /identity/o/api/userinfo - User information endpoint
  • /identity/o/api/revoke - Token revocation endpoint
  • /identity/o/device - Device authorization endpoint
  • /identity/o/logout - RP-initiated logout endpoint

5. Run Migrations

Apply the database migrations to create the necessary tables:

Configuration

Customize the OIDC provider behavior with these settings:

Token Settings

settings.py

Device Flow Settings

settings.py

Adapter Settings

settings.py

Rate Limiting

settings.py

Logout Settings

settings.py

Custom UserInfo Endpoint

settings.py

Managing Clients

OAuth clients represent third-party applications that want to authenticate users through your identity provider.

Creating Clients via Django Admin

  1. Go to the Django admin interface
  2. Navigate to IDP OIDCClients
  3. Click Add Client
  4. Configure the client settings

Client Configuration

Here’s what each field means: Name : The display name shown to users during authorization ID : Automatically generated client identifier Secret : Automatically generated and shown only once at creation (for confidential clients) Type :
  • Confidential: Can securely store secrets (backend applications)
  • Public: Cannot store secrets securely (SPAs, mobile apps)
Scopes : Allowed scopes for this client (one per line)
Default Scopes : Scopes granted when the client doesn’t specify any Grant Types : Allowed OAuth 2.0 grant types (one per line)
Response Types : Allowed response types (one per line)
Redirect URIs : Allowed callback URLs after authentication (one per line)
CORS Origins : Allowed origins for cross-origin requests (one per line)
Allow URI Wildcards : Enable wildcard matching in redirect URIs and CORS origins
Skip Consent : Automatically grant all requested scopes without user confirmation (for trusted first-party apps)

Example Client Configuration

OAuth 2.0 Flows

Authorization Code Flow

The most common and secure flow for web applications:
  1. Initiate Authorization: Redirect user to authorization endpoint
  1. User Authentication: User logs in and grants consent
  2. Authorization Code: User is redirected back with a code
  1. Token Exchange: Exchange code for tokens
  1. Response: Receive tokens

Client Credentials Flow

For server-to-server authentication without user interaction:

Refresh Token Flow

Obtain a new access token using a refresh token:

Device Authorization Flow

For devices with limited input capabilities:
  1. Request Device Code:
Response:
  1. User Enters Code: Display the user_code and verification_uri to the user
  2. Poll for Token:

Using Tokens

Accessing User Information

Use the access token to fetch user information:
Response:

Revoking Tokens

Revoke an access or refresh token:

Customizing the Adapter

Create a custom adapter to modify default behavior:
myapp/adapters.py
Configure Django to use your custom adapter:
settings.py

API Integration

django-allauth provides built-in authentication for popular API frameworks.

Django REST Framework

Django Ninja

Discovery Endpoints

OIDC providers expose discovery endpoints that clients can use to automatically configure themselves:

Provider Configuration

Response:

JWKS Endpoint

Response:

Production Checklist

  • Use HTTPS in production
  • Store private keys securely (environment variables, secrets manager)
  • Enable CSRF protection
  • Validate all redirect URIs
  • Implement rate limiting
  • Enable token rotation
  • Use short-lived access tokens
  • Monitor for suspicious activity
  • Set appropriate token expiration times
  • Configure CORS origins properly
  • Set up proper redirect URIs
  • Enable rate limiting
  • Configure session settings
  • Set up proper cookie domains
  • Use a reverse proxy (nginx, Apache)
  • Configure SSL/TLS properly
  • Set up database backups
  • Configure logging and monitoring
  • Set up error tracking
  • Plan for key rotation

Troubleshooting

Common Issues

“invalid_client” error Ensure the client ID and secret are correct and the client exists in the database. “redirect_uri_mismatch” error The redirect URI in the request must exactly match one of the URIs configured for the client. “invalid_scope” error The requested scopes must be a subset of the scopes allowed for the client. Token signature validation fails Verify that the JWKS endpoint is accessible and the private key is correctly configured. CORS errors Add the client’s origin to the CORS origins list for the OAuth client.

Next Steps

Adapter Reference

Learn about all available adapter methods

Example Implementations

See complete examples of IDP implementations