otp-io
Version:
🕖 Typed library to work 2fa via Google Authenticator/Time-based TOTP/Hmac-based HOTP
58 lines • 1.38 kB
JavaScript
import {hotp}from'./hotp.mjs';import {getKeyUri}from'./uri.mjs';/**
*
*
* @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 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(this._hmac, Object.assign(Object.assign({}, this.options), overrides));
}
/**
*
*
* @return {number} next value of the counter
* @memberof HOTP
*/
increment() {
return ++this.options.counter;
}
}export{HOTP};