From 9709c574ed39b7567471b662c043e46ddb322011 Mon Sep 17 00:00:00 2001 From: David Doorn Date: Sun, 17 May 2020 18:41:51 +0200 Subject: [PATCH 1/4] Moved auth token to headers --- src/CockpitSDK.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CockpitSDK.js b/src/CockpitSDK.js index b031302..baa7c11 100644 --- a/src/CockpitSDK.js +++ b/src/CockpitSDK.js @@ -67,7 +67,6 @@ class CockpitSDK { this.webSocket = webSocket; this.queryParams = { lang: this.lang, - token: this.accessToken, }; if (webSocket) { @@ -81,11 +80,12 @@ class CockpitSDK { ...this.fetchInitOptions, }; - const hostWithToken = `${this.host}${apiPath}?${qs.stringify( + requestInit.headers.Authorization = `Bearer ${this.accessToken}`; + const url = `${this.host}${apiPath}?${qs.stringify( this.queryParams, )}&${qs.stringify(queryParams)}`; - return fetch(hostWithToken, requestInit).then(x => x.json()); + return fetch(url, requestInit).then(x => x.json()); } // @param {string} apiPath From 2ed600611cf2ab5d62a9e66d162cf27b3df4d666 Mon Sep 17 00:00:00 2001 From: David Doorn Date: Sun, 17 May 2020 18:44:26 +0200 Subject: [PATCH 2/4] fixed tests --- src/__tests__/CockpitSDK.test.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/__tests__/CockpitSDK.test.js b/src/__tests__/CockpitSDK.test.js index 28916bc..2bde584 100644 --- a/src/__tests__/CockpitSDK.test.js +++ b/src/__tests__/CockpitSDK.test.js @@ -12,9 +12,9 @@ test('Expect Cockpit.image to return array of numbers', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&w=10 10w, ' + - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&w=20 20w, ' + - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&w=30 30w', + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&w=10 10w, ' + + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&w=20 20w, ' + + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&w=30 30w', ); }); @@ -32,7 +32,7 @@ test('Expect Cockpit.image to return height', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&token=bar&w=200', + 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&w=200', ); }); @@ -51,7 +51,7 @@ test('Expect Cockpit.image with pixel ratio', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&token=bar&w=400', + 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&w=400', ); }); @@ -75,8 +75,8 @@ test('Expect Cockpit.image with array to return height', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&token=bar&w=200 200w, ' + - 'foo/api/cockpit/image?d=1&h=10&lang=biz&o=1&src=bux&token=bar&w=20 20w', + 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&w=200 200w, ' + + 'foo/api/cockpit/image?d=1&h=10&lang=biz&o=1&src=bux&w=20 20w', ); }); @@ -102,8 +102,8 @@ test('Expect Cockpit.image with array to return pixel ratio', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&token=bar&w=400 200w, ' + - 'foo/api/cockpit/image?d=1&h=20&lang=biz&o=1&src=bux&token=bar&w=40 20w', + 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&w=400 200w, ' + + 'foo/api/cockpit/image?d=1&h=20&lang=biz&o=1&src=bux&w=40 20w', ); }); @@ -129,8 +129,8 @@ test('Expect Cockpit.image with array to return different srcSet', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&token=bar&w=200 1x, ' + - 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&token=bar&w=400 2x', + 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&w=200 1x, ' + + 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&w=400 2x', ); }); @@ -147,7 +147,7 @@ test('Expect Cockpit.image to return one filter', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&f[darken]=50', + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&f[darken]=50', ); }); @@ -165,6 +165,6 @@ test('Expect Cockpit.image to return multiple filters', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&f[darken]=50&f[pixelate]=40&f[desaturate]=true&f[flip]=x', + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&f[darken]=50&f[pixelate]=40&f[desaturate]=true&f[flip]=x', ); }); From 6abed9540d76e0800ed19832fb93343759de92f8 Mon Sep 17 00:00:00 2001 From: David Doorn Date: Sun, 17 May 2020 22:34:21 +0200 Subject: [PATCH 3/4] The constructor now accepts an "authHeader" property to use the authorization header instead of a queryparam --- src/CockpitSDK.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/CockpitSDK.js b/src/CockpitSDK.js index baa7c11..733f1d8 100644 --- a/src/CockpitSDK.js +++ b/src/CockpitSDK.js @@ -44,6 +44,7 @@ class CockpitSDK { host, lang, webSocket, + authHeader, apiEndpoints = {}, ...rest }) { @@ -55,7 +56,7 @@ class CockpitSDK { invalidConfig, '\n', 'Valid keys are:', - 'accessToken, defaultOptions, fetchInitOptions, host, lang, webSocket', + 'accessToken, defaultOptions, fetchInitOptions, host, lang, webSocket, authHeader', ); this.host = host; @@ -65,10 +66,15 @@ class CockpitSDK { this.endpoints = { ...this.defaultEndpoints, ...apiEndpoints }; this.accessToken = accessToken; this.webSocket = webSocket; + this.authHeader = authHeader; this.queryParams = { lang: this.lang, }; + if (!this.authHeader) { + this.queryParams.token = this.accessToken; + } + if (webSocket) { this.setWebsocket(webSocket); } @@ -80,7 +86,9 @@ class CockpitSDK { ...this.fetchInitOptions, }; - requestInit.headers.Authorization = `Bearer ${this.accessToken}`; + if (this.authHeader) { + requestInit.headers.Authorization = `Bearer ${this.accessToken}`; + } const url = `${this.host}${apiPath}?${qs.stringify( this.queryParams, )}&${qs.stringify(queryParams)}`; From 6ae4c3385bc153b968d9f894f80464d46e3683e5 Mon Sep 17 00:00:00 2001 From: David Doorn Date: Sun, 17 May 2020 22:40:25 +0200 Subject: [PATCH 4/4] added test and docs --- README.md | 1 + src/__tests__/CockpitSDK.test.js | 39 ++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c83bb1c..2f23d8b 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Connecting your project to Cockpit is done by instantiating CockpitSDK. This obj | **webSocket** | Websocket address (if used) | | **fetchInitOptions** | [Init options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) to apply on every Fetch request | | **defaultOptions** | Options to be applied on every Cockpit fetch | +| **authHeader** | Use the *Authorization* header instead of a query parameter|
defaultOptions:

diff --git a/src/__tests__/CockpitSDK.test.js b/src/__tests__/CockpitSDK.test.js index 2bde584..695d138 100644 --- a/src/__tests__/CockpitSDK.test.js +++ b/src/__tests__/CockpitSDK.test.js @@ -10,6 +10,25 @@ test('Expect Cockpit.image to return array of numbers', () => { const result = cockpit.image('bux', imageOptions); + expect(result).toBe( + '' + + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&w=10 10w, ' + + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&w=20 20w, ' + + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&w=30 30w', + ); +}); + +test('Expect Cockpit.image to return array of numbers and have the token in the header', () => { + const cockpit = new CockpitSDK({ + host: 'foo', + accessToken: 'bar', + lang: 'biz', + authHeader: true, + }); + const imageOptions = [10, 20, 30]; + + const result = cockpit.image('bux', imageOptions); + expect(result).toBe( '' + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&w=10 10w, ' + @@ -32,7 +51,7 @@ test('Expect Cockpit.image to return height', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&w=200', + 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&token=bar&w=200', ); }); @@ -51,7 +70,7 @@ test('Expect Cockpit.image with pixel ratio', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&w=400', + 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&token=bar&w=400', ); }); @@ -75,8 +94,8 @@ test('Expect Cockpit.image with array to return height', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&w=200 200w, ' + - 'foo/api/cockpit/image?d=1&h=10&lang=biz&o=1&src=bux&w=20 20w', + 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&token=bar&w=200 200w, ' + + 'foo/api/cockpit/image?d=1&h=10&lang=biz&o=1&src=bux&token=bar&w=20 20w', ); }); @@ -102,8 +121,8 @@ test('Expect Cockpit.image with array to return pixel ratio', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&w=400 200w, ' + - 'foo/api/cockpit/image?d=1&h=20&lang=biz&o=1&src=bux&w=40 20w', + 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&token=bar&w=400 200w, ' + + 'foo/api/cockpit/image?d=1&h=20&lang=biz&o=1&src=bux&token=bar&w=40 20w', ); }); @@ -129,8 +148,8 @@ test('Expect Cockpit.image with array to return different srcSet', () => { expect(result).toBe( '' + - 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&w=200 1x, ' + - 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&w=400 2x', + 'foo/api/cockpit/image?d=1&h=100&lang=biz&o=1&src=bux&token=bar&w=200 1x, ' + + 'foo/api/cockpit/image?d=1&h=200&lang=biz&o=1&src=bux&token=bar&w=400 2x', ); }); @@ -147,7 +166,7 @@ test('Expect Cockpit.image to return one filter', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&f[darken]=50', + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&f[darken]=50', ); }); @@ -165,6 +184,6 @@ test('Expect Cockpit.image to return multiple filters', () => { const result = cockpit.image('bux', imageOptions); expect(result).toBe( - 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&f[darken]=50&f[pixelate]=40&f[desaturate]=true&f[flip]=x', + 'foo/api/cockpit/image?d=1&lang=biz&o=1&src=bux&token=bar&f[darken]=50&f[pixelate]=40&f[desaturate]=true&f[flip]=x', ); });