Skip to content
Gerald Yeo edited this page Aug 25, 2019 · 18 revisions

THIS DOC IS STILL WIP.

1-1 Migration

In order to ease migration, a module preset (for the classes) is provided for users coming from v11.x. There should be no change to the format of options which you use for v11.x.

// instead of
import { authenticator, hotp, totp } from 'otplib';

// update to
import { authenticator, hotp, totp } from 'otplib/preset-v11';
// you might need to do a `npm install thirty-two` as it is now moved to devDependencies.

Dependencies

As the package moved towards a more pluggable interface, they are now listed under devDependencies. As such, you would need to install the corresponding npm modules for the plugins. These are listed in the README.md.

# Past
npm install otplib

# Now
npm install otplib thirty-two

Removing it from the dependency was to align with the goal of having the library agnostic to base32 and crypto frameworks.

Imports - change of entry points

Previous versions of otplib exposed the initialised classes as an entry point, while you can get the classes via the prototype.

// v11.x imports
import { authenticator, hotp, totp } from 'otplib';

// v11.x get classes
const HOTP = hotp.HOTP; // or hotp.getClass();
const TOTP = totp.TOTP; // or totp.getClass();
const Authenticator = authenticator.Authenticator; // or authenticator.getClass();

In v12, that has been deprecated in favour of a more explicit imports.

Main imports are now the classes, while initialised versions are delegated to presets. This allows the module to have more flexibility in configuration.

// Classes are now the default export
import { HOTP, TOTP, Authenticator } from 'otplib';

// initialised versions are in preset-*
import { authenticator, hotp, totp } from 'otplib/preset-default';

Class Method - Renames

hotp.HOTP
hotp.getClass()
// to
import { HOTP } from 'otplib';

// ---------------------

totp.HOTP
totp.getClass()
// to
import { TOTP } from 'otplib';

// ---------------------

authenticator.HOTP
authenticator.getClass()
// to
import { Authenticator } from 'otplib';

// ---------------------

(authenticator|totp|hotp).optionsAll
// to
(authenticator|totp|hotp).allOptions()`
// Note: changed from getter to a function call.

Class Method - New

(authenticator | totp | hotp).clone();
(authenticator | totp | hotp).create();
(totp | hotp).keyuri();
// Note: this was available for authenticator, but is expanded for totp and hotp.

Class Method - Deprecations

(authenticator | totp | hotp).defaultOptions; // setter and getter
// This has been deprecated in favour of:
(authenticator|totp|hotp).clone(...);
// Using clone also ensures some form of immutability of the instance.

Options - epoch

IMPORTANT - options.epoch is now using JavaScript epoch instead of UNIX epoch. i.e. UNIX epoch * 1000 or Date.now().

Clone this wiki locally