All files / src/derive/factors uuid.js

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

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                  1x                                                   43x 42x   41x 41x       40x     40x         1x  
/**
 * @file MFKDF UUID Factor Derivation
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Derive UUID factor for multi-factor key derivation
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
const { validate: uuidValidate, parse: uuidParse } = require('uuid')
 
/**
 * Derive an MFKDF UUID factor
 *
 * @example
 * // setup key with uuid factor
 * const setup = await mfkdf.setup.key([
 *   await mfkdf.setup.factors.uuid({ uuid: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d' })
 * ], {size: 8})
 *
 * // derive key with uuid factor
 * const derive = await mfkdf.derive.key(setup.policy, {
 *   uuid: mfkdf.derive.factors.uuid('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d')
 * })
 *
 * setup.key.toString('hex') // -> 01d0c7236adf2516
 * derive.key.toString('hex') // -> 01d0c7236adf2516
 *
 * @param {string} uuid - The uuid 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 0.9.0
 * @memberof derive.factors
 */
function uuid (uuid) {
  if (typeof uuid !== 'string') throw new TypeError('uuid must be a string')
  if (!uuidValidate(uuid)) throw new TypeError('uuid is not a valid uuid')
 
  return async () => {
    return {
      type: 'uuid',
      data: Buffer.from(uuidParse(uuid)),
      params: async () => {
        return {}
      },
      output: async () => {
        return { uuid }
      }
    }
  }
}
module.exports.uuid = uuid