Django
This article aims to show the basic integration of authenticating users through Defguard via OpenID Connect. So you can have a solid start to adjust it for your own use case.
The Setup
Domain
This guide assumes both Defguard and Django are running on localhost.
Defguard
We will run Defguard instance on default port 8000.
Configuration
For our example to work on localhost we will need to change the following variables in Defguard:
DEFGUARD_URL
http://localhost:8000
DEFGUARD_COOKIE_DOMAIN
localhost
DEFGUARD_COOKIE_INSECURE
true
Because we use localhost domain we need to set cookies to insecure, DON'T do this in a production environment.
Next, we need to configure the OpenID module to use RSA key instead of the default HMAC, this is due to Authlib being incompatible with HMAC.
Generate RSA key with the following command:
Now we need to set DEFGUARD_OPENID_KEY variable to path pointing to that rsakey.pem file.
When starting Defguard now you should be able to see the following info log:
Django
This section will explain how to setup a fresh Django example project.
We will use poetry as a package manager but pip will also work fine.
Project
Setup a new project with poetry, we will name it django-project.
Delete the generated django_project directory, we don't need it.
Packages
Install the following Python packages:
django
django-jazzmin
Authlib
requests
Django
Now we will make Django project and add oauth app.
With this, we should have a directory structure close to this:
Register OpenID App
We need to register our Django application as an OpenID client in Defguard.
To do that, navigate to OpenID panel and add new client as shown below.


Redirect URL should point to http://localhost:9000/oauth/redirect
Scopes should include at least OpenID, Profile, and Email.
Authentication app setup
Register app in Django
We will use the created oauth Django app to handle our authentication.
Register oauth app in settings.py file.
Views
Modify oauth/views.py file.
With the provided example, you will need to fill out only DEFGUARD_CLIENT_ID and DEFGUARD_CLIENT_SECRET.
Either provide them as environment variables or modify the views file and pass them as strings to oauth register function.
Both Client ID and Secret can be found on OpenID apps page in Defguard, click our Django app row on the list and you will be able to copy needed values from the opened modal.

URLS
We will need to add our views to oauth/urls.py.
Modify example/urls.py file, so it includes oauth app urls:
Custom admin login template
With use of Jazzmin admin theme we will modify login template and add an additional button to login with Defguard.
Register Jazzmin app
Modify example/settings.py
jazzmin app needs to be registered before django.contrib.admin
Add template file
Make templates/admin/auth/login.html file:
Register login route
Modify example/urls.py
Conclusion
Now we need to start our Django server.
If you started a fresh project don't forget to make migrations!
poetry run ./manage.py migrate
After accessing http://localhost:9000/admin we should see our custom login page
Button "Login with Defguard" should redirect us to our Defguard instance. Depending on if Defguard session is active or not we should be able to see app authorization page or login page.



When we authorize Django App to our Defguard account we are redirected back to our Django admin and logged in with a user from Defguard.
Was this helpful?