UNPKG

otp-io

Version:

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

58 lines • 1.4 kB
'use strict';const hotp=require('./hotp.js'),uri=require('./uri.js');/** * * * @export * @class HOTP */ class HOTP { /** * Creates an instance of HOTP. * @param {Hmac} _hmac * @param {HOTPOptions} options * @memberof HOTP */ constructor(_hmac, options) { this._hmac = _hmac; this.options = options; } /** * * * @param {string} name * @param {string} issuer * @return {string} {string} * @memberof HOTP */ getUri(name, issuer) { return uri.getKeyUri(Object.assign({ type: "hotp", name, issuer }, this.options)); } /** * * * @param {string} code * @param {Partial<HOTPOptions>} [overrides] * @return {Promise<boolean>} {Promise<boolean>} * @memberof HOTP */ async checkCode(code, overrides) { return code === (await this.generateCode(overrides)); } /** * * @param {Partial<HOTPOptions>} [overrides={}] * @return {Promise<string>} {Promise<string>} * @memberof HOTP */ async generateCode(overrides = {}) { return await hotp.hotp(this._hmac, Object.assign(Object.assign({}, this.options), overrides)); } /** * * * @return {number} next value of the counter * @memberof HOTP */ increment() { return ++this.options.counter; } }exports.HOTP=HOTP;