From db28323378648782d92f6552d478a4132d58dcdc Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 1 Jun 2020 16:16:13 -0700 Subject: Refactor browser methods to return near-raw resp. --- packages/browser/src/methods/startAssertion.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'packages/browser/src/methods/startAssertion.ts') diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index 093cf30..1590634 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -1,7 +1,7 @@ import { PublicKeyCredentialRequestOptionsJSON, - AuthenticatorAssertionResponseJSON, AssertionCredential, + AssertionCredentialJSON, } from '@simplewebauthn/typescript-types'; import toUint8Array from '../helpers/toUint8Array'; @@ -16,7 +16,7 @@ import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDes */ export default async function startAssertion( requestOptionsJSON: PublicKeyCredentialRequestOptionsJSON, -): Promise { +): Promise { if (!supportsWebauthn()) { throw new Error('WebAuthn is not supported in this browser'); } @@ -31,25 +31,29 @@ export default async function startAssertion( }; // Wait for the user to complete assertion - const credential = await navigator.credentials.get({ publicKey }); + const credential = await navigator.credentials.get({ publicKey }) as AssertionCredential; if (!credential) { throw new Error('Assertion was not completed'); } - const { response } = credential as AssertionCredential; + const { rawId, response } = credential; - let base64UserHandle = undefined; + let userHandle = undefined; if (response.userHandle) { - base64UserHandle = toBase64String(response.userHandle); + userHandle = toBase64String(response.userHandle); } // Convert values to base64 to make it easier to send back to the server return { - base64CredentialID: credential.id, - base64AuthenticatorData: toBase64String(response.authenticatorData), - base64ClientDataJSON: toBase64String(response.clientDataJSON), - base64Signature: toBase64String(response.signature), - base64UserHandle, + ...credential, + rawId: toBase64String(rawId), + response: { + ...response, + authenticatorData: toBase64String(response.authenticatorData), + clientDataJSON: toBase64String(response.clientDataJSON), + signature: toBase64String(response.signature), + userHandle, + }, }; } -- cgit v1.2.3 From 5ae824965ebee5590486c36fe0365c9ad802bb23 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Tue, 2 Jun 2020 12:08:47 -0700 Subject: Update start methods to use new helper --- packages/browser/src/methods/startAssertion.ts | 12 ++++++------ packages/browser/src/methods/startAttestation.ts | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'packages/browser/src/methods/startAssertion.ts') diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index 1590634..81cad60 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -5,7 +5,7 @@ import { } from '@simplewebauthn/typescript-types'; import toUint8Array from '../helpers/toUint8Array'; -import toBase64String from '../helpers/toBase64String'; +import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import supportsWebauthn from '../helpers/supportsWebauthn'; import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor'; @@ -41,18 +41,18 @@ export default async function startAssertion( let userHandle = undefined; if (response.userHandle) { - userHandle = toBase64String(response.userHandle); + userHandle = bufferToBase64URLString(response.userHandle); } // Convert values to base64 to make it easier to send back to the server return { ...credential, - rawId: toBase64String(rawId), + rawId: bufferToBase64URLString(rawId), response: { ...response, - authenticatorData: toBase64String(response.authenticatorData), - clientDataJSON: toBase64String(response.clientDataJSON), - signature: toBase64String(response.signature), + authenticatorData: bufferToBase64URLString(response.authenticatorData), + clientDataJSON: bufferToBase64URLString(response.clientDataJSON), + signature: bufferToBase64URLString(response.signature), userHandle, }, }; diff --git a/packages/browser/src/methods/startAttestation.ts b/packages/browser/src/methods/startAttestation.ts index 51ea6ec..1612961 100644 --- a/packages/browser/src/methods/startAttestation.ts +++ b/packages/browser/src/methods/startAttestation.ts @@ -5,7 +5,7 @@ import { } from '@simplewebauthn/typescript-types'; import toUint8Array from '../helpers/toUint8Array'; -import toBase64String from '../helpers/toBase64String'; +import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import supportsWebauthn from '../helpers/supportsWebauthn'; import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor'; @@ -46,11 +46,11 @@ export default async function startAttestation( // Convert values to base64 to make it easier to send back to the server return { ...credential, - rawId: toBase64String(rawId), + rawId: bufferToBase64URLString(rawId), response: { ...response, - attestationObject: toBase64String(response.attestationObject), - clientDataJSON: toBase64String(response.clientDataJSON), - } + attestationObject: bufferToBase64URLString(response.attestationObject), + clientDataJSON: bufferToBase64URLString(response.clientDataJSON), + }, }; } -- cgit v1.2.3 From 047413af015302639a1bd23934d9eb23fcbec169 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Tue, 2 Jun 2020 13:17:18 -0700 Subject: Explicitly define id and type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out that properties on PublicKeyCredentials are non-enumerable getters, so the spread operator won’t pick them up. This means we need to manually re-construct attestation and assertion credentials when we convert them to JSON. --- packages/browser/src/methods/startAssertion.ts | 6 +++--- packages/browser/src/methods/startAttestation.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'packages/browser/src/methods/startAssertion.ts') diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index 81cad60..b65325b 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -37,7 +37,7 @@ export default async function startAssertion( throw new Error('Assertion was not completed'); } - const { rawId, response } = credential; + const { id, rawId, response, type } = credential; let userHandle = undefined; if (response.userHandle) { @@ -46,14 +46,14 @@ export default async function startAssertion( // Convert values to base64 to make it easier to send back to the server return { - ...credential, + id, rawId: bufferToBase64URLString(rawId), response: { - ...response, authenticatorData: bufferToBase64URLString(response.authenticatorData), clientDataJSON: bufferToBase64URLString(response.clientDataJSON), signature: bufferToBase64URLString(response.signature), userHandle, }, + type, }; } diff --git a/packages/browser/src/methods/startAttestation.ts b/packages/browser/src/methods/startAttestation.ts index 1612961..d5e540f 100644 --- a/packages/browser/src/methods/startAttestation.ts +++ b/packages/browser/src/methods/startAttestation.ts @@ -41,16 +41,16 @@ export default async function startAttestation( throw new Error('Attestation was not completed'); } - const { rawId, response } = credential; + const { id, rawId, response, type } = credential; // Convert values to base64 to make it easier to send back to the server return { - ...credential, + id, rawId: bufferToBase64URLString(rawId), response: { - ...response, attestationObject: bufferToBase64URLString(response.attestationObject), clientDataJSON: bufferToBase64URLString(response.clientDataJSON), }, + type, }; } -- cgit v1.2.3