Skip to main content

A Django middleware that adds 2-factor authentication via Duo.

django-duo-auth is designed to be easily integrated into an existing Django project to quickly add 2-factor authentication. It supports one or more Duo applications and uses the configured AUTHENTICATION_BACKENDS to select which users it should authenticate under which circumstance.

Installation

django-duo-auth can be installed from PyPI, and depends on duo-universal.

?> pip install django-duo-auth

If you wish to use the traditional iframe-based prompt, install like this instead

?> pip install django-duo-auth[legacy]

Configuration

To enable Duo authentication, first add the following to settings.py:

# Add duo_auth to the list of installed apps
INSTALLED_APPS = [
    # ...
    'duo_auth',
]

# The DuoAuthMiddleware requires and must come after the SessionMiddleware
# and AuthenticationMiddleware
MIDDLEWARE = [
    # ...
    'duo_auth.middleware.DuoAuthMiddleware',
]

DUO_CONFIG = {
    'DEFAULT': {
        'API_HOSTNAME': '<api-host-url>',
        'CLIENT_ID': '<integration_key>',
        'CLIENT_SECRET': '<secret_key>',
        'FIRST_STAGE_BACKENDS': [
            'django.contrib.auth.backends.ModelBackend',
        ]
    }
}

Then include the URLs in urls.py:

from django.urls import path, include

urlpatterns = [
    # ...
    path('duo/',  include('duo_auth.urls')),
]

And that’s it!

By default, the Duo Universal Prompt will be used. If the traditional iframe-based prompt is desired, add DUO_LEGACY_PROMPT to your settings and it, as well as DUO_CONFIG, will look more like this:

DUO_LEGACY_PROMPT: True
DUO_CONFIG = {
    'DEFAULT': {
        'HOST': '<api-host-url>',
        'IKEY': '<integration_key>',
        'AKEY': '<app_secret_key>',
        'SKEY': '<secret_key>',
        'FIRST_STAGE_BACKENDS': [
            'django.contrib.auth.backends.ModelBackend',
        ]
    }
}

Note the additional AKEY parameter, which should be a large randomly-generated string, similar to your SECRET_KEY.

First Stage Backends

The FIRST_STAGE_BACKENDS key for each entry in DUO_CONFIG should be a list of the authentication backends that the Duo application should act as a second factor for. If an authentication backend isn’t listed in any FIRST_STAGE_BACKENDS list, then Duo is disabled.

This behavior can be used to create scenarios where only certain users are required to authenticate with Duo. An example might be where all users of Django’s builtin authentication system are required to perform a second factor, but externally authenticated users via LDAP or some other mechanism are not.

Likewise, this feature allows different Duo apps to be tasked with users from separate authentication streams. Take the previous example, only LDAP users are instead required to authenticate against a different institution’s Duo application instance.

Username Remapping

If it is necessary to remap a user’s name to a different name in Duo, you can add the USERNAME_REMAPPER key to a Duo Config entry. The value of USERNAME_REMAPPER should be a function, callable object, or a string containing the dotted path to a callable which accepts an HttpRequest object and returns a username string, which will be used in the Duo signing request instead of the name as it appears in request.user.username.

Failing Open (Not supported on LEGACY)

By default, if any error occurs communicating with Duo’s authentication servers, the authentication attempt is aborted, and the user is logged out. If it is desirable for Duo to be skipped in the event of a communication issue, simply add 'FAIL_OPEN': True to the desired applications in the DUO_CONFIG.

Overloading the Default Template (LEGACY ONLY)

The Duo login view loads a template named duo_auth_form.html which must minimally include the following to properly render the Duo I-Frame:

<form method="POST" id="duo_form">
  {% csrf_token %}
  {% if next %}
    <input type="hidden" name="next" value="{{ next }}"/>
  {% endif %}
  {% if app_name %}
    <input type="hidden" name="app_name" value="{{ app_name }}"/>
  {% endif %}
 </form>

 <link rel="stylesheet" type="text/css" href="{{ duo_css_src }}">
 <script src="{{ duo_js_src }}"></script>
 <iframe id="duo_iframe"
         title="Two-Factor Authentication"
         frameborder="0"
         data-host="{{ duo_host }}"
         data-sig-request="{{ sig_request }}"
         data-post-action="{{ post_action }}"
         >
 </iframe>

Metadata

Release files for django-duo-auth 1.0.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-duo-auth 1.0.0
File Size Uploaded Provenance
django-duo-auth-1.0.0.tar.gz 14.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-duo-auth 1.0.0
File Interpreter ABI Platform
django_duo_auth-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 30.3 kB

Release files / django-duo-auth-1.0.0.tar.gz

Download URL django-duo-auth-1.0.0.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9780453c9dfeb082e0153854625c34c31dddea211d1ca7553bf877a257928d53
BLAKE2b-256 checksum
How to use checksums
37b4929c7d98dbdb8f9b9961447d18b9eb5a3c7bd8ca0b3b10194491c8674453
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.14

Release files / django_duo_auth-1.0.0-py2.py3-none-any.whl

Download URL django_duo_auth-1.0.0-py2.py3-none-any.whl
Size 15.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
06685ee59751fb585bf34c43ff675f6f749fe7e2def4bf5d2f4f5e5561247c84
BLAKE2b-256 checksum
How to use checksums
6fc84dcd6505cd7253bc54abc38f352d8b872cb286a78b725001d3a430cdf1ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.14

Security for django-duo-auth 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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