Overview
Theallauth.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 haveallauth.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:settings.py
4. Configure URLs
Add the IDP URLs to your project’surls.py:
urls.py
/.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
- Go to the Django admin interface
- Navigate to IDP OIDC → Clients
- Click Add Client
- 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)
Example Client Configuration
OAuth 2.0 Flows
Authorization Code Flow
The most common and secure flow for web applications:- Initiate Authorization: Redirect user to authorization endpoint
- User Authentication: User logs in and grants consent
- Authorization Code: User is redirected back with a code
- Token Exchange: Exchange code for tokens
- 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:- Request Device Code:
-
User Enters Code: Display the
user_codeandverification_urito the user - Poll for Token:
Using Tokens
Accessing User Information
Use the access token to fetch user information:Revoking Tokens
Revoke an access or refresh token:Customizing the Adapter
Create a custom adapter to modify default behavior:myapp/adapters.py
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
JWKS Endpoint
Production Checklist
Security
Security
- 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
Configuration
Configuration
- 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
Infrastructure
Infrastructure
- 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
