otp-io
Version:
🕖 Typed library to work 2fa via Google Authenticator/Time-based TOTP/Hmac-based HOTP
49 lines • 1.22 kB
JavaScript
import {totp}from'./totp.mjs';import {getKeyUri}from'./uri.mjs';/**
*
*
* @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 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(this._hmac, Object.assign(Object.assign({}, this.options), overrides));
}
}export{TOTP};