Django Admin Two-Factor Auth Documentation

Google Two-Factor Authentication for Django admin. Inspired by the user experience of Google’s Two-Step Authentication, allowing users to authenticate by using a token generator app like Google-Authenticator.

Why Django Admin Two-Factor Authentication?

  • Using google authenticator to login your Django admin.

  • Used jquery confirm dialog to get code.

  • Simple interface

  • Easy integration

Two-Factor Login Page

Contents:

Requirements

Django

Modern Django versions are supported. Currently this list includes Django 2.*, and 3.2

Python

The following Python versions are supported: 3.5, 3.6, 3.7 and 3.8 with a limit to what Django itself supports. As support for older Django versions is dropped, the minimum version might be raised. See also What Python version can I use with Django?.

PyOTP

This project is used for generating one-time passwords. PyOTP is a Python library for generating and verifying one-time passwords. It can be used to implement two-factor (2FA) or multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in.

QRCode

A Quick Response code (QRCode) is a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols)

Pillow

PIL is the Python Imaging Library adds image processing capabilities to your Python interpreter. This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.

Installation

  • Download and install last version of Django Admin Two-Factor Authentication:

$ pip install django-admin-two-factor

Setup

  • Add admin_two_factor application to the INSTALLED_APPS setting of your Django project settings.py file (note it should be before django.contrib.admin):

INSTALLED_APPS = (
    'admin_two_factor.apps.TwoStepVerificationConfig',
    'django.contrib.admin',
    ...
)
  • Migrate admin_two_factor:

$ python manage.py migrate admin_two_factor
# or
$ python manage.py syncdb
  • Add ‍‍‍‍ADMIN_TWO_FACTOR_NAME in your settings.py. This value will be displayed in Google Authenticator.

ADMIN_TWO_FACTOR_NAME = 'PROJECT_NAME'
  • Include the Admin Two Factor URL config in PROJECT_CORE/urls.py:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('two_factor/', include(('admin_two_factor.urls', 'admin_two_factor'), namespace='two_factor')),
    ...
]
  • Collect static if you are in production environment:

$ python manage.py collectstatic
  • Clear your browser cache

Run & Demo

Run

# Set up the database
$ python manage.py makemigrations
$ python manage.py migrate

# Create the superuser
$ python manage.py createsuperuser

# Start the application (development mode)
$ python manage.py runserver # default port 8000

Access the admin section in the browser: http://127.0.0.1:8000/

Demo

User List: the users who have enabled two-factor auth

User List
Add New User:
Add New User
Scan QRCode and enter the valid code:
Scan QRCode and enter the valid code
Logout and login again with Google Authenticator:
Logout and login

I would love to hear your feedback on this application. If you run into problems, please file an issue on GitHub, or contribute to the project by forking the repository and sending some pull requests.