Django: OpenID-Connect-Anmeldung mit mozilla-django-oidc

Sie brauchen drei Werte aus der Entwicklerkonsole: die Client-ID, das Client-Secret (nur für Webserver-Apps) und die registrierte Weiterleitungs-URI. Alles Weitere steht im Discovery-Dokument unter https://identity.dazr.eu/.well-known/openid-configuration.

Django

Verwenden Sie mozilla-django-oidc. Registrieren Sie eine Webserver-App mit der Weiterleitungs-URI https://app.example.eu/oidc/callback/. Die Bibliothek liest das Discovery-Dokument nicht, deshalb sind die Endpunkte ausgeschrieben.

# settings.py
import os

INSTALLED_APPS += ["mozilla_django_oidc"]  # after django.contrib.auth
AUTHENTICATION_BACKENDS = [
    "mozilla_django_oidc.auth.OIDCAuthenticationBackend",
    "django.contrib.auth.backends.ModelBackend",
]

OIDC_RP_CLIENT_ID = os.environ["DAZR_CLIENT_ID"]
OIDC_RP_CLIENT_SECRET = os.environ["DAZR_CLIENT_SECRET"]
OIDC_RP_SCOPES = "openid profile email"
OIDC_RP_SIGN_ALGO = "ES256"
OIDC_OP_JWKS_ENDPOINT = "https://identity.dazr.eu/oauth/jwks"
OIDC_OP_AUTHORIZATION_ENDPOINT = "https://identity.dazr.eu/oauth/authorize"
OIDC_OP_TOKEN_ENDPOINT = "https://identity.dazr.eu/oauth/token"
OIDC_OP_USER_ENDPOINT = "https://identity.dazr.eu/oauth/userinfo"
OIDC_USE_PKCE = True
OIDC_PKCE_CODE_CHALLENGE_METHOD = "S256"

LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"

# urls.py
from django.urls import include, path
urlpatterns += [path("oidc/", include("mozilla_django_oidc.urls"))]

# template: start the sign-in
# <a href="{% url 'oidc_authentication_init' %}">Sign in with Dazr Identity</a>

Standardmäßig ordnet das Backend Konten über die E-Mail zu. Um Konten über sub zu verknüpfen, leiten Sie von OIDCAuthenticationBackend ab und überschreiben filter_users_by_claims und create_user. Verwenden Sie eine aktuelle Version: Ältere Versionen können ES256-Signaturen nicht prüfen.

Mehr für Entwickler