DEV Community

Cover image for Understanding Multi-Factor Authentication: A Deep Dive into MFA
Igor Venturelli
Igor Venturelli

Posted on • Originally published at igventurelli.io

Understanding Multi-Factor Authentication: A Deep Dive into MFA

In the ever-evolving landscape of cybersecurity, protecting your digital assets has never been more critical. One of the most effective ways to enhance security is through Multi-Factor Authentication (MFA). This blog post will demystify MFA, explain its importance, and provide insights into how it works. We will also touch on implementing MFA using React (Next.js) and Express (NestJS).

What is Multi-Factor Authentication (MFA)?

Multi-Factor Authentication (MFA) is a security mechanism that requires users to provide two or more verification factors to gain access to a resource, such as an application, online account, or VPN. Instead of relying solely on a username and password, MFA adds additional layers of security, making it significantly harder for unauthorized users to gain access.

What is Two-Factor Authentication (2FA)?

Two-Factor Authentication (2FA) is a subset of MFA that specifically requires two distinct forms of identification. Typically, this involves:

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

While 2FA adds an extra layer of security compared to single-factor authentication, MFA can include additional factors, further strengthening security.

Types of Authentication Factors

Besides the traditional username and password, MFA can utilize various external devices and methods, including:

  1. SMS or Email Codes: Temporary codes sent to a user’s phone or email.
  2. Authenticator Apps: Applications like Google Authenticator or Authy that generate time-based one-time passwords (TOTPs).
  3. Hardware Tokens: Physical devices that generate or receive authentication codes.
  4. Biometric Data: Fingerprint scanners, facial recognition, or retina scans.
  5. FIDO (Fast Identity Online) Devices: A set of standards for strong authentication that uses public key cryptography to provide stronger security than password-based methods.

The MFA Process

The general process for MFA involves the following steps:

  1. Login Attempt: The user enters their username and password.
  2. First Factor Verification: The system verifies the username and password.
  3. Second Factor Request: The user is prompted to provide a second form of verification.
  4. Second Factor Verification: The system verifies the second factor, which could be a code from an authenticator app or a biometric scan.
  5. Access Granted: If both factors are correct, the user is granted access.

Time-Based One-Time Password (TOTP)

A common method used in MFA is the Time-Based One-Time Password (TOTP). This method generates a unique code that changes every 30 seconds. Users typically have 60 seconds to enter the code, providing a brief window for authentication.

Implementing MFA in a Web Application

To illustrate how MFA can be implemented, we'll provide a simple example using React (Next.js) for the frontend and Express (NestJS) for the backend.

Backend: Express (NestJS)

First, set up your Express (NestJS) server:

  1. Install Dependencies:

    npm install @nestjs/common @nestjs/core @nestjs/platform-express qrcode speakeasy
    
  2. Create MFA Service:

    import { Injectable } from '@nestjs/common';
    import * as speakeasy from 'speakeasy';
    import * as qrcode from 'qrcode';
    
    @Injectable()
    export class MfaService {
      generateSecret() {
        const secret = speakeasy.generateSecret();
        return {
          otpauth_url: secret.otpauth_url,
          base32: secret.base32,
        };
      }
    
      async generateQrCode(otpauthUrl: string) {
        return await qrcode.toDataURL(otpauthUrl);
      }
    
      verifyToken(secret: string, token: string) {
        return speakeasy.totp.verify({
          secret,
          encoding: 'base32',
          token,
        });
      }
    }
    
  3. Create MFA Controller:

    import { Controller, Get, Post, Body } from '@nestjs/common';
    import { MfaService } from './mfa.service';
    
    @Controller('mfa')
    export class MfaController {
      constructor(private readonly mfaService: MfaService) {}
    
      @Get('generate')
      async generate() {
        const { otpauth_url, base32 } = this.mfaService.generateSecret();
        const qrCode = await this.mfaService.generateQrCode(otpauth_url);
        return { qrCode, secret: base32 };
      }
    
      @Post('verify')
      verify(@Body() body: { secret: string; token: string }) {
        return this.mfaService.verifyToken(body.secret, body.token);
      }
    }
    

Frontend: React (Next.js)

Set up your Next.js application to interact with the NestJS backend:

  1. Install Dependencies:

    npm install axios qrcode.react
    
  2. Create a Page to Display the QR Code:

    import { useState, useEffect } from 'react';
    import axios from 'axios';
    import QRCode from 'qrcode.react';
    
    export default function MfaSetup() {
      const [qrCode, setQrCode] = useState('');
      const [secret, setSecret] = useState('');
    
      useEffect(() => {
        async function fetchQrCode() {
          const response = await axios.get('/api/mfa/generate');
          setQrCode(response.data.qrCode);
          setSecret(response.data.secret);
        }
        fetchQrCode();
      }, []);
    
      return (
        <div>
          <h1>Set up MFA</h1>
          {qrCode && <QRCode value={qrCode} />}
          <p>Secret: {secret}</p>
        </div>
      );
    }
    
  3. Create a Page to Verify the Token:

    import { useState } from 'react';
    import axios from 'axios';
    
    export default function MfaVerify() {
      const [token, setToken] = useState('');
      const [secret, setSecret] = useState('');
      const [result, setResult] = useState(null);
    
      const handleSubmit = async (e) => {
        e.preventDefault();
        const response = await axios.post('/api/mfa/verify', { secret, token });
        setResult(response.data);
      };
    
      return (
        <div>
          <h1>Verify MFA Token</h1>
          <form onSubmit={handleSubmit}>
            <input
              type="text"
              placeholder="Secret"
              value={secret}
              onChange={(e) => setSecret(e.target.value)}
            />
            <input
              type="text"
              placeholder="Token"
              value={token}
              onChange={(e) => setToken(e.target.value)}
            />
            <button type="submit">Verify</button>
          </form>
          {result !== null && <p>Verification result: {result.toString()}</p>}
        </div>
      );
    }
    

Final Thoughts

MFA is a robust security measure that significantly enhances the protection of your digital assets. Understanding its workings and implementing it correctly can safeguard against unauthorized access and data breaches. By leveraging MFA, especially with time-based one-time passwords, you add a crucial layer of security to your applications.

As we move towards more secure authentication standards like FIDO, the landscape of cybersecurity continues to evolve. Stay vigilant, keep learning, and ensure that your security measures are always up to date.


Let’s connect!

📧 Don't Miss a Post! Subscribe to my Newsletter!
➡️ LinkedIn
🚩 Original Post
☕ Buy me a Coffee

Top comments (0)