django-auth-providers is a django app that lets you authenticate to external authentication providers
Project description
django-ext-auth
Django Ext Auth is a package that lets you integrate external authentication providers into your django project.
Overview
If you want to enable authentication against against Azure AD, Google and more, fully compatible with the django authentication backend and session system, this is the package for you.
Supported providers
- Azure AD (single tenant)
- Azure AD (multi tenant)
- Vipps
- BankID
Requirements
Python 3.12, 3.11, 3.10, 3.9 Django >= 5.1
I have not tested lower versions of django and python, so it might be compatible with more versions.
Installation
This is not yet available in pip, so you need to manually clone the repo/download this package.
Place the package in your django project, side by side with your other django apps.
Installed Apps
Add ´ext_auth´ to your INSTALLED_APPS in your settings.py:
INSTALLED_APPS = [
...
'ext_auth',
...
]
Middleware
It is also important to add the ´access_token_middleware´, somewhere after the Session and Authentication middleware:
MIDDLEWARE = [
...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'ext_auth.middleware.tokens.access_token_middleware', < -----
]
If it is not after the AuthenticationMiddleware, it won't be able to initiate authentication properly
AzureADBackend
Now add the AzureADBackend to your AUTHENTICATION_BACKENDS:
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'ext_auth.backends.AzureADBackend'
]
Urls
ext_auth comes with a sign_in view. For it to work you need to include the urls and set LOGIN_URL. setting the urls:
urlpatterns = [
...
path('admin', admin_site.urls),
path('auth/', include('ext_auth.urls')),
...
]
set the LOGIN_URL in settings.py:
LOGIN_URL = '/auth/signin'
Secrets
Finally we need to set some values in the django settings to be able to contact your provider and complete authentications:
Azure AD
EXT_AUTH_AAD_CLIENT_ID = 'XXXXX-XXXXX-XXXXX-XXXXXX' # The ´Client ID´ for your Azure AD App Registration
EXT_AUTH_AAD_TENANT_ID = 'XXXXX-XXXXX-XXXXX-XXXXXX' # Your Azure AD ´Tenant ID´
EXT_AUTH_AAD_AUTH_AUTHORITY = f"https://login.microsoftonline.com/{EXT_AUTH_AAD_TENANT_ID}" # For single tenant
EXT_AUTH_AAD_REDIRECT_URI = '/auth/callback' # Should be the path to you callback view
EXT_AUTH_AAD_CLIENT_SECRET = XXXXXXXXXXXXXXXXXXXXX # The client secret from your Azure App Registration
EXT_AUTH_POST_LOGIN_REDIRECT_URI = '/home' # The url that the user will be sent back to after auth is finished
EXT_AUTH_AAD_SCOPES = ["user.read"] # The scoped permissions you want your user to have.
Add given_name/family_name
In Azure, update the app registration to contain optional claims in the ID token. This will make the user object in the database contain the values. Or else use extra scopes and use the access token to request further information about the user from the Graph API.
Migration from v1 to v2
The v1 used the userPrincipalName as username, but from version 2 it will be the oid claim in the id_token. The oid claim in the same across applications for the same tenant in Entra ID. It means it will be able to identify the user even if you log in to different applications.
Automatic migration
The package automatically migrates old users to the new username setup. It will check if the user already exists with email as username, then update it to avoid duplicate users. This will not work for users from external tenants, so it should be handled manually.
Manual migration for external users
To do a manual migration I suggest to to an update sql statement. Once the users have logged in using the new system, it should've created another user record in the database with the oid as username instead of userPrincipalName. The new user will have email set, so you could update the old user with the oid, then delete the new user account.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_auth_providers-2.0.4.tar.gz.
File metadata
- Download URL: django_auth_providers-2.0.4.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b719eab337fe8a820fb32085f3206647a6868aef74d5f7931e025929546f943
|
|
| MD5 |
335513623c700046b3bc9c9d689741c6
|
|
| BLAKE2b-256 |
adb5aae53996358b1f0cc96f3fc68d94ced44c611505e121b0543128914f35b9
|
Provenance
The following attestation bundles were made for django_auth_providers-2.0.4.tar.gz:
Publisher:
publish.yml on pettermoe95/django-ext-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_auth_providers-2.0.4.tar.gz -
Subject digest:
5b719eab337fe8a820fb32085f3206647a6868aef74d5f7931e025929546f943 - Sigstore transparency entry: 157637121
- Sigstore integration time:
-
Permalink:
pettermoe95/django-ext-auth@40a583ecda01bfc97079a679cbedb6bb2844f848 -
Branch / Tag:
refs/tags/release/v2.0.4 - Owner: https://github.com/pettermoe95
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@40a583ecda01bfc97079a679cbedb6bb2844f848 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_auth_providers-2.0.4-py3-none-any.whl.
File metadata
- Download URL: django_auth_providers-2.0.4-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33053c732193d3187f41f79e6e8922e869f83837eabd72d36d96f320f42e46ac
|
|
| MD5 |
be72abfb575450e3bda238305adca55c
|
|
| BLAKE2b-256 |
58819e0664e320b19b8ae84a55e6ff772b554292fbd93110f092a77c7145cda9
|
Provenance
The following attestation bundles were made for django_auth_providers-2.0.4-py3-none-any.whl:
Publisher:
publish.yml on pettermoe95/django-ext-auth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_auth_providers-2.0.4-py3-none-any.whl -
Subject digest:
33053c732193d3187f41f79e6e8922e869f83837eabd72d36d96f320f42e46ac - Sigstore transparency entry: 157637122
- Sigstore integration time:
-
Permalink:
pettermoe95/django-ext-auth@40a583ecda01bfc97079a679cbedb6bb2844f848 -
Branch / Tag:
refs/tags/release/v2.0.4 - Owner: https://github.com/pettermoe95
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@40a583ecda01bfc97079a679cbedb6bb2844f848 -
Trigger Event:
release
-
Statement type: