Skip to content
Gerald Yeo edited this page Mar 28, 2020 · 18 revisions

Scoped modules

This library is now broken down into multiple packages in order to support more crypto and base32 libraries as needed by different projects. These modules are now scoped under @otplib/* packages, with the exception of original otplib which acts as a quick-start package.

All other otplib-* libraries that are not scoped are not maintained under this project.

1-1 Migration (for classes)

In order to ease migration, a module preset 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';

Management of License

As the package moved towards a more pluggable interface, the external dependencies are now found within the plugin modules. i.e. thirty-two which used to be listed in otplib is within @otplib/plugin-thirty-two module.

As such, for projects in environments that are license sensitive, please do note on this update.

Due to the above, as the browser module contains optimisation and have modules and may have different dependencies, it is no longer included within the otplib by default, to maintain the dependency parity with the previous versions.

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/core';

// 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
// The current (authenticator | totp | hotp).options now returns
// defaults and transient options. If you want to set the instance
// with new defaults, do use the following instead:
// (authenticator|totp|hotp).clone(...);

Options - epoch

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

New Concepts

  • There are now async methods available.
  • All packages are classified into core, plugin, preset (and tests).

Browser Module

If you have been using otplib/otplib-browser, after upgrading to v12, you'll have to import buffer or link to buffer separately as it is now marked as an external module during the build.

i.e.

<script src="https://unpkg.com/@otplib/preset-browser@^12.0.0/buffer.js"></script>

For more information, refer to: https://github.com/yeojz/otplib/blob/master/packages/otplib-preset-browser/README.md

Clone this wiki locally