diff options
author | Matthew Miller <matthew@millerti.me> | 2020-07-30 18:13:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-30 18:13:55 -0700 |
commit | 34752ae8633895626099ea9111fd4d551f516ab1 (patch) | |
tree | 09f0e72869a7f63954b6f813eb18db085eae5315 /packages/browser/src | |
parent | e9ef3215693225203920e39707cc6abeca25ae44 (diff) | |
parent | 92425fbfa00420d24b32901c4fc2b654f2005a52 (diff) |
Merge pull request #42 from MasterKale/feature/better-challenges
feature/better-challenges
Diffstat (limited to 'packages/browser/src')
-rw-r--r-- | packages/browser/src/methods/startAssertion.test.ts | 31 | ||||
-rw-r--r-- | packages/browser/src/methods/startAssertion.ts | 10 | ||||
-rw-r--r-- | packages/browser/src/methods/startAttestation.test.ts | 19 | ||||
-rw-r--r-- | packages/browser/src/methods/startAttestation.ts | 9 |
4 files changed, 45 insertions, 24 deletions
diff --git a/packages/browser/src/methods/startAssertion.test.ts b/packages/browser/src/methods/startAssertion.test.ts index e919d18..996f66a 100644 --- a/packages/browser/src/methods/startAssertion.test.ts +++ b/packages/browser/src/methods/startAssertion.test.ts @@ -4,6 +4,8 @@ import { } from '@simplewebauthn/typescript-types'; import supportsWebauthn from '../helpers/supportsWebauthn'; +import toUint8Array from '../helpers/toUint8Array'; +import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import startAssertion from './startAssertion'; @@ -19,7 +21,7 @@ const mockUserHandle = 'mockUserHandle'; // With ASCII challenge const goodOpts1: PublicKeyCredentialRequestOptionsJSON = { - challenge: 'fizz', + challenge: bufferToBase64URLString(toUint8Array('fizz')), allowCredentials: [ { id: 'C0VGlvYFratUdAV1iCw-ULpUW8E-exHPXQChBfyVeJZCMfjMFcwDmOFgoMUz39LoMtCJUBW8WPlLkGT6q8qTCg', @@ -32,7 +34,7 @@ const goodOpts1: PublicKeyCredentialRequestOptionsJSON = { // With UTF-8 challenge const goodOpts2UTF8: PublicKeyCredentialRequestOptionsJSON = { - challenge: 'やれやれだぜ', + challenge: bufferToBase64URLString(toUint8Array('やれやれだぜ')), allowCredentials: [], timeout: 1, }; @@ -62,7 +64,7 @@ test('should convert options before passing to navigator.credentials.get(...)', const argsPublicKey = mockNavigatorGet.mock.calls[0][0].publicKey; const credId = argsPublicKey.allowCredentials[0].id; - expect(JSON.stringify(argsPublicKey.challenge)).toEqual('{"0":102,"1":105,"2":122,"3":122}'); + expect(new Uint8Array(argsPublicKey.challenge)).toEqual(new Uint8Array([102, 105, 122, 122])); // Make sure the credential ID is an ArrayBuffer with a length of 64 expect(credId instanceof ArrayBuffer).toEqual(true); expect(credId.byteLength).toEqual(64); @@ -148,8 +150,27 @@ test('should handle UTF-8 challenges', async done => { const argsPublicKey = mockNavigatorGet.mock.calls[0][0].publicKey; - expect(JSON.stringify(argsPublicKey.challenge)).toEqual( - '{"0":227,"1":130,"2":132,"3":227,"4":130,"5":140,"6":227,"7":130,"8":132,"9":227,"10":130,"11":140,"12":227,"13":129,"14":160,"15":227,"16":129,"17":156}', + expect(new Uint8Array(argsPublicKey.challenge)).toEqual( + new Uint8Array([ + 227, + 130, + 132, + 227, + 130, + 140, + 227, + 130, + 132, + 227, + 130, + 140, + 227, + 129, + 160, + 227, + 129, + 156, + ]), ); done(); diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index b65325b..09e416f 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -4,8 +4,8 @@ import { AssertionCredentialJSON, } from '@simplewebauthn/typescript-types'; -import toUint8Array from '../helpers/toUint8Array'; import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; +import base64URLStringToBuffer from '../helpers/base64URLStringToBuffer'; import supportsWebauthn from '../helpers/supportsWebauthn'; import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor'; @@ -24,14 +24,12 @@ export default async function startAssertion( // We need to convert some values to Uint8Arrays before passing the credentials to the navigator const publicKey: PublicKeyCredentialRequestOptions = { ...requestOptionsJSON, - challenge: toUint8Array(requestOptionsJSON.challenge), - allowCredentials: requestOptionsJSON.allowCredentials.map( - toPublicKeyCredentialDescriptor, - ), + challenge: base64URLStringToBuffer(requestOptionsJSON.challenge), + allowCredentials: requestOptionsJSON.allowCredentials.map(toPublicKeyCredentialDescriptor), }; // Wait for the user to complete assertion - const credential = await navigator.credentials.get({ publicKey }) as AssertionCredential; + const credential = (await navigator.credentials.get({ publicKey })) as AssertionCredential; if (!credential) { throw new Error('Assertion was not completed'); diff --git a/packages/browser/src/methods/startAttestation.test.ts b/packages/browser/src/methods/startAttestation.test.ts index bf6ab9b..0723213 100644 --- a/packages/browser/src/methods/startAttestation.test.ts +++ b/packages/browser/src/methods/startAttestation.test.ts @@ -5,6 +5,7 @@ import { import toUint8Array from '../helpers/toUint8Array'; import supportsWebauthn from '../helpers/supportsWebauthn'; +import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import startAttestation from './startAttestation'; @@ -17,7 +18,7 @@ const mockAttestationObject = 'mockAtte'; const mockClientDataJSON = 'mockClie'; const goodOpts1: PublicKeyCredentialCreationOptionsJSON = { - challenge: 'fizz', + challenge: bufferToBase64URLString(toUint8Array('fizz')), attestation: 'direct', pubKeyCredParams: [ { @@ -35,11 +36,13 @@ const goodOpts1: PublicKeyCredentialCreationOptionsJSON = { name: 'username', }, timeout: 1, - excludeCredentials: [{ - id: 'C0VGlvYFratUdAV1iCw-ULpUW8E-exHPXQChBfyVeJZCMfjMFcwDmOFgoMUz39LoMtCJUBW8WPlLkGT6q8qTCg', - type: 'public-key', - transports: ['internal'], - }], + excludeCredentials: [ + { + id: 'C0VGlvYFratUdAV1iCw-ULpUW8E-exHPXQChBfyVeJZCMfjMFcwDmOFgoMUz39LoMtCJUBW8WPlLkGT6q8qTCg', + type: 'public-key', + transports: ['internal'], + }, + ], }; beforeEach(() => { @@ -65,8 +68,8 @@ test('should convert options before passing to navigator.credentials.create(...) const credId = argsPublicKey.excludeCredentials[0].id; // Make sure challenge and user.id are converted to Buffers - expect(JSON.stringify(argsPublicKey.challenge)).toEqual('{"0":102,"1":105,"2":122,"3":122}'); - expect(JSON.stringify(argsPublicKey.user.id)).toEqual('{"0":53,"1":54,"2":55,"3":56}'); + expect(new Uint8Array(argsPublicKey.challenge)).toEqual(new Uint8Array([102, 105, 122, 122])); + expect(new Uint8Array(argsPublicKey.user.id)).toEqual(new Uint8Array([53, 54, 55, 56])); // Confirm construction of excludeCredentials array expect(credId instanceof ArrayBuffer).toEqual(true); diff --git a/packages/browser/src/methods/startAttestation.ts b/packages/browser/src/methods/startAttestation.ts index d5e540f..b6faa83 100644 --- a/packages/browser/src/methods/startAttestation.ts +++ b/packages/browser/src/methods/startAttestation.ts @@ -6,6 +6,7 @@ import { import toUint8Array from '../helpers/toUint8Array'; import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; +import base64URLStringToBuffer from '../helpers/base64URLStringToBuffer'; import supportsWebauthn from '../helpers/supportsWebauthn'; import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor'; @@ -24,18 +25,16 @@ export default async function startAttestation( // We need to convert some values to Uint8Arrays before passing the credentials to the navigator const publicKey: PublicKeyCredentialCreationOptions = { ...creationOptionsJSON, - challenge: toUint8Array(creationOptionsJSON.challenge), + challenge: base64URLStringToBuffer(creationOptionsJSON.challenge), user: { ...creationOptionsJSON.user, id: toUint8Array(creationOptionsJSON.user.id), }, - excludeCredentials: creationOptionsJSON.excludeCredentials.map( - toPublicKeyCredentialDescriptor, - ), + excludeCredentials: creationOptionsJSON.excludeCredentials.map(toPublicKeyCredentialDescriptor), }; // Wait for the user to complete attestation - const credential = await navigator.credentials.create({ publicKey }) as AttestationCredential; + const credential = (await navigator.credentials.create({ publicKey })) as AttestationCredential; if (!credential) { throw new Error('Attestation was not completed'); |