Django OAuth Toolkit
Django OAuth Toolkit (DOT) is a Python library that provides OAuth2 capabilities to Django projects, offering out-of-the-box endpoints, data, and logic for robust authorization. It leverages OAuthLib to ensure RFC-compliance and is currently at version 3.2.0. The project is actively maintained with regular releases, supporting recent Django and Python versions.
Warnings
- breaking Upgrading to version 3.0.0 or later requires running `manage.py migrate` due to significant changes in the `AbstractAccessToken` model. Custom swappable models based on `AbstractAccessToken` will also need to be updated and re-migrated.
- breaking Beginning with version 2.0.0, client secrets are hashed upon save. If you need the cleartext secret (e.g., for testing or specific OIDC configurations), you must copy it *before* saving an application in the Django admin. Also, `PKCE_REQUIRED` is now `True` by default, leading to 'invalid_client' errors for clients not using PKCE.
- gotcha The project transitioned from the `jazzband` GitHub organization to `django-oauth` starting with version 3.1.0. While the PyPI package name (`django-oauth-toolkit`) remains the same, this indicates a change in project governance and potentially development practices.
- gotcha If you plan to use a custom `Application` model (by setting `OAUTH2_PROVIDER_APPLICATION_MODEL` in settings), you *must* define and run the migration for your custom model *before* running the initial `oauth2_provider` migrations. Failing to do so will result in system check errors.
Install
-
pip install django-oauth-toolkit
Imports
- urls
from oauth2_provider import urls as oauth2_urls
- views
from oauth2_provider import views as oauth2_views
- AbstractApplication
from oauth2_provider.models import AbstractApplication
- OAuth2TokenMiddleware
from oauth2_provider.middleware import OAuth2TokenMiddleware
- OAuth2Backend
from oauth2_provider.backends import OAuth2Backend
Quickstart
import os
# settings.py
INSTALLED_APPS = [
# ... other apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'oauth2_provider',
'corsheaders', # If using django-cors-headers
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware', # If using django-cors-headers
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'oauth2_provider.middleware.OAuth2TokenMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
AUTHENTICATION_BACKENDS = [
'oauth2_provider.backends.OAuth2Backend',
'django.contrib.auth.backends.ModelBackend', # Required for Django admin login
]
# urls.py
from django.contrib import admin
from django.urls import include, path
from oauth2_provider import urls as oauth2_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('o/', include(oauth2_urls)),
# Your other app URLs
]
# Configure CORS if needed (e.g., for local development or specific clients)
CORS_ORIGIN_ALLOW_ALL = True # WARNING: Set to specific origins in production