Thanks to @kgtechs for the youtube walkthrough of the django_auth_adfs install docs this readme and repo is a reference for these materials.
You can follow along with this readme and the youtube referenced above, or you can just clone the repo run the install instructions and config ur client settings in the .env file
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
$ conda --version
conda 24.5.0
$ python --version
Python 3.13.0
$ python -m django --version
5.1.3
my mostly vanilla conda install did not have pip installed so i also had to include that in my installation.
conda install pip
mkdir django_auth_example
cd django_auth_example/
conda create -n denv
conda activate denv
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install django --channel conda-forge
pip install django-auth-adfs
pip install python-dotenv
django-admin startproject azurelogin .
after install and running django admin setup you will have the following in your folder:
$ls
azurelogin manage.py
$ls azurelogin
asgi.py __init__.py settings.py urls.py wsgi.py
- Per django_auth_adfs install docs
Edit yoursettings.py
as follows- insert the
AUTHENTICATION_BACKENDS
,INSTALLED_APPS
,MIDDLEWARE
settings - insert
LOGIN_URL
andLOGIN_REDIRECT_URL
- also edit your
urls.py
- edit the djanog import with include
from django.urls import path,include
- insert the
- Update your
urls.py
to include newurlpatterns
- Create
.env
file in yourazurelogin
folderclient_id = your_client_id client_secret = your_client_secret tenant_id = you_tenant_id
- Azure Config
django_auth_adfs azure ad config guide
- Step 1 - Register a backend application
- login to azure ensure you are in the right AD directory for me i tested with Default Directory and configure the application updating
.env
file created above with your information - Step 2 - Configuring settings.py
- update
settings.py
- insert
AUTHENTICATION_BACKENDS
per documentation - insert needed imports and variables as follows
#imports from dotenv import load_dotenv import os load_dotenv() #..... client_id = os.getenv('client_id') client_secret = os.getenv('client_secret') tenant_id = os.getenv('tenant_id') #.....
- insert
- insert
AUTH_ADFS
as follows:'*'noteAUTH_ADFS = { 'AUDIENCE': client_id, 'CLIENT_ID': client_id, 'CLIENT_SECRET': client_secret, 'CLAIM_MAPPING': {'first_name': 'given_name', 'last_name': 'family_name', 'email': 'email'}, 'USERNAME_CLAIM': 'given_name', 'TENANT_ID': tenant_id, 'RELYING_PARTY_ID': client_id, }
GROUPS_CLAIM
andMIRROR_GROUPS
removed as groups was not provided my my AzureUSERNAME_CLAIM
altered to usegiven_name
asupn
was not provided my my Azure - Update
urls.py
to include newurlpatterns
- update
- Step 3 - Register and configure an Azure AD frontend application
- Middleware Config django_auth_adfs install docs Login Middleware
- update
settings.py
with newMIDDLEWARE
- OPTIONAL: if desired may update
AUTH_ADFS
to also includeLOGIN_EXEMPT_URLS
- start (create??) app
$ python manage.py startapp app
- add
app
toINSTALLED_APPS
insettings.py
- create
app\urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.login_successful, name='login-view'), ]
- create
app\views.py
from django.shortcuts import render from django.http import HttpResponse,request # Create your views here. def login_successful(request): if request.user.is_authenticated: return HttpResponse(f"200 ok welcome:{request.user.username}") else: return HttpResponse(f"403 ")
- update
urls.py
with newurlpatters
path('', include('app.urls')),
- Optional: Debug loggin
- update
settings.py
with the followign:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(name)s %(message)s'
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
},
'loggers': {
'django_auth_adfs': {
'handlers': ['console'],
'level': 'DEBUG',
},
},
}
- Migrations
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
#runserver with warnings enables
#python -Wd manage.py runserver
browse to http://127.0.0.1:8000/