summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/browser/src')
-rw-r--r--packages/browser/src/helpers/supportsWebauthn.test.ts10
-rw-r--r--packages/browser/src/helpers/supportsWebauthn.ts3
-rw-r--r--packages/browser/src/helpers/toBase64String.ts4
-rw-r--r--packages/browser/src/index.ts6
-rw-r--r--packages/browser/src/methods/startAssertion.test.ts91
-rw-r--r--packages/browser/src/methods/startAssertion.ts10
-rw-r--r--packages/browser/src/methods/startAttestation.test.ts85
-rw-r--r--packages/browser/src/methods/startAttestation.ts4
8 files changed, 116 insertions, 97 deletions
diff --git a/packages/browser/src/helpers/supportsWebauthn.test.ts b/packages/browser/src/helpers/supportsWebauthn.test.ts
index f6b8a8e..6eb42c9 100644
--- a/packages/browser/src/helpers/supportsWebauthn.test.ts
+++ b/packages/browser/src/helpers/supportsWebauthn.test.ts
@@ -1,4 +1,4 @@
-import supportsWebauthn from './supportsWebauthn'
+import supportsWebauthn from './supportsWebauthn';
beforeEach(() => {
// @ts-ignore 2741
@@ -17,12 +17,12 @@ test('should return false when browser does not support WebAuthn', () => {
test('should return false when window is undefined', () => {
// Make window undefined as it is in node environments.
// @ts-expect-error
- const windowSpy = jest.spyOn(global, "window", "get");
+ const windowSpy = jest.spyOn(global, 'window', 'get');
windowSpy.mockImplementation(() => undefined);
- expect(window).toBe(undefined)
+ expect(window).toBe(undefined);
expect(supportsWebauthn()).toBe(false);
// Restore original window value.
- windowSpy.mockRestore()
-})
+ windowSpy.mockRestore();
+});
diff --git a/packages/browser/src/helpers/supportsWebauthn.ts b/packages/browser/src/helpers/supportsWebauthn.ts
index 605bb67..b572080 100644
--- a/packages/browser/src/helpers/supportsWebauthn.ts
+++ b/packages/browser/src/helpers/supportsWebauthn.ts
@@ -3,7 +3,6 @@
*/
export default function supportsWebauthn(): boolean {
return (
- window?.PublicKeyCredential !== undefined
- && typeof window.PublicKeyCredential === 'function'
+ window?.PublicKeyCredential !== undefined && typeof window.PublicKeyCredential === 'function'
);
}
diff --git a/packages/browser/src/helpers/toBase64String.ts b/packages/browser/src/helpers/toBase64String.ts
index 9c949be..3178695 100644
--- a/packages/browser/src/helpers/toBase64String.ts
+++ b/packages/browser/src/helpers/toBase64String.ts
@@ -2,7 +2,5 @@ import base64js from 'base64-js';
export default function toBase64String(buffer: ArrayBuffer): string {
// TODO: Make sure converting buffer to Uint8Array() is correct
- return base64js.fromByteArray(new Uint8Array(buffer))
- .replace(/\+/g, "-")
- .replace(/\//g, "_");
+ return base64js.fromByteArray(new Uint8Array(buffer)).replace(/\+/g, '-').replace(/\//g, '_');
}
diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts
index 38ce91b..18f5944 100644
--- a/packages/browser/src/index.ts
+++ b/packages/browser/src/index.ts
@@ -2,8 +2,4 @@ import startAttestation from './methods/startAttestation';
import startAssertion from './methods/startAssertion';
import supportsWebauthn from './helpers/supportsWebauthn';
-export {
- startAttestation,
- startAssertion,
- supportsWebauthn,
-};
+export { startAttestation, startAssertion, supportsWebauthn };
diff --git a/packages/browser/src/methods/startAssertion.test.ts b/packages/browser/src/methods/startAssertion.test.ts
index 259400c..2287b49 100644
--- a/packages/browser/src/methods/startAssertion.test.ts
+++ b/packages/browser/src/methods/startAssertion.test.ts
@@ -1,6 +1,9 @@
import base64js from 'base64-js';
-import { AssertionCredential, PublicKeyCredentialRequestOptionsJSON } from '@webauthntine/typescript-types';
+import {
+ AssertionCredential,
+ PublicKeyCredentialRequestOptionsJSON,
+} from '@webauthntine/typescript-types';
import toUint8Array from '../helpers/toUint8Array';
import supportsWebauthn from '../helpers/supportsWebauthn';
@@ -10,8 +13,8 @@ import startAssertion from './startAssertion';
jest.mock('../helpers/supportsWebauthn');
-const mockNavigatorGet = (window.navigator.credentials.get as jest.Mock);
-const mockSupportsWebauthn = (supportsWebauthn as jest.Mock);
+const mockNavigatorGet = window.navigator.credentials.get as jest.Mock;
+const mockSupportsWebauthn = supportsWebauthn as jest.Mock;
const mockAuthenticatorData = toBase64String(toUint8Array('mockAuthenticatorData'));
const mockClientDataJSON = toBase64String(toUint8Array('mockClientDataJSON'));
@@ -21,11 +24,13 @@ const mockUserHandle = toBase64String(toUint8Array('mockUserHandle'));
const goodOpts1: PublicKeyCredentialRequestOptionsJSON = {
publicKey: {
challenge: 'fizz',
- allowCredentials: [{
- id: 'abcdefgfdnsdfunguisdfgs',
- type: 'public-key',
- transports: ['nfc'],
- }],
+ allowCredentials: [
+ {
+ id: 'abcdefgfdnsdfunguisdfgs',
+ type: 'public-key',
+ transports: ['nfc'],
+ },
+ ],
timeout: 1,
},
};
@@ -35,15 +40,17 @@ beforeEach(() => {
mockSupportsWebauthn.mockReset();
});
-test('should convert options before passing to navigator.credentials.get(...)', async (done) => {
+test('should convert options before passing to navigator.credentials.get(...)', async done => {
mockSupportsWebauthn.mockReturnValue(true);
// Stub out a response so the method won't throw
- mockNavigatorGet.mockImplementation((): Promise<any> => {
- return new Promise((resolve) => {
- resolve({ response: {} });
- });
- });
+ mockNavigatorGet.mockImplementation(
+ (): Promise<any> => {
+ return new Promise(resolve => {
+ resolve({ response: {} });
+ });
+ },
+ );
await startAssertion(goodOpts1);
@@ -57,27 +64,29 @@ test('should convert options before passing to navigator.credentials.get(...)',
done();
});
-test('should return base64-encoded response values', async (done) => {
+test('should return base64-encoded response values', async done => {
mockSupportsWebauthn.mockReturnValue(true);
const credentialID = 'foobar';
- mockNavigatorGet.mockImplementation((): Promise<AssertionCredential> => {
- return new Promise((resolve) => {
- resolve({
- id: 'foobar',
- rawId: toUint8Array('foobar'),
- response: {
- authenticatorData: base64js.toByteArray(mockAuthenticatorData),
- clientDataJSON: base64js.toByteArray(mockClientDataJSON),
- signature: base64js.toByteArray(mockSignature),
- userHandle: base64js.toByteArray(mockUserHandle),
- },
- getClientExtensionResults: () => ({}),
- type: 'webauthn.get',
+ mockNavigatorGet.mockImplementation(
+ (): Promise<AssertionCredential> => {
+ return new Promise(resolve => {
+ resolve({
+ id: 'foobar',
+ rawId: toUint8Array('foobar'),
+ response: {
+ authenticatorData: base64js.toByteArray(mockAuthenticatorData),
+ clientDataJSON: base64js.toByteArray(mockClientDataJSON),
+ signature: base64js.toByteArray(mockSignature),
+ userHandle: base64js.toByteArray(mockUserHandle),
+ },
+ getClientExtensionResults: () => ({}),
+ type: 'webauthn.get',
+ });
});
- });
- });
+ },
+ );
const response = await startAssertion(goodOpts1);
@@ -90,24 +99,28 @@ test('should return base64-encoded response values', async (done) => {
});
done();
-})
+});
-test('should throw error if WebAuthn isn\'t supported', async (done) => {
+test("should throw error if WebAuthn isn't supported", async done => {
mockSupportsWebauthn.mockReturnValue(false);
- await expect(startAssertion(goodOpts1)).rejects.toThrow('WebAuthn is not supported in this browser');
+ await expect(startAssertion(goodOpts1)).rejects.toThrow(
+ 'WebAuthn is not supported in this browser',
+ );
done();
});
-test('should throw error if assertion is cancelled for some reason', async (done) => {
+test('should throw error if assertion is cancelled for some reason', async done => {
mockSupportsWebauthn.mockReturnValue(true);
- mockNavigatorGet.mockImplementation((): Promise<null> => {
- return new Promise((resolve) => {
- resolve(null);
- });
- });
+ mockNavigatorGet.mockImplementation(
+ (): Promise<null> => {
+ return new Promise(resolve => {
+ resolve(null);
+ });
+ },
+ );
await expect(startAssertion(goodOpts1)).rejects.toThrow('Assertion was not completed');
diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts
index 0733194..36c7194 100644
--- a/packages/browser/src/methods/startAssertion.ts
+++ b/packages/browser/src/methods/startAssertion.ts
@@ -15,7 +15,7 @@ import supportsWebauthn from '../helpers/supportsWebauthn';
* @param requestOptionsJSON Output from @webauthntine/server's generateAssertionOptions(...)
*/
export default async function startAssertion(
- requestOptionsJSON: PublicKeyCredentialRequestOptionsJSON
+ requestOptionsJSON: PublicKeyCredentialRequestOptionsJSON,
): Promise<AuthenticatorAssertionResponseJSON> {
if (!supportsWebauthn()) {
throw new Error('WebAuthn is not supported in this browser');
@@ -25,16 +25,16 @@ export default async function startAssertion(
const publicKey: PublicKeyCredentialRequestOptions = {
...requestOptionsJSON.publicKey,
challenge: toUint8Array(requestOptionsJSON.publicKey.challenge),
- allowCredentials: requestOptionsJSON.publicKey.allowCredentials.map((cred) => {
+ allowCredentials: requestOptionsJSON.publicKey.allowCredentials.map(cred => {
// Make sure the credential ID length is a multiple of 4
- const padLength = 4 - cred.id.length % 4;
+ const padLength = 4 - (cred.id.length % 4);
let id = cred.id.padEnd(cred.id.length + padLength, '=');
return {
...cred,
id: base64js.toByteArray(id),
};
- })
+ }),
};
// Wait for the user to complete assertion
@@ -44,7 +44,7 @@ export default async function startAssertion(
throw new Error('Assertion was not completed');
}
- const { response } = (credential as AssertionCredential);
+ const { response } = credential as AssertionCredential;
let base64UserHandle = undefined;
if (response.userHandle) {
diff --git a/packages/browser/src/methods/startAttestation.test.ts b/packages/browser/src/methods/startAttestation.test.ts
index 0efec48..539ffe5 100644
--- a/packages/browser/src/methods/startAttestation.test.ts
+++ b/packages/browser/src/methods/startAttestation.test.ts
@@ -1,6 +1,9 @@
import base64js from 'base64-js';
-import { AttestationCredential, PublicKeyCredentialCreationOptionsJSON } from '@webauthntine/typescript-types';
+import {
+ AttestationCredential,
+ PublicKeyCredentialCreationOptionsJSON,
+} from '@webauthntine/typescript-types';
import toUint8Array from '../helpers/toUint8Array';
import supportsWebauthn from '../helpers/supportsWebauthn';
@@ -9,8 +12,8 @@ import startAttestation from './startAttestation';
jest.mock('../helpers/supportsWebauthn');
-const mockNavigatorCreate = (window.navigator.credentials.create as jest.Mock);
-const mockSupportsWebauthn = (supportsWebauthn as jest.Mock);
+const mockNavigatorCreate = window.navigator.credentials.create as jest.Mock;
+const mockSupportsWebauthn = supportsWebauthn as jest.Mock;
const mockAttestationObject = 'mockAtte';
const mockClientDataJSON = 'mockClie';
@@ -19,10 +22,12 @@ const goodOpts1: PublicKeyCredentialCreationOptionsJSON = {
publicKey: {
challenge: 'fizz',
attestation: 'direct',
- pubKeyCredParams: [{
- alg: -7,
- type: "public-key",
- }],
+ pubKeyCredParams: [
+ {
+ alg: -7,
+ type: 'public-key',
+ },
+ ],
rp: {
id: '1234',
name: 'webauthntine',
@@ -41,15 +46,17 @@ beforeEach(() => {
mockSupportsWebauthn.mockReset();
});
-test('should convert options before passing to navigator.credentials.create(...)', async (done) => {
+test('should convert options before passing to navigator.credentials.create(...)', async done => {
mockSupportsWebauthn.mockReturnValue(true);
// Stub out a response so the method won't throw
- mockNavigatorCreate.mockImplementation((): Promise<any> => {
- return new Promise((resolve) => {
- resolve({ response: {} });
- });
- });
+ mockNavigatorCreate.mockImplementation(
+ (): Promise<any> => {
+ return new Promise(resolve => {
+ resolve({ response: {} });
+ });
+ },
+ );
await startAttestation(goodOpts1);
@@ -61,23 +68,25 @@ test('should convert options before passing to navigator.credentials.create(...)
done();
});
-test('should return base64-encoded response values', async (done) => {
+test('should return base64-encoded response values', async done => {
mockSupportsWebauthn.mockReturnValue(true);
- mockNavigatorCreate.mockImplementation((): Promise<AttestationCredential> => {
- return new Promise((resolve) => {
- resolve({
- id: 'foobar',
- rawId: toUint8Array('foobar'),
- response: {
- attestationObject: base64js.toByteArray(mockAttestationObject),
- clientDataJSON: base64js.toByteArray(mockClientDataJSON),
- },
- getClientExtensionResults: () => ({}),
- type: 'webauthn.create',
+ mockNavigatorCreate.mockImplementation(
+ (): Promise<AttestationCredential> => {
+ return new Promise(resolve => {
+ resolve({
+ id: 'foobar',
+ rawId: toUint8Array('foobar'),
+ response: {
+ attestationObject: base64js.toByteArray(mockAttestationObject),
+ clientDataJSON: base64js.toByteArray(mockClientDataJSON),
+ },
+ getClientExtensionResults: () => ({}),
+ type: 'webauthn.create',
+ });
});
- });
- });
+ },
+ );
const response = await startAttestation(goodOpts1);
@@ -87,24 +96,28 @@ test('should return base64-encoded response values', async (done) => {
});
done();
-})
+});
-test('should throw error if WebAuthn isn\'t supported', async (done) => {
+test("should throw error if WebAuthn isn't supported", async done => {
mockSupportsWebauthn.mockReturnValue(false);
- await expect(startAttestation(goodOpts1)).rejects.toThrow('WebAuthn is not supported in this browser');
+ await expect(startAttestation(goodOpts1)).rejects.toThrow(
+ 'WebAuthn is not supported in this browser',
+ );
done();
});
-test('should throw error if attestation is cancelled for some reason', async (done) => {
+test('should throw error if attestation is cancelled for some reason', async done => {
mockSupportsWebauthn.mockReturnValue(true);
- mockNavigatorCreate.mockImplementation((): Promise<null> => {
- return new Promise((resolve) => {
- resolve(null);
- });
- });
+ mockNavigatorCreate.mockImplementation(
+ (): Promise<null> => {
+ return new Promise(resolve => {
+ resolve(null);
+ });
+ },
+ );
await expect(startAttestation(goodOpts1)).rejects.toThrow('Attestation was not completed');
diff --git a/packages/browser/src/methods/startAttestation.ts b/packages/browser/src/methods/startAttestation.ts
index 1a4b13d..c095670 100644
--- a/packages/browser/src/methods/startAttestation.ts
+++ b/packages/browser/src/methods/startAttestation.ts
@@ -14,7 +14,7 @@ import supportsWebauthn from '../helpers/supportsWebauthn';
* @param creationOptionsJSON Output from @webauthntine/server's generateAttestationOptions(...)
*/
export default async function startAttestation(
- creationOptionsJSON: PublicKeyCredentialCreationOptionsJSON
+ creationOptionsJSON: PublicKeyCredentialCreationOptionsJSON,
): Promise<AuthenticatorAttestationResponseJSON> {
if (!supportsWebauthn()) {
throw new Error('WebAuthn is not supported in this browser');
@@ -37,7 +37,7 @@ export default async function startAttestation(
throw new Error('Attestation was not completed');
}
- const { response } = (credential as AttestationCredential);
+ const { response } = credential as AttestationCredential;
// Convert values to base64 to make it easier to send back to the server
return {