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.

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.3.tar.gz (11.5 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.3-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file django_auth_providers-2.0.3.tar.gz.

File metadata

  • Download URL: django_auth_providers-2.0.3.tar.gz
  • Upload date:
  • Size: 11.5 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.3.tar.gz
Algorithm Hash digest
SHA256 55c8a1770e02f2df723b2aa6bdf469313fd723a1349c584086252f81ff55ebe1
MD5 f7946f16bf24f3192e98f7347eabc98c
BLAKE2b-256 b2c61eff6f4f9db772ab2a71ffed0040dbae381a88e19e026eeb119323a717e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_auth_providers-2.0.3.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.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_auth_providers-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d07c3b9355c18c8de55250a7cc629cf663980304a77c6ae5aedbf811a4e2f85b
MD5 e649a36197b5ffa793fb6a939ab338e5
BLAKE2b-256 1d19ef6afc116dffa691d95916d1e8fff62282930c5980c7ad07bc7784e233a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_auth_providers-2.0.3-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