|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { buildEvalByCredential, matchDeviceByCredentialId } from '../../src'; |
| 3 | +import { WebauthnDevice } from '@bitgo/public-types'; |
| 4 | + |
| 5 | +const device1: WebauthnDevice = { |
| 6 | + otpDeviceId: 'oid-1', |
| 7 | + authenticatorInfo: { credID: 'cred-aaa', fmt: 'none', publicKey: 'pk-1' }, |
| 8 | + prfSalt: 'salt-aaa', |
| 9 | + encryptedPrv: 'enc-prv-1', |
| 10 | +}; |
| 11 | + |
| 12 | +const device2: WebauthnDevice = { |
| 13 | + otpDeviceId: 'oid-2', |
| 14 | + authenticatorInfo: { credID: 'cred-bbb', fmt: 'none', publicKey: 'pk-2' }, |
| 15 | + prfSalt: 'salt-bbb', |
| 16 | + encryptedPrv: 'enc-prv-2', |
| 17 | +}; |
| 18 | + |
| 19 | +describe('buildEvalByCredential', function () { |
| 20 | + it('maps each device credID to its prfSalt in evalByCredential', function () { |
| 21 | + const { evalByCredential } = buildEvalByCredential([device1, device2]); |
| 22 | + assert.deepStrictEqual(evalByCredential, { 'cred-aaa': 'salt-aaa', 'cred-bbb': 'salt-bbb' }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('populates credIdToDevice with both devices', function () { |
| 26 | + const { credIdToDevice } = buildEvalByCredential([device1, device2]); |
| 27 | + assert.strictEqual(credIdToDevice.get('cred-aaa'), device1); |
| 28 | + assert.strictEqual(credIdToDevice.get('cred-bbb'), device2); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns empty maps for an empty device list', function () { |
| 32 | + const { evalByCredential, credIdToDevice } = buildEvalByCredential([]); |
| 33 | + assert.deepStrictEqual(evalByCredential, {}); |
| 34 | + assert.strictEqual(credIdToDevice.size, 0); |
| 35 | + }); |
| 36 | + |
| 37 | + it('skips devices with empty prfSalt', function () { |
| 38 | + const deviceNoPrf = { ...device1, prfSalt: '' }; |
| 39 | + const { evalByCredential, credIdToDevice } = buildEvalByCredential([deviceNoPrf, device2]); |
| 40 | + assert.deepStrictEqual(evalByCredential, { 'cred-bbb': 'salt-bbb' }); |
| 41 | + assert.strictEqual(credIdToDevice.has('cred-aaa'), false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('skips devices with undefined prfSalt', function () { |
| 45 | + const deviceNoPrf = { ...device1, prfSalt: undefined as unknown as string }; |
| 46 | + const { evalByCredential, credIdToDevice } = buildEvalByCredential([deviceNoPrf, device2]); |
| 47 | + assert.deepStrictEqual(evalByCredential, { 'cred-bbb': 'salt-bbb' }); |
| 48 | + assert.strictEqual(credIdToDevice.has('cred-aaa'), false); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('matchDeviceByCredentialId', function () { |
| 53 | + it('returns the matching device', function () { |
| 54 | + assert.strictEqual(matchDeviceByCredentialId([device1, device2], 'cred-bbb'), device2); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns the first device when it matches', function () { |
| 58 | + assert.strictEqual(matchDeviceByCredentialId([device1, device2], 'cred-aaa'), device1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns a device even when it has no prfSalt', function () { |
| 62 | + const deviceNoPrf = { ...device1, prfSalt: '' }; |
| 63 | + assert.strictEqual(matchDeviceByCredentialId([deviceNoPrf, device2], 'cred-aaa'), deviceNoPrf); |
| 64 | + }); |
| 65 | + |
| 66 | + it('throws the expected error message when no device matches', function () { |
| 67 | + assert.throws( |
| 68 | + () => matchDeviceByCredentialId([device1, device2], 'cred-unknown'), |
| 69 | + (err: Error) => { |
| 70 | + assert.strictEqual(err.message, 'Could not identify which passkey device was used'); |
| 71 | + return true; |
| 72 | + } |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('throws when the device list is empty', function () { |
| 77 | + assert.throws(() => matchDeviceByCredentialId([], 'cred-aaa'), Error); |
| 78 | + }); |
| 79 | +}); |
0 commit comments