All files / src/policy validate.js

100% Statements 10/10
100% Branches 2/2
100% Functions 2/2
100% Lines 9/9

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 65 66 67 68 69 70 71 72 73 74                                                                      150x 150x 277x 277x   150x   1x                                                     41x 41x   1x  
/**
 * @file MFKDF Policy Validate
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Determine whether key can be derived from given factors
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
/**
 * Get all ids of multi-factor derived key factors (including factors of stacked keys)
 *
 * @example
 * // setup key that can be derived from passwordA AND (passwordB OR passwordC)
 * const setup = await mfkdf.policy.setup(
 *   await mfkdf.policy.and(
 *     await mfkdf.setup.factors.password('passwordA', { id: 'passwordA' }),
 *     await mfkdf.policy.or(
 *       await mfkdf.setup.factors.password('passwordB', { id: 'passwordB' }),
 *       await mfkdf.setup.factors.password('passwordC', { id: 'passwordC' })
 *     )
 *   )
 * )
 *
 * // get list of ids
 * const ids = mfkdf.policy.ids(setup.policy) // -> ['passwordA', 'passwordB', 'passwordC', ...]
 *
 * @param {Object} policy - Policy used to derive a key
 * @returns {Array.<string>} The ids of the provided factors
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @memberOf policy
 */
function ids (policy) {
  let list = []
  for (const factor of policy.factors) {
    list.push(factor.id)
    if (factor.type === 'stack') list = list.concat(ids(factor.params))
  }
  return list
}
module.exports.ids = ids
 
/**
 * Validate multi-factor derived key policy
 *
 * @example
 * // setup key that can be derived from passwordA AND (passwordB OR passwordC)
 * const setup = await mfkdf.policy.setup(
 *   await mfkdf.policy.and(
 *     await mfkdf.setup.factors.password('passwordA', { id: 'passwordA' }),
 *     await mfkdf.policy.or(
 *       await mfkdf.setup.factors.password('passwordB', { id: 'passwordB' }),
 *       await mfkdf.setup.factors.password('passwordC', { id: 'passwordC' })
 *     )
 *   )
 * )
 *
 * // validate policy
 * const valid = mfkdf.policy.validate(setup.policy) // -> true
 *
 * @param {Object} policy - Policy used to derive a key
 * @returns {boolean} Whether the policy is valid
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @memberOf policy
 */
function validate (policy) {
  const list = ids(policy)
  return ((new Set(list)).size === list.length)
}
module.exports.validate = validate