image

Ishaan

Introduction

Two-Factor Authentication (2FA) is a security process in which users provide two different authentication factors to verify their identity. This method creates a layered defense, making it more difficult for unauthorized users to gain access to a target, such as a computer, network, or database.


The two factors typically involved in 2FA are:

  1. Something you know (e.g., a password or PIN)
  2. Something you have (e.g., a smartphone or security token)


How it works

When you enable 2FA on your account, logging in becomes a two-step process:

  1. Password Entry: You start by entering your usual password.
  2. Second Factor Verification: After the correct password is entered, a second form of identification is required. This could be a code sent to your smartphone, a fingerprint scan, or a physical security key.

Common Methods of 2FA

  • SMS-Based 2FA: A one-time code is sent to your phone via SMS. While convenient, this method is less secure due to the risk of SIM swapping and interception.
  • Authenticator Apps: Apps like Google Authenticator or Authy generate time-based one-time passwords (TOTP) that are more secure than SMS.
  • Email-Based 2FA: A code is sent to your email address. This method is more secure than SMS but still susceptible to email account compromises.
  • Hardware Tokens: Physical devices like YubiKeys provide the highest level of security by generating unique codes or using NFC for authentication.


Importance of TFA

Enhanced Security

Passwords alone are often weak due to common user habits like reusing passwords across multiple sites or choosing easily guessable passwords. 2FA significantly enhances security by requiring an additional verification step, making it much harder for attackers to gain access.

Protection Against Phishing

Phishing attacks trick users into revealing their passwords by masquerading as legitimate websites. Even if an attacker manages to steal your password, they would still need the second factor to access your account, which they likely don't have.

Compliance and Trust

Many industries and regulatory bodies now require 2FA to protect sensitive information. Implementing 2FA can help ensure compliance with regulations and build trust with your users by demonstrating a commitment to security.

Implementation of TFA

For Backend

To implement two-factor authentication (TFA) in your application, you'll need to install the node-2fa package. This package provides a straightforward method for incorporating TFA into your system. Start by installing the package, which handles the generation and validation of TFA tokens.




To manage two-factor authentication (TFA) in your application, you will need to create two endpoints in your backend:


Enable TFA Endpoint:

This is designed to enable TFA for a user. It is accessed via a POST request and accepts three parameters:

  • Provider: Specifies the TFA provider being used (e.g., Google Authenticator, Authy).
  • OTP (One-Time Password): The initial OTP provided by the user to set up TFA.
  • Boolean Value: Indicates whether to enable or disable TFA for the user. Setting this to true will enable TFA, while false will disable it.


This endpoint handles the configuration of the TFA settings based on the provided parameters, allowing users to activate or deactivate TFA as needed.

when this endpoint is called it returns QR code as response.


Verify Passcode Endpoint:

This is used to verify the TFA passcode during the authentication process. It accepts two parameters:

  1. Provider: The TFA provider used for generating the passcode.
  2. OTP: The one-time passcode entered by the user.

The endpoint checks the validity of the passcode against the stored TFA settings to ensure that the user is authenticated correctly.

When this end point is triggered it checks the OTP and returns the Boolean Value




For Frontend


Start by checking if Google Authenticator is already enabled for the user. If not, you need to initiate a registration process, which involves fetching a QR code from the first endpoint which returns QR code data. This QR code can be scanned using the Google Authenticator app, setting up the user's device for TFA.



Once the QR code is provided, prompt the user to enter the code generated by their Authenticator app. Use this code to either enable or disable TFA based on the current status. Upon submission, validate the code against your second endpoint.



If enabling TFA, you store the code securely; if disabling, ensure the code is verified before deactivating the feature.

Implementing this involves creating a modal with options to view the QR code, input the verification code, and submit the request. Display appropriate messages based on the success or failure of the TFA setup, ensuring a smooth user experience.


By following these steps, you can seamlessly add an extra layer of security to your application, protecting user accounts from unauthorized access.

image

Ishaan

Architect
ishaan@fabbuilder.com

I love finding patterns and concluding insights out of it. After working as Employee, a Consultant, a Freelancer I figured out mostly we start our project or a module or microservice either by copying an existing code or base repositories from GitHub lets say. And then spend a lot of time on customising it, analysing pitfalls, making it work so that I can start wrinting business logic for the work. So, I started POC-ing FAB Builder so that I get a trailered project targetted to my requirements and on which I can start wrinting business logic within few mins. This makes my life easy and my clients get a better quality products which are easy to modify and easy to maintain.

chat-icon