All files / src/setup/factors uuid.js

100% Statements 17/17
100% Branches 10/10
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                  1x 1x                                                         56x   56x 55x   54x 54x 53x   52x           52x     51x       1x  
/**
 * @file MFKDF UUID Factor Setup
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Setup UUID factor for multi-factor key derivation
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
const defaults = require('../../defaults')
const { v4: uuidv4, validate: uuidValidate, parse: uuidParse } = require('uuid')
 
/**
 * Setup 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 {Object} [options] - Configuration options
 * @param {string} [options.uuid] - UUID to use for this factor; random v4 uuid default
 * @param {string} [options.id='uuid'] - Unique identifier for this factor
 * @returns {MFKDFFactor} MFKDF factor information
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.9.0
 * @async
 * @memberof setup.factors
 */
async function uuid (options) {
  options = Object.assign(Object.assign({}, defaults.uuid), 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')
 
  if (typeof options.uuid === 'undefined') options.uuid = uuidv4()
  if (typeof options.uuid !== 'string') throw new TypeError('uuid must be a string')
  if (!uuidValidate(options.uuid)) throw new TypeError('uuid is not a valid uuid')
 
  return {
    type: 'uuid',
    id: options.id,
    entropy: 122,
    data: Buffer.from(uuidParse(options.uuid)),
    params: async () => {
      return {}
    },
    output: async () => {
      return { uuid: options.uuid }
    }
  }
}
module.exports.uuid = uuid