Skip to content
Md Rasheduzzaman edited this page May 25, 2024 · 2 revisions

Welcome to the Two-Factor Authentication Wiki

Overview

The two-factor-auth repository provides a robust and easy-to-implement solution for adding two-factor authentication (2FA) to your web application. This repository includes a comprehensive implementation using the latest security standards to ensure your users' accounts are protected.

Table of Contents

Installation

To get started with the two-factor-auth library, follow these steps:

  1. Clone the Repository

    git clone https://github.com/jmrashed/two-factor-auth.git
    cd two-factor-auth
  2. Install Dependencies Ensure you have Node.js installed. Then, run:

    npm install

Usage

Integrating two-factor-auth into your application is straightforward. Here is a step-by-step guide:

  1. Initialize 2FA in Your Application

    const TwoFactorAuth = require('two-factor-auth');
    const twoFactor = new TwoFactorAuth({
        secretKey: 'YOUR_SECRET_KEY',
        algorithm: 'SHA1',
        digits: 6,
        period: 30,
    });
  2. Generate a QR Code for User Enrollment

    const user = { username: 'exampleUser' };
    const qrCodeUrl = twoFactor.generateQRCodeUrl(user.username);
    console.log(`Scan this QR code with your authenticator app: ${qrCodeUrl}`);
  3. Verify a User's Token

    const userToken = '123456'; // Token entered by the user
    const isValid = twoFactor.verifyToken(user.username, userToken);
    if (isValid) {
        console.log('Token is valid!');
    } else {
        console.log('Token is invalid.');
    }

Configuration

The two-factor-auth library can be configured with the following options:

  • secretKey: The base secret key used for generating tokens.
  • algorithm: The hashing algorithm to use (SHA1, SHA256, SHA512).
  • digits: The number of digits in the token (default is 6).
  • period: The time period in seconds for which a token is valid (default is 30 seconds).

Example configuration:

const twoFactor = new TwoFactorAuth({
    secretKey: process.env.SECRET_KEY || 'defaultSecretKey',
    algorithm: 'SHA256',
    digits: 6,
    period: 30,
});

Contributing

We welcome contributions from the community! To contribute:

  1. Fork the Repository
  2. Create a Branch
    git checkout -b feature/your-feature-name
  3. Commit Your Changes
    git commit -m 'Add some feature'
  4. Push to the Branch
    git push origin feature/your-feature-name
  5. Open a Pull Request

License

This project is licensed under the MIT License. See the LICENSE file for details.

FAQ

How secure is this implementation?

The two-factor-auth library uses industry-standard algorithms and practices to ensure the highest level of security for your users.

Can I customize the token generation?

Yes, you can customize the token generation by configuring the algorithm, digits, and period options.

What if I lose my secret key?

It's crucial to keep your secret key secure. If it's compromised, generate a new key and update your configuration.

Clone this wiki locally