Django Two Factor Authentication Example

Published On: 12/12/2022 | Category: Django


Hi Dev,

Are you looking for example of django two factor authentication example. we will help you to give example of django-otp authentication example. I explained simply about django admin two factor authentication. We will look at example of two factor authentication in django.

One of the numerous types of multi-factor authentication that provides an additional layer of protection in addition to login credentials to verify user identity is two-factor authentication, or 2FA for short.

In this case, I'm going to create a system where admin users will be asked to enter a Time-based One-time Password (TOTP) while logging in. The produced token will only be valid for a limited period of time (i.e. 30 seconds), as the name "TOTP" implies, after which a new token will be automatically generated.

Here i explained simply step by step example of how to setup cron job task scheduling in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core

Step 3: Installing required library

In this section first of all we need to install django-otp qrcode library through below following command:

pip install django-otp qrcode

Step 4: Update setting.py

Then update INSTALLED_APPS within our settings.py file to notify Django about the app.

settings.py
....
INSTALLED_APPS = [
    'core'

    'django_otp',
    'django_otp.plugins.otp_totp',
]

MIDDLEWARE = [
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django_otp.middleware.OTPMiddleware',  #new
]

Step 5: Configure the admin interface

The OTPAdminSite model must be used in our urls.py file. It will enable us to initially register for the TOTP device.

example/urls.py
from django.contrib import admin
from django.urls import path, include

from django_otp.admin import OTPAdminSite
from django.contrib.auth.models import User
from django_otp.plugins.otp_totp.models import TOTPDevice
from django_otp.plugins.otp_totp.admin import TOTPDeviceAdmin

class OTPAdmin(OTPAdminSite):
    pass

admin_site = OTPAdmin(name='OTPAdmin')
admin_site.register(User)
admin_site.register(TOTPDevice, TOTPDeviceAdmin)

To register the User and TOTPDevice model in the admin panel, we are merely building an OTPAdmin class in this sample.

The TOTP Device for our first superuser must now be made and registered. I'm referring to the superuser's Google Authenticator URL when I say "TOTP Device."

python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Right now, you may visit a standard Django admin panel (without 2FA) via http://localhost:8000/admin

Step 6: Setup 2FA

Enter your login information for the Django admin panel at http://localhost:8000/admin and log in as the superuser you created before.

1. To add your first device, go to the TOTP devices table and click the "ADD TOTP DEVICE +" button.

2. Choose the user from the User table, then type the desired device name.

3. Maintain the current settings and click "Save" to save the record.

4. Click the QR code from the listing to scan it with your Google Authenticator app, or manually share the link.

5. After scanning, it will automatically produce Time-based OTPs every 30 seconds and save this account in the Google Authenticator app.



Step 7: 2FA while login

We have currently added our TOTP gadget to the Google Authenticator app. Now we can use the 2FA-based login screen in place of the standard Django login.

urlpatterns = [
    path('admin/', admin_site.urls),
    ...
]

Step 8: Testing 2FA

The username and password fields are now present in the Django admin panel, along with a new field called "OTP Token" where you can enter a time-based OTP from Google Authenticator.



I hope it will help you....

Happy Coding!