derive/factors/uuid.js

  1. /**
  2. * @file MFKDF UUID Factor Derivation
  3. * @copyright Multifactor 2022 All Rights Reserved
  4. *
  5. * @description
  6. * Derive UUID factor for multi-factor key derivation
  7. *
  8. * @author Vivek Nair (https://nair.me) <vivek@nair.me>
  9. */
  10. const { validate: uuidValidate, parse: uuidParse } = require('uuid')
  11. /**
  12. * Derive an MFKDF UUID factor
  13. *
  14. * @example
  15. * // setup key with uuid factor
  16. * const setup = await mfkdf.setup.key([
  17. * await mfkdf.setup.factors.uuid({ uuid: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d' })
  18. * ], {size: 8})
  19. *
  20. * // derive key with uuid factor
  21. * const derive = await mfkdf.derive.key(setup.policy, {
  22. * uuid: mfkdf.derive.factors.uuid('9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d')
  23. * })
  24. *
  25. * setup.key.toString('hex') // -> 01d0c7236adf2516
  26. * derive.key.toString('hex') // -> 01d0c7236adf2516
  27. *
  28. * @param {string} uuid - The uuid from which to derive an MFKDF factor
  29. * @returns {function(config:Object): Promise<MFKDFFactor>} Async function to generate MFKDF factor information
  30. * @author Vivek Nair (https://nair.me) <vivek@nair.me>
  31. * @since 0.9.0
  32. * @memberof derive.factors
  33. */
  34. function uuid (uuid) {
  35. if (typeof uuid !== 'string') throw new TypeError('uuid must be a string')
  36. if (!uuidValidate(uuid)) throw new TypeError('uuid is not a valid uuid')
  37. return async () => {
  38. return {
  39. type: 'uuid',
  40. data: Buffer.from(uuidParse(uuid)),
  41. params: async () => {
  42. return {}
  43. },
  44. output: async () => {
  45. return { uuid }
  46. }
  47. }
  48. }
  49. }
  50. module.exports.uuid = uuid