Skip to content

A full-featured React Native TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) authentication.

License

Notifications You must be signed in to change notification settings

4cc3ssX/react-native-nitro-totp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-nitro-totp

Installation

# npm
npm install react-native-nitro-totp

# yarn
yarn add react-native-nitro-totp

Extra installation steps for Nitro Modules can be found here

Usage

import {
  formatOTP,
  formatSecretKey,
  isSecretKeyValid,
  NitroSecret,
  NitroTotp,
  parseSecretKey,
} from 'react-native-nitro-totp';

const secret = NitroSecret.generate();

// TOTP
const otp = NitroTotp.generate(secret);
const isValid = NitroTotp.validate(secret, otp);
const generatedAuthURL = NitroTotp.generateAuthURL({
  secret,
  issuer: 'NitroTotp',
  label: 'NitroTotp',
  period: defaultOptions.period,
  digits: defaultOptions.digits,
  issuerInLabel: false,
  algorithm: SupportedAlgorithm.SHA1,
});

// HOTP
const otp = NitroHOTP.generate(secret);
const isValid = NitroHOTP.validate(secret, otp);
const generatedAuthURL = NitroHOTP.generateAuthURL({
  secret,
  issuer: 'NitroHotp',
  label: 'NitroHotp',
  counter: defaultOptions.counter,
  digits: defaultOptions.digits,
  issuerInLabel: false,
  algorithm: SupportedAlgorithm.SHA1,
});

Generation Options

export interface BaseGenerateOptions {
  /**
   * The number of digits in the OTP.
   * @type {number}
   * @default 6
   */
  digits?: number

  /**
   * The algorithm to use for generating the OTP.
   * @type {SupportedAlgorithm}
   * @default 'SHA1'
   */
  algorithm?: SupportedAlgorithm
}

export interface NitroTotpGenerateOptions extends BaseGenerateOptions {
  /**
   * The period in seconds.
   * @type {number}
   * @default 30
   */
  period?: number
}

export interface NitroHOTPGenerateOptions extends BaseGenerateOptions {
  /**
   * The counter to use for generating the OTP.
   * @type {number}
   * @default 0
   */
  counter?: number
}

Validation Options

export interface BaseValidateOptions extends BaseGenerateOptions {
  /**
   * The window of time steps to allow for OTP validation.
   * @type {number}
   * @default 1
   */
  window?: number
}

export interface NitroHOTPValidateOptions extends BaseValidateOptions {
  /**
   * The counter to use for validating the OTP.
   * @type {number}
   * @default 0
   */
  counter?: number
}

export interface NitroTotpValidateOptions extends BaseValidateOptions {
  /**
   * The period in seconds.
   * @type {number}
   * @default 30
   */
  period?: number
}

OTP Auth URL Options

export interface OTPAuthURLOptions extends BaseGenerateOptions {
  /**
   * The issuer of the secret key.
   * @type {string}
   */
  issuer?: string

  /**
   * The label of the secret key.
   * @type {string}
   */
  label?: string

  /**
   * The secret key to use for generating the OTP.
   * @type {string}
   */
  secret: string

  /**
   * Whether to include the issuer in the label.
   * @type {boolean}
   * @default false
   */
  issuerInLabel?: boolean

  /**
   * The period in seconds.
   * @type {number}
   * @default 30
   */
  period?: number

  /**
   * The counter to use for generating the OTP.
   * @type {number}
   * @default 0
   */
  counter?: number
}

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with react-native-nitro-modules

About

A full-featured React Native TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) authentication.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published