From ce486db665fd805fff59772dfc852ac2d4cb3a43 Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 25 May 2020 11:11:48 -0700 Subject: Run Prettier over everything --- .../browser/src/helpers/supportsWebauthn.test.ts | 10 +-- packages/browser/src/helpers/supportsWebauthn.ts | 3 +- packages/browser/src/helpers/toBase64String.ts | 4 +- packages/browser/src/index.ts | 6 +- .../browser/src/methods/startAssertion.test.ts | 91 ++++++++++++---------- packages/browser/src/methods/startAssertion.ts | 10 +-- .../browser/src/methods/startAttestation.test.ts | 85 +++++++++++--------- packages/browser/src/methods/startAttestation.ts | 4 +- 8 files changed, 116 insertions(+), 97 deletions(-) (limited to 'packages/browser/src') 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 => { - return new Promise((resolve) => { - resolve({ response: {} }); - }); - }); + mockNavigatorGet.mockImplementation( + (): Promise => { + 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 => { - 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 => { + 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 => { - return new Promise((resolve) => { - resolve(null); - }); - }); + mockNavigatorGet.mockImplementation( + (): Promise => { + 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 { 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 => { - return new Promise((resolve) => { - resolve({ response: {} }); - }); - }); + mockNavigatorCreate.mockImplementation( + (): Promise => { + 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 => { - 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 => { + 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 => { - return new Promise((resolve) => { - resolve(null); - }); - }); + mockNavigatorCreate.mockImplementation( + (): Promise => { + 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 { 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 { -- cgit v1.2.3 From 8e55856d806dc9acb05a313ee42363950f46d7cd Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Mon, 25 May 2020 11:41:23 -0700 Subject: Run ESLint over everything --- example/index.js | 1 + packages/browser/src/methods/startAssertion.ts | 2 +- packages/server/src/assertion/generateAssertionOptions.ts | 2 +- packages/server/src/attestation/generateAttestationOptions.ts | 2 +- packages/server/src/attestation/verifications/verifyPacked.ts | 4 ++-- packages/server/src/helpers/asciiToBinary.ts | 2 +- packages/server/src/helpers/convertASN1toPEM.ts | 2 +- packages/server/src/helpers/convertCOSEtoPKCS.ts | 2 +- packages/server/src/helpers/toHash.ts | 2 +- 9 files changed, 10 insertions(+), 9 deletions(-) (limited to 'packages/browser/src') diff --git a/example/index.js b/example/index.js index 6cc1408..db33179 100644 --- a/example/index.js +++ b/example/index.js @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ /** * An example Express server showing off a simple integration of @webauthntine/server. * diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index 36c7194..f504f19 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -28,7 +28,7 @@ export default async function startAssertion( allowCredentials: requestOptionsJSON.publicKey.allowCredentials.map(cred => { // Make sure the credential ID length is a multiple of 4 const padLength = 4 - (cred.id.length % 4); - let id = cred.id.padEnd(cred.id.length + padLength, '='); + const id = cred.id.padEnd(cred.id.length + padLength, '='); return { ...cred, diff --git a/packages/server/src/assertion/generateAssertionOptions.ts b/packages/server/src/assertion/generateAssertionOptions.ts index 5dc4c84..ac18233 100644 --- a/packages/server/src/assertion/generateAssertionOptions.ts +++ b/packages/server/src/assertion/generateAssertionOptions.ts @@ -11,7 +11,7 @@ import { PublicKeyCredentialRequestOptionsJSON } from '@webauthntine/typescript- export default function generateAssertionOptions( challenge: string, base64CredentialIDs: string[], - timeout: number = 60000, + timeout = 60000, ): PublicKeyCredentialRequestOptionsJSON { return { publicKey: { diff --git a/packages/server/src/attestation/generateAttestationOptions.ts b/packages/server/src/attestation/generateAttestationOptions.ts index 6d1f7d9..e644d06 100644 --- a/packages/server/src/attestation/generateAttestationOptions.ts +++ b/packages/server/src/attestation/generateAttestationOptions.ts @@ -17,7 +17,7 @@ export default function generateAttestationOptions( challenge: string, userID: string, username: string, - timeout: number = 60000, + timeout = 60000, attestationType: 'direct' | 'indirect' = 'direct', ): PublicKeyCredentialCreationOptionsJSON { return { diff --git a/packages/server/src/attestation/verifications/verifyPacked.ts b/packages/server/src/attestation/verifications/verifyPacked.ts index a27962d..1fce442 100644 --- a/packages/server/src/attestation/verifications/verifyPacked.ts +++ b/packages/server/src/attestation/verifications/verifyPacked.ts @@ -6,7 +6,7 @@ import { AttestationObject, VerifiedAttestation, COSEKEYS, - COSEPublicKey, + COSEPublicKey as COSEPublicKeyType, } from '@webauthntine/typescript-types'; import convertCOSEtoPKCS from '@helpers/convertCOSEtoPKCS'; @@ -89,7 +89,7 @@ export default function verifyAttestationPacked( toReturn.verified = verifySignature(sig, signatureBase, leafCert); } else { - const cosePublicKey: COSEPublicKey = cbor.decodeAllSync(COSEPublicKey)[0]; + const cosePublicKey: COSEPublicKeyType = cbor.decodeAllSync(COSEPublicKey)[0]; const kty = cosePublicKey.get(COSEKEYS.kty); const alg = cosePublicKey.get(COSEKEYS.alg); diff --git a/packages/server/src/helpers/asciiToBinary.ts b/packages/server/src/helpers/asciiToBinary.ts index b006edd..beb6f1d 100644 --- a/packages/server/src/helpers/asciiToBinary.ts +++ b/packages/server/src/helpers/asciiToBinary.ts @@ -3,6 +3,6 @@ * * @param input Base64-encoded string */ -export default function asciiToBinary(input: string) { +export default function asciiToBinary(input: string): string { return Buffer.from(input, 'base64').toString('binary'); } diff --git a/packages/server/src/helpers/convertASN1toPEM.ts b/packages/server/src/helpers/convertASN1toPEM.ts index c282e15..4cdfb2f 100644 --- a/packages/server/src/helpers/convertASN1toPEM.ts +++ b/packages/server/src/helpers/convertASN1toPEM.ts @@ -4,7 +4,7 @@ * @param buffer - Cert or PubKey buffer * @return PEM */ -export default function convertASN1toPEM(pkBuffer: Buffer) { +export default function convertASN1toPEM(pkBuffer: Buffer): string { let buffer = pkBuffer; let type; diff --git a/packages/server/src/helpers/convertCOSEtoPKCS.ts b/packages/server/src/helpers/convertCOSEtoPKCS.ts index f3b4a7e..d07b8a4 100644 --- a/packages/server/src/helpers/convertCOSEtoPKCS.ts +++ b/packages/server/src/helpers/convertCOSEtoPKCS.ts @@ -7,7 +7,7 @@ import { COSEKEYS, COSEPublicKey } from '@webauthntine/typescript-types'; * @param cosePublicKey COSE-encoded public key * @return RAW PKCS encoded public key */ -export default function convertCOSEtoPKCS(cosePublicKey: Buffer) { +export default function convertCOSEtoPKCS(cosePublicKey: Buffer): Buffer { /* +------+-------+-------+---------+----------------------------------+ | name | key | label | type | description | diff --git a/packages/server/src/helpers/toHash.ts b/packages/server/src/helpers/toHash.ts index 6e8db1d..65e4072 100644 --- a/packages/server/src/helpers/toHash.ts +++ b/packages/server/src/helpers/toHash.ts @@ -5,6 +5,6 @@ import crypto from 'crypto'; * @param data Data to hash * @return The hash */ -export default function toHash(data: Buffer, algo: string = 'SHA256'): Buffer { +export default function toHash(data: Buffer, algo = 'SHA256'): Buffer { return crypto.createHash(algo).update(data).digest(); } -- cgit v1.2.3