summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2020-06-01 16:16:13 -0700
committerMatthew Miller <matthew@millerti.me>2020-06-01 16:16:13 -0700
commitdb28323378648782d92f6552d478a4132d58dcdc (patch)
tree8e53582b16315393a95bb78f6ddeda680ea48c4b /packages/browser/src
parent2e31764344df57ec2a9176a74279a459f82729b4 (diff)
Refactor browser methods to return near-raw resp.
Diffstat (limited to 'packages/browser/src')
-rw-r--r--packages/browser/src/methods/startAssertion.test.ts14
-rw-r--r--packages/browser/src/methods/startAssertion.ts26
-rw-r--r--packages/browser/src/methods/startAttestation.test.ts7
-rw-r--r--packages/browser/src/methods/startAttestation.ts17
4 files changed, 34 insertions, 30 deletions
diff --git a/packages/browser/src/methods/startAssertion.test.ts b/packages/browser/src/methods/startAssertion.test.ts
index db401dc..d106d05 100644
--- a/packages/browser/src/methods/startAssertion.test.ts
+++ b/packages/browser/src/methods/startAssertion.test.ts
@@ -65,8 +65,6 @@ test('should convert options before passing to navigator.credentials.get(...)',
test('should return base64-encoded response values', async done => {
mockSupportsWebauthn.mockReturnValue(true);
- const credentialID = 'foobar';
-
mockNavigatorGet.mockImplementation(
(): Promise<AssertionCredential> => {
return new Promise(resolve => {
@@ -88,13 +86,11 @@ test('should return base64-encoded response values', async done => {
const response = await startAssertion(goodOpts1);
- expect(response).toEqual({
- base64CredentialID: credentialID,
- base64AuthenticatorData: mockAuthenticatorData,
- base64ClientDataJSON: mockClientDataJSON,
- base64Signature: mockSignature,
- base64UserHandle: mockUserHandle,
- });
+ expect(response.rawId).toEqual('Zm9vYmFy');
+ expect(response.response.authenticatorData).toEqual(mockAuthenticatorData);
+ expect(response.response.clientDataJSON).toEqual(mockClientDataJSON);
+ expect(response.response.signature).toEqual(mockSignature);
+ expect(response.response.userHandle).toEqual(mockUserHandle);
done();
});
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<AuthenticatorAssertionResponseJSON> {
+): Promise<AssertionCredentialJSON> {
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,
+ },
};
}
diff --git a/packages/browser/src/methods/startAttestation.test.ts b/packages/browser/src/methods/startAttestation.test.ts
index ae79235..a54e8b8 100644
--- a/packages/browser/src/methods/startAttestation.test.ts
+++ b/packages/browser/src/methods/startAttestation.test.ts
@@ -98,10 +98,9 @@ test('should return base64-encoded response values', async done => {
const response = await startAttestation(goodOpts1);
- expect(response).toEqual({
- base64AttestationObject: mockAttestationObject,
- base64ClientDataJSON: mockClientDataJSON,
- });
+ expect(response.rawId).toEqual('Zm9vYmFy');
+ expect(response.response.attestationObject).toEqual(mockAttestationObject);
+ expect(response.response.clientDataJSON).toEqual(mockClientDataJSON);
done();
});
diff --git a/packages/browser/src/methods/startAttestation.ts b/packages/browser/src/methods/startAttestation.ts
index ea05fa8..51ea6ec 100644
--- a/packages/browser/src/methods/startAttestation.ts
+++ b/packages/browser/src/methods/startAttestation.ts
@@ -1,7 +1,7 @@
import {
PublicKeyCredentialCreationOptionsJSON,
- AuthenticatorAttestationResponseJSON,
AttestationCredential,
+ AttestationCredentialJSON,
} from '@simplewebauthn/typescript-types';
import toUint8Array from '../helpers/toUint8Array';
@@ -16,7 +16,7 @@ import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDes
*/
export default async function startAttestation(
creationOptionsJSON: PublicKeyCredentialCreationOptionsJSON,
-): Promise<AuthenticatorAttestationResponseJSON> {
+): Promise<AttestationCredentialJSON> {
if (!supportsWebauthn()) {
throw new Error('WebAuthn is not supported in this browser');
}
@@ -35,17 +35,22 @@ export default async function startAttestation(
};
// Wait for the user to complete attestation
- const credential = await navigator.credentials.create({ publicKey });
+ const credential = await navigator.credentials.create({ publicKey }) as AttestationCredential;
if (!credential) {
throw new Error('Attestation was not completed');
}
- const { response } = credential as AttestationCredential;
+ const { rawId, response } = credential;
// Convert values to base64 to make it easier to send back to the server
return {
- base64AttestationObject: toBase64String(response.attestationObject),
- base64ClientDataJSON: toBase64String(response.clientDataJSON),
+ ...credential,
+ rawId: toBase64String(rawId),
+ response: {
+ ...response,
+ attestationObject: toBase64String(response.attestationObject),
+ clientDataJSON: toBase64String(response.clientDataJSON),
+ }
};
}