Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Request Header Authorization #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
17 changes: 16 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -52,7 +68,6 @@ var hasHarmonyHubClient = function(req, res, next) {
}
app.use(hasHarmonyHubClient)


var discover = new harmonyHubDiscover(61991)

discover.on('online', function(hubInfo) {
Expand Down
1 change: 1 addition & 0 deletions config/config.sample.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enableHTTPserver": true,
"authentication_code": "asdf1234",
"mqtt_host": "mqtt://127.0.0.1",
"topic_namespace": "harmony-api",
"mqtt_options": {
Expand Down