Mozilla Django OIDC

5.0.2 · active · verified Sun Apr 12

mozilla-django-oidc is a lightweight authentication and access management library for integrating Django applications with OpenID Connect enabled authentication services. It is actively maintained with frequent updates, currently at version 5.0.2, and typically releases new versions to support new Django and Python versions.

Warnings

Install

Imports

Quickstart

This quickstart outlines the essential `settings.py` and `urls.py` configurations. You must add `mozilla_django_oidc` to `INSTALLED_APPS` and include its `OIDCAuthenticationBackend` in `AUTHENTICATION_BACKENDS`. Critical OIDC provider (OP) and relying party (RP) details (`OIDC_OP_*`, `OIDC_RP_CLIENT_ID`, `OIDC_RP_CLIENT_SECRET`) must be provided, ideally via environment variables for security. The library's URLs are included via `path('oidc/', include('mozilla_django_oidc.urls'))`. Basic login and logout links can then be added to your templates.

# settings.py
import os

INSTALLED_APPS = [
    # ...
    'django.contrib.auth',
    'mozilla_django_oidc',
    # ...
]

AUTHENTICATION_BACKENDS = (
    'mozilla_django_oidc.auth.OIDCAuthenticationBackend',
    'django.contrib.auth.backends.ModelBackend',
)

# OpenID Connect Provider (OP) settings - REQUIRED
OIDC_OP_AUTHORIZATION_ENDPOINT = os.environ.get('OIDC_OP_AUTHORIZATION_ENDPOINT', 'https://your-op.com/auth')
OIDC_OP_TOKEN_ENDPOINT = os.environ.get('OIDC_OP_TOKEN_ENDPOINT', 'https://your-op.com/token')
OIDC_OP_USER_ENDPOINT = os.environ.get('OIDC_OP_USER_ENDPOINT', 'https://your-op.com/userinfo')
OIDC_OP_JWKS_ENDPOINT = os.environ.get('OIDC_OP_JWKS_ENDPOINT', 'https://your-op.com/jwks')

# Relying Party (RP) / Client settings - REQUIRED
OIDC_RP_CLIENT_ID = os.environ.get('OIDC_RP_CLIENT_ID', 'your-client-id')
OIDC_RP_CLIENT_SECRET = os.environ.get('OIDC_RP_CLIENT_SECRET', 'your-client-secret')

# Optional settings for redirection after login/logout
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

# urls.py (in your project's root urls.py)
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('oidc/', include('mozilla_django_oidc.urls')),
    # Your other app URLs
    path('', lambda request: HttpResponse("Welcome! <a href='/oidc/authenticate/'>Login</a> or <a href='/oidc/logout/'>Logout</a>"), name='home'),
]

# In a simple template (e.g., base.html) add login/logout links:
# {% if user.is_authenticated %}
#   <p>Hello, {{ user.username }}!</p>
#   <a href="{% url 'oidc_logout' %}">Log Out</a>
# {% else %}
#   <a href="{% url 'oidc_authentication_init' %}">Log In with OIDC</a>
# {% endif %}

view raw JSON →