

A comprehensive Node.js package that simplifies the implementation of One-Time Password (OTP) authentication using HMAC-based One-Time Password (HOTP) and Time-based One-Time Password (TOTP) algorithms.
Features
- Support both HOTP and TOTP algorithms
- Easy-to-use API for generating and verifying OTPs
- Customizable OTP length, counters and time window
- Supports various hashing algorithms (SHA-1, SHA-256, SHA-512)
- Compatible with popular OTP generators like Google Authenticator and Authy
Installation
Install the package using NPM:
npm i --save time2fa
Usage/Examples
TOTP
Generate key
import { Totp } from "time2fa";
const key = Totp.generateKey({ issuer: "N0C", user: "johndoe@n0c.com" });
console.log(key);
Validate passcode
import { Totp } from "time2fa";
const valid = Totp.validate({ passcode: "123456", secret: "ABCDEFGHIJKLMN12" });
console.log(valid);
Generate passcodes
import { Totp, generateConfig } from "time2fa";
const config = generateConfig();
const codes = Totp.generatePasscodes({ secret: "ABCDEFGHIJKLMN12" }, config);
console.log(codes);
QRCode generation
You must use an external library. For the example below we use qrcode.
import { Totp } from "time2fa";
import * as qrcode from "qrcode";
const key = Totp.generateKey({ issuer: "N0C", user: "johndoe@n0c.com" });
console.log(key);
qrcode.toDataURL(key.url, (err, url) => {
console.log(url);
});
HOTP
Generate Passcode
import { Hotp, generateConfig, generateSecret } from "time2fa";
const config = generateConfig();
const secret = generateSecret();
const code = Hotp.generatePasscode({ secret, counter: 1 }, config);
console.log(code);
Validate passcode
import { Hotp } from "time2fa";
const valid = Hotp.validate({
passcode: "123456",
secret: "ABCDEFGHIJKLMN12",
counter: 1,
});
console.log(valid);
Helpers
generateConfig()
Generate default configuration
import { generateConfig } from "time2fa";
const config = generateConfig();
console.log(config);
generateSecret()
Only support base32 at the moment
import { generateSecret } from "time2fa";
const secret = generateSecret();
console.log(secret);
generateUrl()
import { generateUrl } from "time2fa";
const url = generateUrl({
issuer: "N0C",
user: "johndoe@n0c.com",
secret: "ABCDEFGHIJKLMN12",
});
console.log(url);
generateBackupCodes()
Backup code should only be used once
import { generateBackupCodes } from "time2fa";
const backupCodes = generateBackupCodes();
console.log(backupCodes);
Documentation
Functions
Helpers
generateConfig(config?: TotpConfig): ValidTotpConfig
generateSecret(secretSize: number = DEFAULT_TOTP_SECRET_SIZE): string
generateBackupCodes(numCodes = 10, codeLength = DEFAULT_TOTP_DIGITS): string[]
generateUrl(options: UrlOptions, config: ValidTotpConfig): string
Totp
Totp.generateKey(options: TotpOptions, config?: TotpConfig): GenerateKey
Totp.generatePasscodes(options: TotpCode, config: ValidTotpConfig): string[]
Totp.validate(options: TotpValidateOptions, config?: TotpConfig): boolean
Hotp
Hotp.generatePasscode(options: HotpCode, config: ValidTotpConfig): string
Hotp.validate(options: HotpValidateOptions, config?: TotpConfig): boolean
Interfaces / Parameters
TotpConfig
secretSize | number | 10 | Optional - Secret size |
period | number | 30 | Optional - Period of time |
digits | number | 6 | Optional- Code length |
algo | Algorithms | sha1 | Optional - 'sha1' | 'sha256' | 'sha512' |
ValidTotpConfig
secretSize | number | - | Required - Secret size |
period | number | - | Required - Period of time |
digits | number | - | Required- Code length |
algo | Algorithms | - | Required - 'sha1' | 'sha256' | 'sha512' |
TotpOptions
issuer | string | - | Required - Issuer name |
user | string | - | Required - Username |
UrlOptions
issuer | string | - | Required - Issuer name |
user | string | - | Required - Username |
secret | string | - | Required - Secret |
TotpCode
secret | string | - | Required - Secret |
drift | number | 0 | Optional - Time tolerance |
TotpValidateOptions
passcode | string | - | Required - The passcode to validate |
secret | string | - | Required - Secret |
drift | number | 0 | Optional - Time tolerance |
HotpCode
secret | string | - | Required - Secret |
counter | number | - | Required - Custom counter value |
HotpValidateOptions
passcode | string | - | Required - The passcode to validate |
secret | string | - | Required - Secret |
counter | number | - | Required - Custom counter value |
Contributing
All PR's are welcome!
Running Tests
To run tests, run the following command
npm run test
License
MIT