-
Notifications
You must be signed in to change notification settings - Fork 299
feat(wp): gpg encryption for passkey auth #5013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,9 @@ | |
| import * as crypto from 'crypto'; | ||
| import * as nock from 'nock'; | ||
| import * as should from 'should'; | ||
| import assert = require('assert'); | ||
|
|
||
| import { common } from '@bitgo/sdk-core'; | ||
| import { common, generateGPGKeyPair, encryptAndSignText } from '@bitgo/sdk-core'; | ||
| import { bip32, ECPair } from '@bitgo/utxo-lib'; | ||
| import * as _ from 'lodash'; | ||
| import * as BitGoJS from '../../src/index'; | ||
|
|
@@ -687,4 +688,197 @@ describe('BitGo Prototype Methods', function () { | |
| response.user.ecdhKeychain.should.equal('some-xpub'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('passkey authentication', () => { | ||
| afterEach(function ensureNoPendingMocks() { | ||
| nock.cleanAll(); | ||
| nock.pendingMocks().should.be.empty(); | ||
| }); | ||
|
|
||
| it('should authenticate with a passkey', async () => { | ||
| const userId = '123'; | ||
| const passkey = `{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "${userId}"}}`; | ||
| const keyPair = await generateGPGKeyPair('secp256k1'); | ||
|
|
||
| nock('https://bitgo.fakeurl') | ||
| .persist() | ||
| .get('/api/v1/client/constants') | ||
| .reply(200, { ttl: 3600, constants: { passkeyBitGoGpgKey: keyPair.publicKey } }); | ||
|
|
||
| nock('https://bitgo.fakeurl') | ||
| .post('/api/auth/v1/session') | ||
| .reply(200, async (uri, requestBody) => { | ||
| assert(typeof requestBody === 'object'); | ||
| should.exist(requestBody.publicKey); | ||
| should.exist(requestBody.userId); | ||
| should.exist(requestBody.passkey); | ||
| requestBody.userId.should.equal(userId); | ||
| requestBody.passkey.should.equal(passkey); | ||
| const encryptedToken = (await encryptAndSignText( | ||
| 'access_token', | ||
| requestBody.publicKey, | ||
| keyPair.privateKey | ||
| )) as string; | ||
|
|
||
| return { | ||
| encryptedToken: encryptedToken, | ||
| user: { username: 'auth-test@bitgo.com' }, | ||
| }; | ||
| }); | ||
|
|
||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| const response = await bitgo.authenticateWithPasskey(passkey); | ||
| should.exist(response.access_token); | ||
| response.access_token.should.equal('access_token'); | ||
| }); | ||
|
|
||
| it('should not authenticate with wrong encryption key', async () => { | ||
| const keyPair = await generateGPGKeyPair('secp256k1'); | ||
|
|
||
| nock('https://bitgo.fakeurl') | ||
| .persist() | ||
| .get('/api/v1/client/constants') | ||
| .reply(200, { ttl: 3600, constants: { passkeyBitGoGpgKey: keyPair.publicKey } }); | ||
| nock('https://bitgo.fakeurl') | ||
| .post('/api/auth/v1/session') | ||
| .reply(200, async () => { | ||
| const keyPair = await generateGPGKeyPair('secp256k1'); | ||
| const encryptedToken = (await encryptAndSignText( | ||
| 'access_token', | ||
| keyPair.publicKey, | ||
| keyPair.privateKey | ||
| )) as string; | ||
| return { | ||
| encryptedToken: encryptedToken, | ||
| user: { username: 'auth-test@bitgo.com' }, | ||
| }; | ||
| }); | ||
|
|
||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| try { | ||
| await bitgo.authenticateWithPasskey( | ||
| '{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}' | ||
| ); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'Error decrypting message: Session key decryption failed.'); | ||
| } | ||
| }); | ||
|
|
||
| it('should not authenticate with wrong signing key', async () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add empty lines between tests, that looks more readable |
||
| const userId = '123'; | ||
| const passkey = `{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "${userId}"}}`; | ||
| const badKeyPair = await generateGPGKeyPair('secp256k1'); | ||
| const bitgoKeyPair = await generateGPGKeyPair('secp256k1'); | ||
|
|
||
| nock('https://bitgo.fakeurl') | ||
| .persist() | ||
| .get('/api/v1/client/constants') | ||
| .reply(200, { ttl: 3600, constants: { passkeyBitGoGpgKey: bitgoKeyPair.publicKey } }); | ||
|
|
||
| nock('https://bitgo.fakeurl') | ||
| .post('/api/auth/v1/session') | ||
| .reply(200, async (uri, requestBody) => { | ||
| assert(typeof requestBody === 'object'); | ||
| const encryptedToken = (await encryptAndSignText( | ||
| 'access_token', | ||
| requestBody.publicKey, | ||
| badKeyPair.privateKey | ||
| )) as string; | ||
|
|
||
| return { | ||
| encryptedToken: encryptedToken, | ||
| user: { username: 'auth-test@bitgo.com' }, | ||
| }; | ||
| }); | ||
|
|
||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| try { | ||
| await bitgo.authenticateWithPasskey(passkey); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| assert(e.message.startsWith('Error decrypting message: Could not find signing key with key ID')); | ||
| } | ||
| }); | ||
| it('should throw - missing bitgo public key', async () => { | ||
| const userId = '123'; | ||
| const passkey = `{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "${userId}"}}`; | ||
| const keyPair = await generateGPGKeyPair('secp256k1'); | ||
|
|
||
| nock('https://bitgo.fakeurl').persist().get('/api/v1/client/constants').reply(200, { ttl: 3600, constants: {} }); | ||
|
|
||
| nock('https://bitgo.fakeurl') | ||
| .post('/api/auth/v1/session') | ||
| .reply(200, async (uri, requestBody) => { | ||
| assert(typeof requestBody === 'object'); | ||
| const encryptedToken = (await encryptAndSignText( | ||
| 'access_token', | ||
| requestBody.publicKey, | ||
| keyPair.privateKey | ||
| )) as string; | ||
|
|
||
| return { | ||
| encryptedToken: encryptedToken, | ||
| user: { username: 'auth-test@bitgo.com' }, | ||
| }; | ||
| }); | ||
|
|
||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| try { | ||
| await bitgo.authenticateWithPasskey(passkey); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'Unable to get passkeyBitGoGpgKey'); | ||
| } | ||
| }); | ||
| it('should throw - invalid userHandle', async () => { | ||
| const passkey = `{"id": "id", "response": {"authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": 123}}`; | ||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| try { | ||
| await bitgo.validatePasskeyResponse(passkey); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'userHandle is missing'); | ||
| } | ||
| }); | ||
| it('should throw - invalid authenticatorData', async () => { | ||
| const passkey = `{"id": "id", "response": { "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}`; | ||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| try { | ||
| await bitgo.validatePasskeyResponse(passkey); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'authenticatorData is missing'); | ||
| } | ||
| }); | ||
| it('should throw - invalid passkey json', async () => { | ||
| const passkey = `{{"id": "id", "response": { "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}`; | ||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| try { | ||
| await bitgo.validatePasskeyResponse(passkey); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| console.log(e); | ||
| assert(e.message.includes('JSON')); | ||
| } | ||
| }); | ||
| it('should throw - missing encrypted token', async () => { | ||
| const passkey = `{"id": "id", "response": { "authenticatorData": "123", "clientDataJSON": "123", "signature": "123", "userHandle": "123"}}`; | ||
| nock('https://bitgo.fakeurl') | ||
| .post('/api/auth/v1/session') | ||
| .reply(200, async () => { | ||
| return { | ||
| user: { username: 'auth-test@bitgo.com' }, | ||
| }; | ||
| }); | ||
|
|
||
| try { | ||
| const bitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); | ||
| await bitgo.authenticateWithPasskey(passkey); | ||
| assert.fail('Expected error not thrown'); | ||
| } catch (e) { | ||
| assert.equal(e.message, 'Failed to login. Please contact support@bitgo.com'); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename the one of the keyPair object, it looks very confusing