All files / src/derive/factors question.js

100% Statements 12/12
100% Branches 4/4
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                  1x                                                   7x 6x   5x 5x   5x 5x       5x     5x         1x  
/**
 * @file MFKDF Question Factor Derivation
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Derive question factor for multi-factor key derivation
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
const zxcvbn = require('zxcvbn')
 
/**
 * Derive an MFKDF Security Question factor
 *
 * @example
 * // setup key with security question factor
 * const setup = await mfkdf.setup.key([
 *   await mfkdf.setup.factors.question('Fido')
 * ], {size: 8})
 *
 * // derive key with security question factor
 * const derive = await mfkdf.derive.key(setup.policy, {
 *   question: mfkdf.derive.factors.question('Fido')
 * })
 *
 * setup.key.toString('hex') // -> 01d0c7236adf2516
 * derive.key.toString('hex') // -> 01d0c7236adf2516
 *
 * @param {string} answer - The answer from which to derive an MFKDF factor
 * @returns {function(config:Object): Promise<MFKDFFactor>} Async function to generate MFKDF factor information
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 1.0.0
 * @memberof derive.factors
 */
function question (answer) {
  if (typeof answer !== 'string') throw new TypeError('answer must be a string')
  if (answer.length === 0) throw new RangeError('answer cannot be empty')
 
  answer = answer.toLowerCase().replace(/[^0-9a-z ]/gi, '').trim()
  const strength = zxcvbn(answer)
 
  return async (params) => {
    return {
      type: 'question',
      data: Buffer.from(answer),
      params: async () => {
        return params
      },
      output: async () => {
        return { strength }
      }
    }
  }
}
module.exports.question = question