From the course: Securing Django Applications

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Validating 2FA login before performing actions in Django

Validating 2FA login before performing actions in Django - Django Tutorial

From the course: Securing Django Applications

Start my 1-month free trial

Validating 2FA login before performing actions in Django

- [Tutor] To use two factor authentication with Django and Django REST framework, we need a way to confirm and validate an action. Therefore, we need to create a permission class. We're going to be importing permissions from Django REST framework, and we're going to be importing our TwoFactorAuthCode model. And the permission class will be called, TwoFactorAuthRequired. Inheriting from BasePermission, and it will be operating at the has_permission level instead of has object permission. So we're going to extract the code from any request_data, and the parameter will be called auth code. So all requests that require twofactorauth will have this additional auth code parameter. And then we're going to be using the TwoFactorAuthCode, validate_code class method. So we pass in the request_user and then we pass in the code.

Contents