All files / src/setup/factors password.js

100% Statements 16/16
100% Branches 8/8
100% Functions 3/3
100% Lines 12/12

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 57 58 59 60 61 62 63 64                  1x 1x                                                         390x 389x   388x   388x 387x   386x   386x           369x     368x       1x  
/**
 * @file MFKDF Password Factor Setup
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Setup password factor for multi-factor key derivation
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
const defaults = require('../../defaults')
const zxcvbn = require('zxcvbn')
 
/**
 * Setup an MFKDF password factor
 *
 * @example
 * // setup key with password factor
 * const setup = await mfkdf.setup.key([
 *   await mfkdf.setup.factors.password('password')
 * ], {size: 8})
 *
 * // derive key with password factor
 * const derive = await mfkdf.derive.key(setup.policy, {
 *   password: mfkdf.derive.factors.password('password')
 * })
 *
 * setup.key.toString('hex') // -> 01d0c7236adf2516
 * derive.key.toString('hex') // -> 01d0c7236adf2516
 *
 * @param {string} password - The password from which to derive an MFKDF factor
 * @param {Object} [options] - Configuration options
 * @param {string} [options.id='password'] - Unique identifier for this factor
 * @returns {MFKDFFactor} MFKDF factor information
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.8.0
 * @async
 * @memberof setup.factors
 */
async function password (password, options) {
  if (typeof password !== 'string') throw new TypeError('password must be a string')
  if (password.length === 0) throw new RangeError('password cannot be empty')
 
  options = Object.assign(Object.assign({}, defaults.password), options)
 
  if (typeof options.id !== 'string') throw new TypeError('id must be a string')
  if (options.id.length === 0) throw new RangeError('id cannot be empty')
 
  const strength = zxcvbn(password)
 
  return {
    type: 'password',
    id: options.id,
    entropy: Math.log2(strength.guesses),
    data: Buffer.from(password, 'utf-8'),
    params: async () => {
      return {}
    },
    output: async () => {
      return { strength }
    }
  }
}
module.exports.password = password