|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { registerPasskey } from '../../src/registerPasskey'; |
| 4 | + |
| 5 | +describe('registerPasskey', function () { |
| 6 | + let mockBitGo: { |
| 7 | + get: sinon.SinonStub; |
| 8 | + put: sinon.SinonStub; |
| 9 | + url: sinon.SinonStub; |
| 10 | + }; |
| 11 | + |
| 12 | + const mockChallenge = { |
| 13 | + challenge: btoa('server-challenge-bytes'), |
| 14 | + baseSalt: 'server-base-salt-abc123', |
| 15 | + rp: { id: 'bitgo.com', name: 'BitGo' }, |
| 16 | + user: { id: new Uint8Array([1, 2, 3]), name: 'test@bitgo.com', displayName: 'Test User' }, |
| 17 | + pubKeyCredParams: [{ type: 'public-key' as const, alg: -7 }], |
| 18 | + timeout: 60000, |
| 19 | + }; |
| 20 | + |
| 21 | + const mockOtpResponse = { |
| 22 | + user: { |
| 23 | + otpDevices: [ |
| 24 | + { |
| 25 | + id: 'mongo-device-id-123', |
| 26 | + credentialId: 'cred-id-base64', |
| 27 | + prfSalt: 'server-assigned-prf-salt', |
| 28 | + isPasskey: true, |
| 29 | + extensions: { prf: true }, |
| 30 | + }, |
| 31 | + ], |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + const clientDataJSON = new TextEncoder().encode(JSON.stringify({ type: 'webauthn.create' })); |
| 36 | + const attestationObject = new Uint8Array([0xde, 0xad, 0xbe, 0xef]); |
| 37 | + |
| 38 | + function makeAttestation(prfEnabled = false): PublicKeyCredential { |
| 39 | + return { |
| 40 | + id: 'cred-id-base64', |
| 41 | + rawId: new ArrayBuffer(8), |
| 42 | + type: 'public-key', |
| 43 | + response: { |
| 44 | + clientDataJSON: clientDataJSON.buffer, |
| 45 | + attestationObject: attestationObject.buffer, |
| 46 | + getTransports: () => [], |
| 47 | + } as unknown as AuthenticatorAttestationResponse, |
| 48 | + authenticatorAttachment: null, |
| 49 | + getClientExtensionResults: () => |
| 50 | + prfEnabled |
| 51 | + ? ({ prf: { enabled: true } } as unknown as AuthenticationExtensionsClientOutputs) |
| 52 | + : ({} as AuthenticationExtensionsClientOutputs), |
| 53 | + } as unknown as PublicKeyCredential; |
| 54 | + } |
| 55 | + |
| 56 | + beforeEach(function () { |
| 57 | + mockBitGo = { |
| 58 | + get: sinon.stub(), |
| 59 | + put: sinon.stub(), |
| 60 | + url: sinon.stub().callsFake((path: string, _version?: number) => `/api/v2${path}`), |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(function () { |
| 65 | + sinon.restore(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('with PRF supported', function () { |
| 69 | + it('should include scopes in PUT payload and return prfSupported: true', async function () { |
| 70 | + const attestation = makeAttestation(true); |
| 71 | + |
| 72 | + const mockProvider = { create: sinon.stub().resolves(attestation), get: sinon.stub() }; |
| 73 | + |
| 74 | + mockBitGo.get.returns({ result: sinon.stub().resolves(mockChallenge) }); |
| 75 | + const sendStub = sinon.stub().returns({ result: sinon.stub().resolves(mockOtpResponse) }); |
| 76 | + mockBitGo.put.returns({ send: sendStub }); |
| 77 | + |
| 78 | + const result = await registerPasskey({ |
| 79 | + bitgo: mockBitGo as never, |
| 80 | + provider: mockProvider, |
| 81 | + label: 'My Passkey', |
| 82 | + }); |
| 83 | + |
| 84 | + assert.ok(mockBitGo.get.calledWith('/api/v2/user/otp/webauthn/register'), 'GET should call correct URL'); |
| 85 | + assert.ok(mockBitGo.put.calledWith('/api/v2/user/otp'), 'PUT should call correct URL'); |
| 86 | + |
| 87 | + const putBody = sendStub.firstCall.args[0] as Record<string, unknown>; |
| 88 | + assert.deepStrictEqual(putBody.extensions, ['prf']); |
| 89 | + assert.deepStrictEqual(putBody.scopes, ['wallet_hot']); |
| 90 | + assert.strictEqual(putBody.type, 'webauthn'); |
| 91 | + assert.strictEqual(putBody.label, 'My Passkey'); |
| 92 | + |
| 93 | + const device = mockOtpResponse.user.otpDevices[0]; |
| 94 | + assert.strictEqual(result.id, device.id); |
| 95 | + assert.strictEqual(result.credentialId, device.credentialId); |
| 96 | + assert.strictEqual(result.prfSalt, device.prfSalt); |
| 97 | + assert.strictEqual(result.prfSupported, true); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('without PRF support', function () { |
| 102 | + it('should omit scopes from PUT payload and return prfSupported: false', async function () { |
| 103 | + const mockProvider = { create: sinon.stub().resolves(makeAttestation(false)), get: sinon.stub() }; |
| 104 | + |
| 105 | + mockBitGo.get.returns({ result: sinon.stub().resolves(mockChallenge) }); |
| 106 | + const sendStub = sinon.stub().returns({ result: sinon.stub().resolves(mockOtpResponse) }); |
| 107 | + mockBitGo.put.returns({ send: sendStub }); |
| 108 | + |
| 109 | + const result = await registerPasskey({ |
| 110 | + bitgo: mockBitGo as never, |
| 111 | + provider: mockProvider, |
| 112 | + label: 'My Passkey No PRF', |
| 113 | + }); |
| 114 | + |
| 115 | + const putBody = sendStub.firstCall.args[0] as Record<string, unknown>; |
| 116 | + assert.ok(putBody.extensions === undefined, 'extensions should be omitted when PRF is not supported'); |
| 117 | + assert.ok(putBody.scopes === undefined, 'scopes should be omitted when PRF is not supported'); |
| 118 | + assert.strictEqual(result.prfSupported, false); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('baseSalt sourcing', function () { |
| 123 | + it('should call GET challenge before provider.create()', async function () { |
| 124 | + const callOrder: string[] = []; |
| 125 | + |
| 126 | + const mockProvider = { |
| 127 | + create: sinon.stub().callsFake(() => { |
| 128 | + callOrder.push('provider.create'); |
| 129 | + return Promise.resolve(makeAttestation()); |
| 130 | + }), |
| 131 | + get: sinon.stub(), |
| 132 | + }; |
| 133 | + |
| 134 | + mockBitGo.get.returns({ |
| 135 | + result: sinon.stub().callsFake(() => { |
| 136 | + callOrder.push('GET challenge'); |
| 137 | + return Promise.resolve(mockChallenge); |
| 138 | + }), |
| 139 | + }); |
| 140 | + mockBitGo.put.returns({ send: sinon.stub().returns({ result: sinon.stub().resolves(mockOtpResponse) }) }); |
| 141 | + |
| 142 | + await registerPasskey({ bitgo: mockBitGo as never, provider: mockProvider, label: 'Test' }); |
| 143 | + |
| 144 | + assert.deepStrictEqual(callOrder, ['GET challenge', 'provider.create']); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments