UNPKG

otp-io

Version:

🕖 Typed library to work 2fa via Google Authenticator/Time-based TOTP/Hmac-based HOTP

49 lines • 1.23 kB
'use strict';const totp=require('./totp.js'),uri=require('./uri.js');/** * * * @export * @class TOTP */ class TOTP { /** * Creates an instance of TOTP. * @param {Hmac} _hmac * @param {TOTPOptions} options * @memberof TOTP */ constructor(_hmac, options) { this._hmac = _hmac; this.options = options; } /** * * * @param {string} name * @param {string} issuer * @return {string} {string} * @memberof TOTP */ getUri(name, issuer) { return uri.getKeyUri(Object.assign({ type: "totp", name, issuer }, this.options)); } /** * * * @param {string} code * @param {Partial<TOTPOptions>} [overrides] * @return {Promise<boolean>} {Promise<boolean>} * @memberof TOTP */ async checkCode(code, overrides) { return code === (await this.generateCode(overrides)); } /** * * @param {Partial<TOTPOptions>} [overrides={}] * @return {Promise<string>} {Promise<string>} * @memberof TOTP */ async generateCode(overrides = {}) { return await totp.totp(this._hmac, Object.assign(Object.assign({}, this.options), overrides)); } }exports.TOTP=TOTP;