All files / src/policy logic.js

100% Statements 13/13
100% Branches 0/0
100% Functions 5/5
100% Lines 13/13

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                    1x 1x                                                                     27x   1x                                                                     28x   1x                                                                 2x   1x                                                             4x   1x                                                                 62x 62x   1x  
/**
 * @file MFKDF Policy Logic
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Logical operators for MFKDF policy establishment
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
const stack = require('../setup/factors/stack').stack
const { v4: uuidv4 } = require('uuid')
 
/**
 * Create a MFKDF factor based on OR of two MFKDF factors
 *
 * @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' })
 *     )
 *   ), { size: 8 }
 * )
 *
 * // derive key with passwordA and passwordC (or passwordA and passwordB)
 * const derive = await mfkdf.policy.derive(setup.policy, {
 *   passwordA: mfkdf.derive.factors.password('passwordA'),
 *   passwordC: mfkdf.derive.factors.password('passwordC'),
 * })
 *
 * setup.key.toString('hex') // -> e16a227944a65263
 * derive.key.toString('hex') // -> e16a227944a65263
 *
 * @param {MFKDFFactor} factor1 - The first factor input to the OR policy
 * @param {MFKDFFactor} factor2 - The second factor input to the OR policy
 * @returns {MFKDFFactor} Factor that can be derived with either factor
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @async
 * @memberOf policy
 */
async function or (factor1, factor2) {
  return await atLeast(1, [factor1, factor2])
}
module.exports.or = or
 
/**
 * Create a MFKDF factor based on AND of two MFKDF factors
 *
 * @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' })
 *     )
 *   ), { size: 8 }
 * )
 *
 * // derive key with passwordA and passwordC (or passwordA and passwordB)
 * const derive = await mfkdf.policy.derive(setup.policy, {
 *   passwordA: mfkdf.derive.factors.password('passwordA'),
 *   passwordC: mfkdf.derive.factors.password('passwordC'),
 * })
 *
 * setup.key.toString('hex') // -> e16a227944a65263
 * derive.key.toString('hex') // -> e16a227944a65263
 *
 * @param {MFKDFFactor} factor1 - The first factor input to the AND policy
 * @param {MFKDFFactor} factor2 - The second factor input to the AND policy
 * @returns {MFKDFFactor} Factor that can be derived with both factors
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @async
 * @memberOf policy
 */
async function and (factor1, factor2) {
  return await atLeast(2, [factor1, factor2])
}
module.exports.and = and
 
/**
 * Create a MFKDF factor based on ALL of the provided MFKDF factors
 *
 * @example
 * // setup key that can be derived from passwordA AND passwordB AND passwordC
 * const setup = await mfkdf.policy.setup(
 *   await mfkdf.policy.all([
 *     await mfkdf.setup.factors.password('passwordA', { id: 'passwordA' }),
 *     await mfkdf.setup.factors.password('passwordB', { id: 'passwordB' }),
 *     await mfkdf.setup.factors.password('passwordC', { id: 'passwordC' })
 *   ]), { size: 8 }
 * )
 *
 * // derive key with passwordA and passwordB and passwordC
 * const derive = await mfkdf.policy.derive(setup.policy, {
 *   passwordA: mfkdf.derive.factors.password('passwordA'),
 *   passwordB: mfkdf.derive.factors.password('passwordB'),
 *   passwordC: mfkdf.derive.factors.password('passwordC'),
 * })
 *
 * setup.key.toString('hex') // -> e16a227944a65263
 * derive.key.toString('hex') // -> e16a227944a65263
 *
 * @param {Array.<MFKDFFactor>} factors - The factor inputs to the ALL policy
 * @returns {MFKDFFactor} Factor that can be derived with all factors
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @async
 * @memberOf policy
 */
async function all (factors) {
  return await atLeast(factors.length, factors)
}
module.exports.all = all
 
/**
 * Create a MFKDF factor based on ANY of the provided MFKDF factors
 *
 * @example
 * // setup key that can be derived from passwordA OR passwordB OR passwordC
 * const setup = await mfkdf.policy.setup(
 *   await mfkdf.policy.any([
 *     await mfkdf.setup.factors.password('passwordA', { id: 'passwordA' }),
 *     await mfkdf.setup.factors.password('passwordB', { id: 'passwordB' }),
 *     await mfkdf.setup.factors.password('passwordC', { id: 'passwordC' })
 *   ]), { size: 8 }
 * )
 *
 * // derive key with passwordA (or passwordB or passwordC)
 * const derive = await mfkdf.policy.derive(setup.policy, {
 *   passwordB: mfkdf.derive.factors.password('passwordB')
 * })
 *
 * setup.key.toString('hex') // -> e16a227944a65263
 * derive.key.toString('hex') // -> e16a227944a65263
 *
 * @param {Array.<MFKDFFactor>} factors - The factor inputs to the ANY policy
 * @returns {MFKDFFactor} Factor that can be derived with any factor
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @async
 * @memberOf policy
 */
async function any (factors) {
  return await atLeast(1, factors)
}
module.exports.any = any
 
/**
 * Create a MFKDF factor based on at least some number of the provided MFKDF factors
 *
 * @example
 * // setup key that can be derived from at least 2 of (passwordA, passwordB, passwordC)
 * const setup = await mfkdf.policy.setup(
 *   await mfkdf.policy.any([
 *     await mfkdf.setup.factors.password('passwordA', { id: 'passwordA' }),
 *     await mfkdf.setup.factors.password('passwordB', { id: 'passwordB' }),
 *     await mfkdf.setup.factors.password('passwordC', { id: 'passwordC' })
 *   ]), { size: 8 }
 * )
 *
 * // derive key with passwordA and passwordB (or passwordA and passwordC, or passwordB and passwordC)
 * const derive = await mfkdf.policy.derive(setup.policy, {
 *   passwordA: mfkdf.derive.factors.password('passwordA'),
 *   passwordB: mfkdf.derive.factors.password('passwordB')
 * })
 *
 * setup.key.toString('hex') // -> e16a227944a65263
 * derive.key.toString('hex') // -> e16a227944a65263
 *
 * @param {number} n - The number of factors to be required
 * @param {Array.<MFKDFFactor>} factors - The factor inputs to the atLeast(#) policy
 * @returns {MFKDFFactor} Factor that can be derived with at least n of the given factors
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.16.0
 * @async
 * @memberOf policy
 */
async function atLeast (n, factors) {
  const id = uuidv4()
  return await stack(factors, { threshold: n, id })
}
module.exports.atLeast = atLeast