Skip to main content

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)
  • Google
  • Facebook
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_auth_providers-2.0.4.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_auth_providers-2.0.4-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

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

Hashes for django_auth_providers-2.0.4.tar.gz
Algorithm Hash digest
SHA256 5b719eab337fe8a820fb32085f3206647a6868aef74d5f7931e025929546f943
MD5 335513623c700046b3bc9c9d689741c6
BLAKE2b-256 adb5aae53996358b1f0cc96f3fc68d94ced44c611505e121b0543128914f35b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_auth_providers-2.0.4.tar.gz:

Publisher: publish.yml on pettermoe95/django-ext-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_auth_providers-2.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auth_providers-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 33053c732193d3187f41f79e6e8922e869f83837eabd72d36d96f320f42e46ac
MD5 be72abfb575450e3bda238305adca55c
BLAKE2b-256 58819e0664e320b19b8ae84a55e6ff772b554292fbd93110f092a77c7145cda9

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page