Django OAuth Toolkit

3.2.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

To quickly set up Django OAuth Toolkit, first install it along with `django-cors-headers` (if needed for cross-origin requests). Add `oauth2_provider` and `corsheaders` to `INSTALLED_APPS`. Configure `MIDDLEWARE` to include `OAuth2TokenMiddleware` and `CorsMiddleware`. Add `OAuth2Backend` to `AUTHENTICATION_BACKENDS`. Finally, include `oauth2_provider` URLs in your project's `urls.py`. Remember to run `python manage.py makemigrations` and `python manage.py migrate` to apply database changes. After migration, you can register OAuth2 applications via the Django admin at `/o/applications/`.

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

view raw JSON →