From 8180f8bb2ac7cefeb7af0fa48c33d3272d083972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Thu, 26 Oct 2017 20:08:42 +0200 Subject: [PATCH 1/6] Propagate CORS-enabling to Restivus - fixes #7915 (cherry picked from commit cf7d37f) --- packages/rocketchat-api/server/api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index 1f0ee1a669e9..108a2e3ae57d 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -182,13 +182,13 @@ RocketChat.API.v1 = new API({ version: 'v1', useDefaultAuth: true, prettyJson: true, - enableCors: false, + enableCors: RocketChat.settings.get('API_Enable_CORS'), auth: getUserAuth() }); RocketChat.API.default = new API({ useDefaultAuth: true, prettyJson: true, - enableCors: false, + enableCors: RocketChat.settings.get('API_Enable_CORS'), auth: getUserAuth() }); From 92f1e2d224ada0eda948b97a1c5e491da5112154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 1 Nov 2017 09:09:12 +0100 Subject: [PATCH 2/6] Re-instantiate API after changing CORS-setting --- packages/rocketchat-api/server/api.js | 37 +++++++++++++++++---------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index 108a2e3ae57d..02e82b37e95e 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -52,7 +52,7 @@ class API extends Restivus { this.authMethods.push(method); } - success(result={}) { + success(result = {}) { if (_.isObject(result)) { result.success = true; } @@ -111,7 +111,7 @@ class API extends Restivus { if (this.helperMethods) { Object.keys(endpoints).forEach((method) => { if (typeof endpoints[method] === 'function') { - endpoints[method] = { action: endpoints[method] }; + endpoints[method] = {action: endpoints[method]}; } //Add a try/catch for each endpoint @@ -178,17 +178,26 @@ const getUserAuth = function _getUserAuth() { }; }; -RocketChat.API.v1 = new API({ - version: 'v1', - useDefaultAuth: true, - prettyJson: true, - enableCors: RocketChat.settings.get('API_Enable_CORS'), - auth: getUserAuth() -}); +let enableCors = RocketChat.settings.get('API_Enable_CORS'); + +const createApi = function() { + RocketChat.API.v1 = new API({ + version: 'v1', + useDefaultAuth: true, + prettyJson: true, + enableCors, + auth: getUserAuth() + }); + + RocketChat.API.default = new API({ + useDefaultAuth: true, + prettyJson: true, + enableCors, + auth: getUserAuth() + }); +}; -RocketChat.API.default = new API({ - useDefaultAuth: true, - prettyJson: true, - enableCors: RocketChat.settings.get('API_Enable_CORS'), - auth: getUserAuth() +RocketChat.settings.onload('API_Enable_CORS', (key, value) => { + enableCors = value; + createApi(); }); From dee0f58786d6129b545aafe4cf42a05f2c5b9ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 6 Dec 2017 08:27:43 +0100 Subject: [PATCH 3/6] use settings.get() instead of onLoad() --- packages/rocketchat-api/server/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index d82d1f26a33c..8660a67cb723 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -199,7 +199,7 @@ const createApi = function() { }); }; -RocketChat.settings.onload('API_Enable_CORS', (key, value) => { +RocketChat.settings.get('API_Enable_CORS', (key, value) => { enableCors = value; createApi(); }); From 3021ce4db17d887620ff7da026b888259f05d0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 6 Dec 2017 09:25:33 +0100 Subject: [PATCH 4/6] Create the API also immediately --- packages/rocketchat-api/server/api.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index 8660a67cb723..bf9728895f75 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -181,7 +181,6 @@ const getUserAuth = function _getUserAuth() { }; let enableCors = RocketChat.settings.get('API_Enable_CORS'); - const createApi = function() { RocketChat.API.v1 = new API({ version: 'v1', @@ -203,3 +202,6 @@ RocketChat.settings.get('API_Enable_CORS', (key, value) => { enableCors = value; createApi(); }); + +// also create the API immediately +createApi(); From 7775a1f75b6883ef481df54a9d11435f40497c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 6 Dec 2017 09:26:39 +0100 Subject: [PATCH 5/6] Create the API also immediately --- packages/rocketchat-api/server/api.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index 8660a67cb723..45b457a0ac87 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -181,7 +181,6 @@ const getUserAuth = function _getUserAuth() { }; let enableCors = RocketChat.settings.get('API_Enable_CORS'); - const createApi = function() { RocketChat.API.v1 = new API({ version: 'v1', @@ -199,7 +198,11 @@ const createApi = function() { }); }; +// register the API to be re-created once the CORS-setting changes. RocketChat.settings.get('API_Enable_CORS', (key, value) => { enableCors = value; createApi(); }); + +// also create the API immediately +createApi(); From 287d97a8266baae03fa8eb8aae0979da3a4ba839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 6 Dec 2017 14:59:37 +0100 Subject: [PATCH 6/6] Only create new API once the setting changes. --- packages/rocketchat-api/server/api.js | 40 ++++++++++++++------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/rocketchat-api/server/api.js b/packages/rocketchat-api/server/api.js index 45b457a0ac87..871f136f6a9a 100644 --- a/packages/rocketchat-api/server/api.js +++ b/packages/rocketchat-api/server/api.js @@ -180,29 +180,31 @@ const getUserAuth = function _getUserAuth() { }; }; -let enableCors = RocketChat.settings.get('API_Enable_CORS'); -const createApi = function() { - RocketChat.API.v1 = new API({ - version: 'v1', - useDefaultAuth: true, - prettyJson: true, - enableCors, - auth: getUserAuth() - }); - - RocketChat.API.default = new API({ - useDefaultAuth: true, - prettyJson: true, - enableCors, - auth: getUserAuth() - }); +const createApi = function(enableCors) { + if (!RocketChat.API.v1 || RocketChat.API.v1._config.enableCors !== enableCors) { + RocketChat.API.v1 = new API({ + version: 'v1', + useDefaultAuth: true, + prettyJson: true, + enableCors, + auth: getUserAuth() + }); + } + + if (!RocketChat.API.default || RocketChat.API.default._config.enableCors !== enableCors) { + RocketChat.API.default = new API({ + useDefaultAuth: true, + prettyJson: true, + enableCors, + auth: getUserAuth() + }); + } }; // register the API to be re-created once the CORS-setting changes. RocketChat.settings.get('API_Enable_CORS', (key, value) => { - enableCors = value; - createApi(); + createApi(value); }); // also create the API immediately -createApi(); +createApi(!!RocketChat.settings.get('API_Enable_CORS'));