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

@twuni/mfa

v0.0.2

Published

A tiny, no-frills library for integrating MFA into your app's authentication and provisioning flows.

Downloads

9

Readme

Twuni MFA

A tiny, no-frills library for integrating MFA into your app's authentication and provisioning flows.

Features

  • Lightweight and minimal (zero dependencies)
  • Compatible with Google Authenticator (HMAC-SHA1 TOTP)
  • Readable implementation, so you can learn how to write your own (if you want to!)

Usage

Provisioning (aka: Setup)

To add MFA for one of your users, you need to generate (or derive) an MFA secret for that user. The simplest way to do this in Node.js is via crypto.randomBytes(), but you're free to choose your favorite PRNG. You'll need to store this secret safely, because you'll need it every time the user needs to authenticate with MFA.

import { randomBytes } from 'crypto';

const mfaSecret = randomBytes(32);

Once you have the user's MFA secret, you'll need to present them with a QR code to scan into their Authenticator app. How you generate the QR code is up to you, but the content of that QR code is a special URL you get via this library's provision() function.

import { provision } from '@twuni/mfa';

const mfaProvisioningUrl = provision({
  issuer: 'ACME Widgets, Inc.',
  secret: mfaSecret,
  subject: '[email protected]'
});
// "otpauth://totp/alice%40example.com?issuer=ACME+Widgets%2C+Inc.&secret=..."

Once the user has been presented with a QR code to scan for that URL, make sure you ask them to verify by asking them for their MFA code right away:

import { verify } from '@twuni/mfa';

try {
  verify(mfaSecret, mfaCode);
  // MFA verification succeeded!
} catch (error) {
  // ...verification failed, code did not match!
}

If verify() does not throw an error, then MFA succeeded and you can turn on MFA for that user.

Authentication with MFA

Typically, you want to ask the user to provide an MFA code after successfully logging in with their usual credentials. Just integrate the same verify() step as above in the last step of the provisioning flow.

MFA via email, text, or phone

You can use the challenge() function to generate an MFA code for a user, which you can send to them via whichever communication channel the user prefers for MFA. The verify() step is the same as before.

import { challenge } from '@twuni/mfa';

const mfaCode = challenge(mfaSecret);
// "123004"

Build your own Authenticator app

You can use the challenge() function, as above, to generate MFA codes for other apps, too, if you know their MFA secrets. You can use this to automate MFA challenge/response protocols and/or build an MFA authenticator right into your app.