All files / src/derive/factors passkey.js

100% Statements 9/9
100% Branches 4/4
100% Functions 4/4
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                                                                  4x 1x   3x 1x     2x 2x       2x     2x         1x  
/**
 * @file MFKDF Passkey Factor Derivation
 * @copyright Multifactor, Inc. 2022–2025
 *
 * @description
 * Derive passkey factor for multi-factor key derivation
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
/**
 * Derive an MFKDF passkey factor
 *
 * @example
 *  const prf = await crypto.randomBytes(32)
 *
 *  const setup = await mfkdf.setup.key([
 *    await mfkdf.setup.factors.passkey(prf)
 *  ])
 *
 *  const derive = await mfkdf.derive.key(setup.policy, {
 *    passkey: mfkdf.derive.factors.passkey(prf)
 *  })
 *
 *  derive.key.toString('hex').should.equal(setup.key.toString('hex'))
 *
 * @param {Buffer} secret - The 256-bit PRF secret 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 2.0.0
 * @memberof derive.factors
 */
function passkey (secret) {
  if (!Buffer.isBuffer(secret)) {
    throw new TypeError('secret must be a Buffer')
  }
  if (Buffer.byteLength(secret) !== 32) {
    throw new RangeError('secret must be 32 bytes (256 bits) in length')
  }
 
  return async () => {
    return {
      type: 'passkey',
      data: secret,
      params: async () => {
        return {}
      },
      output: async () => {
        return {}
      }
    }
  }
}
module.exports.passkey = passkey