Skip to main content

A Django authentication backend for Microsoft Entra ID

Project description

Entra ID Authentication for Django

Attribution: This project is a fork of django-auth-adfs created by Joris Beckers and maintained by Snok.

docs PyPI version Python versions Django versions License Tests

A Django authentication backend for Microsoft Entra ID (formerly Azure AD). This project extends the foundation established by django-auth-adfs with specialized focus on Entra ID and Graph API integration.

Features

  • Integrates Django with Microsoft Entra ID (formerly Azure AD)
  • Provides seamless single sign on (SSO) for your Django project
  • Auto creates users and adds them to Django groups based on info received from Entra ID
  • Token Lifecycle Management (new): Automatically handles storing, refreshing, and encrypting access/refresh tokens
  • On-Behalf-Of Token Support (new): Simplifies access to Microsoft Graph API and other delegated access scenarios
  • Django Rest Framework (DRF) integration: Authenticate against your API with an Entra ID access token

While building on the original django-auth-adfs, we've focused specifically on the Entra ID authentication scenario with enhancements for token management, storing them so that your application can securely and seamlessly delegate access to Graph API on behalf of your users.

Installation

Python package:

pip install django-entra-auth

In your project's settings.py add these settings.

AUTHENTICATION_BACKENDS = (
    ...
    'django_entra_auth.backend.AdfsAuthCodeBackend',
    ...
)

INSTALLED_APPS = (
    ...
    # Needed for the auth redirect URI and static files to function
    'django_entra_auth',
    ...
)

# Basic configuration for Entra ID
# checkout the documentation for more settings
ENTRA_AUTH = {
    # Optional. Defaults to "login.microsoftonline.com".
    # Override for national clouds or compatible Entra ID hosts.
    "SERVER": "login.microsoftonline.com",
    "TENANT_ID": "your-tenant-id",
    "CLIENT_ID": "your-application-client-id",
    "RELYING_PARTY_ID": "your-application-client-id", # Often same as CLIENT_ID for Entra ID
    # OIDC Audience ("aud" claim). For Entra ID, CLIENT_ID
    "AUDIENCE": "your-application-client-id",
    # Set to False for Entra ID. Provide path for ADFS.
    "CA_BUNDLE": False,
    "CLAIM_MAPPING": {"first_name": "given_name",
                      "last_name": "family_name",
                      "email": "email"}, # Adjust based on claims from your provider
    # See documentation for TokenLifecycleMiddleware settings like:
    # "TOKEN_REFRESH_THRESHOLD", "STORE_OBO_TOKEN", "TOKEN_ENCRYPTION_SALT",
    # "LOGOUT_ON_TOKEN_REFRESH_FAILURE"
}

# Configure django to redirect users to the right URL for login
LOGIN_URL = "django_entra_auth:login"
LOGIN_REDIRECT_URL = "/" # Or wherever users should land after login

########################
# OPTIONAL SETTINGS
########################

MIDDLEWARE = (
    ...
    # Optional: Automatically manage access/refresh/OBO tokens in the session
    # Must be AFTER SessionMiddleware and AuthenticationMiddleware
    'django_entra_auth.middleware.TokenLifecycleMiddleware',
    # With this you can force a user to login without using
    # the LoginRequiredMixin on every view class
    #
    # You can specify URLs for which login is not enforced by
    # specifying them in the LOGIN_EXEMPT_URLS setting.
    'django_entra_auth.middleware.LoginRequiredMiddleware',
)

# Specify URLs exempt from LoginRequiredMiddleware (if used)
# LOGIN_EXEMPT_URLS = (
#     r'^/about/.*$',
#     r'^/legal/.*$',
# )

In your project's urls.py add these paths:

from django.urls import path, include

urlpatterns = [
    ...
    path('oauth2/', include('django_entra_auth.urls')),
]

This will add these paths to Django:

  • /oauth2/login where users are redirected to, to initiate the login with the identity provider.
  • /oauth2/login_no_sso where users are redirected to, but forcing a login screen.
  • /oauth2/callback where the identity provider redirects back to after login. Ensure your redirect URI in Entra ID/ADFS is set to this.
  • /oauth2/logout which logs out the user from both Django and the identity provider (if supported by provider).

Below is sample Django template code to use these paths depending if you'd like to use GET or POST requests. Logging out via GET was deprecated in Django 4.1.

  • Using GET requests (Login only):

    {# Logout requires POST #}
    <a href="{% url 'django_entra_auth:login' %}">Login</a>
    <a href="{% url 'django_entra_auth:login-no-sso' %}">Login (no SSO)</a>
    
  • Using POST requests:

    <form method="post" action="{% url 'django_entra_auth:logout' %}">
        {% csrf_token %}
        <button type="submit">Logout</button>
    </form>
    <form method="post" action="{% url 'django_entra_auth:login' %}">
        {% csrf_token %}
        <input type="hidden" name="next" value="{{ next }}">
        <button type="submit">Login</button>
    </form>
    <form method="post" action="{% url 'django_entra_auth:login-no-sso' %}">
        {% csrf_token %}
        <input type="hidden" name="next" value="{{ next }}">
        <button type="submit">Login (no SSO)</button>
    </form>
    

Contributing

Contributions to the code are more then welcome. For more details have a look at the CONTRIBUTING.rst file.

Download files

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

Source Distribution

django_entra_auth-1.16.1.tar.gz (4.1 MB view details)

Uploaded Source

Built Distribution

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

django_entra_auth-1.16.1-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file django_entra_auth-1.16.1.tar.gz.

File metadata

  • Download URL: django_entra_auth-1.16.1.tar.gz
  • Upload date:
  • Size: 4.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for django_entra_auth-1.16.1.tar.gz
Algorithm Hash digest
SHA256 4286e103153560044077eaf1329669bc2156835dc9338383a98f14e284dc523e
MD5 04d22ddc98b15ed533af39c12764de7d
BLAKE2b-256 8e5e49ec6c72a8895e0e85e6c580c5b7f6485d2ae4ba85df6d8c8eacb3acf549

See more details on using hashes here.

File details

Details for the file django_entra_auth-1.16.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_entra_auth-1.16.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11df2aac34b2423edd1f9c358cc1e9e8926805ff49712eb9e221cd232270f100
MD5 17da9298358aca79f64d82b17e19ebdc
BLAKE2b-256 10c788face54a46e6e9f6a554fb06083092436ed02fd0729c45dd030183e7d3f

See more details on using hashes here.

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