npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easy-2fa

v1.0.1

Published

A lightweight npm package for generation and verification of 2FA codes.

Downloads

249

Readme

Easy-2FA

Easy-2FA is a lightweight NPM package designed for effortless generation and verification of Two-Factor Authentication (2FA) codes with additional utilities. It supports both TOTP and HOTP.

Additional utilities include generating the otpauth:// URL and generating a QR code.

How to use

Firstly, install easy-2fa using your package manager:

npm install easy-2fa
yarn add easy-2fa
pnpm install easy-2fa

After installation, you can import the package in your project and start using it.

import tfa from 'easy-2fa';

// Generate a seed (keep this unique and stored somewhere safe)
const seed = tfa.generateSeed();
// => tj8kxt4sa6wuq6tucnda2h6gth76s80o04276hhjth89evbp

// Generate a QR code for the user to scan into the authenticator app
const qrCode = await tfa.generateQRCode({ seed });

// ...

// Once you receive a code from the user, verify it this way
tfa.verifyTOTP(seed, 961853);
// => true

tfa.verifyTOTP(seed, 0);
// => false

And that's it! For more information about the options, consult the API documentation below.

API Reference

generateSeed()

Description: Generates a seed used for generating and checking the validity of 2FA codes.

Parameters:

  • options?: The options to use when generating the seed.
    • length?: The length of the seed to generate. Default is 48.

Returns: A string representing the generated seed.

Example:

const seed = tfa.generateSeed();

Important note:

Do NOT share this publicly. This is a secret seed that should only be known by the user. Store it somewhere safe.

generateCode()

Description: Generates a 2FA code from a seed.

Parameters:

  • seed: The seed to generate the code from.
  • counter: The counter to generate the code from.
  • options?: The options to use when generating the code.
    • length?: The length of the code to generate. Default is 6.

Returns: A number representing the generated code.

Example:

const code = tfa.generateCode(seed);

verifyTOTP()

Description: Verifies a 2FA time-based (TOTP) code.

Parameters:

  • seed: The seed to check against.
  • code: The code to check.
  • options?: The options to use when verifying the code.
    • length?: The length of the code to verify. Default is 6.
    • step?: The time interval in seconds for which a TOTP code is valid. Default is 30.

Returns: A boolean representing the validity of the code.

Example:

const isValid = tfa.verifyTOTP(seed, 961853);
if (isValid) console.log('The code is valid!');
else console.log('The code is invalid!');

verifyHOTP()

Description: Verifies a 2FA hash-based (HOTP) code.

Parameters:

  • seed: The seed to check against.
  • code: The code to check.
  • counter: The counter to check against.
  • options?: The options to use when verifying the code.
    • length?: The length of the code to verify. Default is 6.
    • allowedBeforeDrift?: The number of HOTP codes that can be checked before the current counter value. Default is 0.
    • allowedAfterDrift?: The number of HOTP codes that can be checked after the current counter value. Default is 0.

Returns: A boolean representing the validity of the code.

Example:

const isValid = tfa.verifyHOTP(seed, 961853, 0);
if (isValid) console.log('The code is valid!');
else console.log('The code is invalid!');

generateURL()

Description: Generates a URL from a seed. This URL is later used to generate a QR code.

Parameters:

  • seed: The seed to generate the URL from.
  • options: The options to use when generating the URL.
    • account: The name of the user's account.
    • issuer?: The issuer of the 2FA.

Returns: A string representing the generated URL.

Example:

const url = tfa.generateURL(seed, {
  account: '[email protected]',
  issuer: 'Discord'
});

generateQRCode()

Description: Generates a QR code from a seed/URL. Either can be provided.

Parameters:

  • options?: The options to use when generating the QR code.
    • seed?: The seed to generate the QR code from.
    • urlOptions?: The options to use when generating a URL, if the seed is provided.
      • account?: The name of the user's account.
      • issuer?: The issuer of the 2FA.
    • url?: The URL to generate the QR code from.

Returns: A string representing the generated QR code.

Example:

const qrCode = tfa.generateQRCode({
  seed: 'tj8kxt4sa6wuq6tucnda2h6gth76s80o04276hhjth89evbp'
});

Bugs/Issues

If you find an issue or bugs in the code or need any help, please open an issue on the repository.

Contribution

If you think that something can be improved or changed, feel free to open a pull request.

License

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