From 4016e9675a23cdd394a8b1fc1004379221016b5d Mon Sep 17 00:00:00 2001 From: Toby Jennings Date: Thu, 5 Oct 2017 17:27:13 -0500 Subject: [PATCH 1/3] Implement header authorization --- app.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index fcdc9c2..d5138d1 100755 --- a/app.js +++ b/app.js @@ -42,6 +42,22 @@ var logFormat = "'[:date[iso]] - :remote-addr - :method :url :status :response-t app.use(morgan(logFormat)) // Middleware +//Perform request authentication if configured. +var headerAuthentication = function(req, res, next) { + + //Move on if header-auth is not specified in the config. + if (! config.hasOwnProperty("authentication_code") ) { + next(); + } else if (! req.headers.auth_code ) { + return res.status(500).json({ message: "Header authentication configured but no code supplied in request." }); + } else if ( req.headers.auth_code != config.authentication_code) { + return res.status(500).json({ message: "Header authentication failed." }); + }else{ + next(); + } +} +app.use(headerAuthentication) + // Check to make sure we have a harmonyHubClient to connect to var hasHarmonyHubClient = function(req, res, next) { if (Object.keys(harmonyHubClients).length > 0) { @@ -52,7 +68,6 @@ var hasHarmonyHubClient = function(req, res, next) { } app.use(hasHarmonyHubClient) - var discover = new harmonyHubDiscover(61991) discover.on('online', function(hubInfo) { From 4344e5f740a2599e050d11b337c9c3e2e8b27f7a Mon Sep 17 00:00:00 2001 From: Toby Jennings Date: Thu, 5 Oct 2017 17:29:15 -0500 Subject: [PATCH 2/3] Add authentication code to example config. --- config/config.sample.json | 1 + 1 file changed, 1 insertion(+) diff --git a/config/config.sample.json b/config/config.sample.json index e6ea469..d9392af 100644 --- a/config/config.sample.json +++ b/config/config.sample.json @@ -1,5 +1,6 @@ { "enableHTTPserver": true, + "authentication_code": "asdf1234", "mqtt_host": "mqtt://127.0.0.1", "topic_namespace": "harmony-api", "mqtt_options": { From c30c37a176fb70bcb7bcdd94ccbd101ffebd9511 Mon Sep 17 00:00:00 2001 From: Toby Jennings Date: Thu, 5 Oct 2017 17:34:22 -0500 Subject: [PATCH 3/3] Add header authorization notes to README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 72110ec..96213b0 100755 --- a/README.md +++ b/README.md @@ -50,6 +50,18 @@ broker's host to connect to it. `mqtt_options` is optional, see the [mqtt](https://github.com/mqttjs/MQTT.js#connect) project for allowed host and options values. +### Authorization + +The optional `authentication_code` configuration element can be used to require the presence of a HTTP header named "auth_code" with a matching value. + +```json +{ + "authentication_code": "asdf1234" +} +``` + +The header could be added via `curl`: `curl -H "auth_code:asdf1234" localhost:8282:/hubs`, for example. + ## Running It Get up and running immediately with `script/server`.