Skip to main content
Documentation Status https://img.shields.io/pypi/v/django-auth-adfs.svg https://img.shields.io/pypi/pyversions/django-auth-adfs.svg https://img.shields.io/pypi/djversions/django-auth-adfs.svg https://codecov.io/github/snok/django-auth-adfs/coverage.svg?branch=main

A Django authentication backend for Microsoft ADFS and Azure AD

Features

  • Integrates Django with Active Directory on Windows 2012 R2, 2016 or Azure AD in the cloud.

  • Provides seamless single sign on (SSO) for your Django project on intranet environments.

  • Auto creates users and adds them to Django groups based on info received from ADFS.

  • Django Rest Framework (DRF) integration: Authenticate against your API with an ADFS access token.

Installation

Python package:

pip install django-auth-adfs

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

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

INSTALLED_APPS = (
    ...
    # Needed for the ADFS redirect URI to function
    'django_auth_adfs',
    ...

# checkout the documentation for more settings
AUTH_ADFS = {
    "SERVER": "adfs.yourcompany.com",
    "CLIENT_ID": "your-configured-client-id",
    "RELYING_PARTY_ID": "your-adfs-RPT-name",
    # Make sure to read the documentation about the AUDIENCE setting
    # when you configured the identifier as a URL!
    "AUDIENCE": "microsoft:identityserver:your-RelyingPartyTrust-identifier",
    "CA_BUNDLE": "/path/to/ca-bundle.pem",
    "CLAIM_MAPPING": {"first_name": "given_name",
                      "last_name": "family_name",
                      "email": "email"},
}

# Configure django to redirect users to the right URL for login
LOGIN_URL = "django_auth_adfs:login"
LOGIN_REDIRECT_URL = "/"

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

MIDDLEWARE = (
    ...
    # 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_auth_adfs.middleware.LoginRequiredMiddleware',
)

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

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

This will add these paths to Django:

  • /oauth2/login where users are redirected to, to initiate the login with ADFS.

  • /oauth2/login_no_sso where users are redirected to, to initiate the login with ADFS but forcing a login screen.

  • /oauth2/callback where ADFS redirects back to after login. So make sure you set the redirect URI on ADFS to this.

  • /oauth2/logout which logs out the user from both Django and ADFS.

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

  • Using GET requests:

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

    <form method="post" action="{% url 'django_auth_adfs:logout' %}">
        {% csrf_token %}
        <button type="submit">Logout</button>
    </form>
    <form method="post" action="{% url 'django_auth_adfs:login' %}">
        {% csrf_token %}
        <input type="hidden" name="next" value="{{ next }}">
        <button type="submit">Login</button>
    </form>
    <form method="post" action="{% url 'django_auth_adfs: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.

Release files for django-auth-adfs 1.16.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-auth-adfs 1.16.0
File Size Uploaded
django_auth_adfs-1.16.0.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-auth-adfs 1.16.0
File Interpreter ABI Platform
django_auth_adfs-1.16.0-py3-none-any.whl Python 3 none any Details

Total release size:36.3 kB

Release files / django_auth_adfs-1.16.0.tar.gz

Download URL django_auth_adfs-1.16.0.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2af853fcfc8365c9b43dbeda909c799798ad2aaf2d23ccbddd313066f3c0707d
BLAKE2b-256 checksum
How to use checksums
4be92513662195c0a1b308ff18b8dc7ce1b45b1ee35ee2dc65fcf8fee6134e64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.9.25 Linux/6.11.0-1018-azure

Release files / django_auth_adfs-1.16.0-py3-none-any.whl

Download URL django_auth_adfs-1.16.0-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22b580ba520ce910d6abbe0c74cd06802d1b21bed7815bb048bd15f1fa5daabd
BLAKE2b-256 checksum
How to use checksums
99346fe3fec0cf68e778ac320d68fe025b4822e3374df5450c44391ffe8bdee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.9.25 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

1.16.0 This release

2 release files

1.15.1

2 release files

1.15.0

2 release files

1.14.0

2 release files

1.13.0

2 release files

1.12.0

2 release files

1.11.5

2 release files

1.11.4

2 release files

1.11.0

2 release files

1.10.1

2 release files

1.10.0

2 release files

1.9.7

2 release files

1.9.6

2 release files

1.9.5

2 release files

1.9.4

2 release files

1.9.3

2 release files

1.9.2

2 release files

1.9.1

2 release files

1.9.0

2 release files

1.8.1

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

1 release file

0.1.1

2 release files

0.1.0

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page