diff options
Diffstat (limited to 'packages/browser')
-rw-r--r-- | packages/browser/jest-environment.js | 22 | ||||
-rw-r--r-- | packages/browser/jest.config.js | 2 | ||||
-rw-r--r-- | packages/browser/src/helpers/toUint8Array.ts | 4 | ||||
-rw-r--r-- | packages/browser/src/methods/startAssertion.test.ts | 34 |
4 files changed, 60 insertions, 2 deletions
diff --git a/packages/browser/jest-environment.js b/packages/browser/jest-environment.js new file mode 100644 index 0000000..c3a9135 --- /dev/null +++ b/packages/browser/jest-environment.js @@ -0,0 +1,22 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const Environment = require('jest-environment-jsdom'); + +/** + * Set up a custom JSDOM-based test environment for Jest so we can add things JSDOM doesn't support + */ +class CustomTestEnvironment extends Environment { + async setup() { + await super.setup(); + /** + * JSDOM doesn't implement TextEncoder so we need to fake it with Node's + * + * Solved thanks to https://stackoverflow.com/a/57713960/2133271 + */ + if (typeof this.global.TextEncoder === 'undefined') { + const { TextEncoder } = require('util'); + this.global.TextEncoder = TextEncoder; + } + } +} + +module.exports = CustomTestEnvironment; diff --git a/packages/browser/jest.config.js b/packages/browser/jest.config.js index c934d3f..b9a5083 100644 --- a/packages/browser/jest.config.js +++ b/packages/browser/jest.config.js @@ -2,6 +2,6 @@ const rootConfig = require('../../jest.config'); module.exports = { ...rootConfig, - testEnvironment: 'jsdom', + testEnvironment: '<rootDir>/jest-environment.js', setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'], }; diff --git a/packages/browser/src/helpers/toUint8Array.ts b/packages/browser/src/helpers/toUint8Array.ts index ed4aa5d..1855dd7 100644 --- a/packages/browser/src/helpers/toUint8Array.ts +++ b/packages/browser/src/helpers/toUint8Array.ts @@ -1,7 +1,9 @@ +const utf8Encoder = new TextEncoder(); + /** * A helper method to convert an arbitrary string sent from the server to a Uint8Array the * authenticator will expect. */ export default function toUint8Array(value: string): Uint8Array { - return Uint8Array.from(value, c => c.charCodeAt(0)); + return utf8Encoder.encode(value); } diff --git a/packages/browser/src/methods/startAssertion.test.ts b/packages/browser/src/methods/startAssertion.test.ts index 669e8eb..e919d18 100644 --- a/packages/browser/src/methods/startAssertion.test.ts +++ b/packages/browser/src/methods/startAssertion.test.ts @@ -17,6 +17,7 @@ const mockClientDataJSON = 'mockClientDataJSON'; const mockSignature = 'mockSignature'; const mockUserHandle = 'mockUserHandle'; +// With ASCII challenge const goodOpts1: PublicKeyCredentialRequestOptionsJSON = { challenge: 'fizz', allowCredentials: [ @@ -29,6 +30,13 @@ const goodOpts1: PublicKeyCredentialRequestOptionsJSON = { timeout: 1, }; +// With UTF-8 challenge +const goodOpts2UTF8: PublicKeyCredentialRequestOptionsJSON = { + challenge: 'やれやれだぜ', + allowCredentials: [], + timeout: 1, +}; + beforeEach(() => { mockNavigatorGet.mockReset(); mockSupportsWebauthn.mockReset(); @@ -120,3 +128,29 @@ test('should throw error if assertion is cancelled for some reason', async done done(); }); + +test('should handle UTF-8 challenges', 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: {}, + getClientExtensionResults: () => ({}), + }); + }); + }, + ); + + await startAssertion(goodOpts2UTF8); + + 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}', + ); + + done(); +}); |