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