Skip to content

Commit 1eda08f

Browse files
author
Alvin Dai
committed
feat(sdk-api): gpg encryption for passkey auth
add unit tests TICKET: WP-2733
1 parent 2b96e37 commit 1eda08f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

modules/bitgo/test/unit/bitgo.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,5 +800,85 @@ describe('BitGo Prototype Methods', function () {
800800
assert(e.message.startsWith('Error decrypting message: Could not find signing key with key ID'));
801801
}
802802
});
803+
it('should throw - missing bitgo public key', async () => {
804+
const userId = '123';
805+
const passkey = `{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "${userId}"}}`;
806+
const keyPair = await generateGPGKeyPair('secp256k1');
807+
808+
nock('https://bitgo.fakeurl').persist().get('/api/v1/client/constants').reply(200, { ttl: 3600, constants: {} });
809+
810+
nock('https://bitgo.fakeurl')
811+
.post('/api/auth/v1/session')
812+
.reply(200, async (uri, requestBody) => {
813+
assert(typeof requestBody === 'object');
814+
const encryptedToken = (await encryptAndSignText(
815+
'access_token',
816+
requestBody.publicKey,
817+
keyPair.privateKey
818+
)) as string;
819+
820+
return {
821+
encryptedToken: encryptedToken,
822+
user: { username: '[email protected]' },
823+
};
824+
});
825+
826+
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' });
827+
try {
828+
await bitgo.authenticateWithPasskey(passkey);
829+
assert.fail('Expected error not thrown');
830+
} catch (e) {
831+
assert.equal(e.message, 'Unable to get passkeyBitGoGpgKey');
832+
}
833+
});
834+
it('should throw - invalid userHandle', async () => {
835+
const passkey = `{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": 123}}`;
836+
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' });
837+
try {
838+
await bitgo.validatePasskeyResponse(passkey);
839+
assert.fail('Expected error not thrown');
840+
} catch (e) {
841+
assert.equal(e.message, 'userHandle is missing');
842+
}
843+
});
844+
it('should throw - invalid authenticatorData', async () => {
845+
const passkey = `{"id": "id", "response": { "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}`;
846+
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' });
847+
try {
848+
await bitgo.validatePasskeyResponse(passkey);
849+
assert.fail('Expected error not thrown');
850+
} catch (e) {
851+
assert.equal(e.message, 'authenticatorData is missing');
852+
}
853+
});
854+
it('should throw - invalid passkey json', async () => {
855+
const passkey = `{{"id": "id", "response": { "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}`;
856+
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' });
857+
try {
858+
await bitgo.validatePasskeyResponse(passkey);
859+
assert.fail('Expected error not thrown');
860+
} catch (e) {
861+
console.log(e);
862+
assert(e.message.includes('JSON'));
863+
}
864+
});
865+
it('should throw - missing encrypted token', async () => {
866+
const passkey = `{"id": "id", "response": { "authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}`;
867+
nock('https://bitgo.fakeurl')
868+
.post('/api/auth/v1/session')
869+
.reply(200, async () => {
870+
return {
871+
user: { username: '[email protected]' },
872+
};
873+
});
874+
875+
try {
876+
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' });
877+
await bitgo.authenticateWithPasskey(passkey);
878+
assert.fail('Expected error not thrown');
879+
} catch (e) {
880+
assert.equal(e.message, 'Failed to login. Please contact [email protected]');
881+
}
882+
});
803883
});
804884
});

modules/sdk-api/src/bitgoAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ export class BitGoAPI implements BitGoBase {
974974
const constants = await this.fetchConstants();
975975

976976
if (!constants.passkeyBitGoGpgKey) {
977-
throw new Error('passkeyBitGoGpgKey is missing from constants');
977+
throw new Error('Unable to get passkeyBitGoGpgKey');
978978
}
979979

980980
const access_token = await readSignedMessage(

0 commit comments

Comments
 (0)