From ba6a38a7f6c497e025708111046d67875c4361be Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Tue, 1 Mar 2016 13:00:54 -0300 Subject: Allow CORS --- api/context.go | 4 +++ config/config.json | 1 + model/config.go | 1 + utils/config.go | 2 ++ .../components/admin_console/service_settings.jsx | 35 ++++++++++++++++++++++ web/static/i18n/en.json | 3 ++ web/static/i18n/es.json | 3 ++ 7 files changed, 49 insertions(+) diff --git a/api/context.go b/api/context.go index 9e05c5d87..3b9782851 100644 --- a/api/context.go +++ b/api/context.go @@ -166,6 +166,10 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // All api response bodies will be JSON formatted by default w.Header().Set("Content-Type", "application/json") + if len(utils.Cfg.ServiceSettings.AllowCorsFrom) > 0 { + w.Header().Set("Access-Control-Allow-Origin", utils.Cfg.ServiceSettings.AllowCorsFrom) + } + if r.Method == "GET" { w.Header().Set("Expires", "0") } diff --git a/config/config.json b/config/config.json index 2795546f8..b211b16d3 100644 --- a/config/config.json +++ b/config/config.json @@ -15,6 +15,7 @@ "EnableDeveloper": false, "EnableSecurityFixAlert": true, "EnableInsecureOutgoingConnections": false, + "AllowCorsFrom": "", "SessionLengthWebInDays": 30, "SessionLengthMobileInDays": 30, "SessionLengthSSOInDays": 30, diff --git a/model/config.go b/model/config.go index aa3dd3586..a7d92c101 100644 --- a/model/config.go +++ b/model/config.go @@ -39,6 +39,7 @@ type ServiceSettings struct { EnableDeveloper *bool EnableSecurityFixAlert *bool EnableInsecureOutgoingConnections *bool + AllowCorsFrom string SessionLengthWebInDays *int SessionLengthMobileInDays *int SessionLengthSSOInDays *int diff --git a/utils/config.go b/utils/config.go index 3e4ba5c5b..0a1d40db0 100644 --- a/utils/config.go +++ b/utils/config.go @@ -236,5 +236,7 @@ func getClientConfig(c *model.Config) map[string]string { props["WebsocketPort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketPort) props["WebsocketSecurePort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketSecurePort) + props["AllowCorsFrom"] = c.ServiceSettings.AllowCorsFrom + return props } diff --git a/web/react/components/admin_console/service_settings.jsx b/web/react/components/admin_console/service_settings.jsx index 047c7eb8d..9ed81b6a3 100644 --- a/web/react/components/admin_console/service_settings.jsx +++ b/web/react/components/admin_console/service_settings.jsx @@ -31,6 +31,10 @@ var holders = defineMessages({ id: 'admin.service.sessionDaysEx', defaultMessage: 'Ex "30"' }, + corsExample: { + id: 'admin.service.corsEx', + defaultMessage: 'http://example.com' + }, saving: { id: 'admin.service.saving', defaultMessage: 'Saving Config...' @@ -131,6 +135,8 @@ class ServiceSettings extends React.Component { config.ServiceSettings.SessionCacheInMinutes = SessionCacheInMinutes; ReactDOM.findDOMNode(this.refs.SessionCacheInMinutes).value = SessionCacheInMinutes; + config.ServiceSettings.AllowCorsFrom = ReactDOM.findDOMNode(this.refs.AllowCorsFrom).value.trim(); + Client.saveConfig( config, () => { @@ -763,6 +769,35 @@ class ServiceSettings extends React.Component { +
+ +
+ +

+ +

+
+
+