Django: inloggen via OpenID Connect met mozilla-django-oidc
Inloggen met Dazr Identity: gratis OpenID Connect, gehost in de EU, met passkeys, e-mailcodes en Google- of Microsoft-accounts achter één knop.
Je hebt drie waarden uit de ontwikkelaarsconsole nodig: de client-ID, het clientgeheim (alleen voor webserver-apps) en de redirect-URI die je hebt geregistreerd. De rest staat in het discovery-document op https://identity.dazr.eu/.well-known/openid-configuration.
Django
Gebruik mozilla-django-oidc. Registreer een webserver-app met de redirect-URI https://app.example.eu/oidc/callback/. De bibliotheek leest het discovery-document niet, daarom staan de endpoints uitgeschreven.
# 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>
Standaard koppelt de backend gebruikers op e-mailadres. Om accounts aan sub te koppelen, maak je een subklasse van OIDCAuthenticationBackend en overschrijf je filter_users_by_claims en create_user. Gebruik een actuele versie: oudere versies kunnen ES256-handtekeningen niet controleren.
sub is pairwise: hetzelfde voor elke app van je organisatie, anders voor elke andere organisatie. Hij verandert nooit voor een persoon, een e-mailadres wel. Sla sub op als sleutel van het account en behandel email als contactgegeven.
Meer voor ontwikkelaars
OntwikkelaarsdocumentatieAlle integratiehandleidingenElke OpenID Connect-bibliotheekPrijzen