X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Ftotp.js;h=aff5d961331caa33389f337efeef17122f6f53df;hb=refs%2Fheads%2Fmaster;hp=a305193e7dde085bc4ba7a76bef1814e1fa9acf6;hpb=2207e51e71606cbd1b4e3a688d450a79853dc8e9;p=squeep-totp diff --git a/lib/totp.js b/lib/totp.js index a305193..aff5d96 100644 --- a/lib/totp.js +++ b/lib/totp.js @@ -5,27 +5,27 @@ const HOTP = require('./hotp'); class TimeBasedOneTimePassword extends HOTP { /** * - * @param {Object} options - * @param {Number} options.codeLength - * @param {Buffer|String} options.key - * @param {String} options.keyEncoding - * @param {String} options.algorithm - * @param {Number} options.timeStepSeconds - * @param {Number} options.timeStepStartSeconds - * @param {Number} options.driftForward - * @param {Number} options.driftBackward + * @param {object} options options + * @param {number} options.codeLength digits in code + * @param {Buffer|string} options.key secret key + * @param {string} options.keyEncoding secret key encoding + * @param {string} options.algorithm algorithm + * @param {number} options.timeStepSeconds seconds per increment + * @param {number} options.timeStepStartSeconds seconds offset + * @param {number} options.driftForward allowed future steps to check + * @param {number} options.driftBackward allowed past steps to check */ constructor(options) { const _options = { ...options }; super(_options); this.driftOffsets = [ - 0n, // check now first + 0n, // Check now first ...Array.from({ length: this.driftBackward }, (v, k) => BigInt(-(k + 1))), ...Array.from({ length: this.driftForward }, (v, k) => BigInt(k + 1)), ]; } - get _algorithmKeyLengths() { + static get _algorithmKeyLengths() { return { ...super._algorithmKeyLengths, 'sha256': 32, @@ -33,14 +33,26 @@ class TimeBasedOneTimePassword extends HOTP { }; } + /** + * The type used when constructing the otpauth URI. + * @returns {string} otp auth type + */ + static get _type() { + return 'totp'; + } + + /** + * Derive counter from epoch. + * @returns {bigint} time based counter + */ get counter() { const epoch = Math.floor(Date.now() / 1000); return BigInt(Math.floor((epoch - this.timeStepStartSeconds) / this.timeStepSeconds)); } - set counter(_) { /* */ } + set counter(_) { /* Ignore assignment */ } // eslint-disable-line class-methods-use-this - get _defaultOptions() { + static get _defaultOptions() { const options = Object.assign(super._defaultOptions, { timeStepSeconds: 30, timeStepStartSeconds: 0, @@ -51,15 +63,20 @@ class TimeBasedOneTimePassword extends HOTP { return options; } + /** + * + * @param {bigint=} count counter value + * @returns {string} code + */ generate(count = this.counter) { return super.generate(count); } /** * - * @param {String} hotp - * @param {BigInt=} count - * @returns {Boolean} + * @param {string} hotp code + * @param {bigint=} count counter value + * @returns {boolean} is valid */ validate(hotp, count) { const counter = count ?? this.counter; @@ -71,7 +88,7 @@ class TimeBasedOneTimePassword extends HOTP { } return false; } - + } module.exports = TimeBasedOneTimePassword;