Skip to content

Commit

Permalink
Better doc. Custom error message from keys.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
julien66 committed Dec 3, 2019
1 parent c73d091 commit 21ada84
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM node:10

COPY . /app
COPY /etc/keys.js /app/keys.js
WORKDIR /app
RUN yarn install

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Inspired by:

## API usage

!!! This fork REQUIRE an api key for each request with a parameter ?key=APIKEYGOESHERE
Rename existing keys_example.js file, place it in your /etc/keys.js and store properly hashed (sha256 hex format) keys inside the file !!!

The service has a very simple API. Just post your latitude-longitude pairs as a JSON array to the service and receive an array of elevations as response. Maximum post payload is by default 700 KB (which fits roughly 10,000 points).

```bash
Expand Down
15 changes: 6 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ async function handlePOST(req, res) {

if (!apiKey) {
return send(res, 400, {
error: apiMessage('Missing'),
error: keys.message('Missing'),
});
}

if (verifyKey(apiKey) < 0) {
return send(res, 400, {
error: apiMessage('Invalid'),
error: keys.message('Invalid'),
});
}

Expand Down Expand Up @@ -65,13 +65,13 @@ async function handleGET(req, res) {
const apiKey = reqQuery.key;
if (!apiKey) {
return send(res, 400, {
error: apiMessage('Missing'),
error: keys.message('Missing'),
});
}

if (verifyKey(apiKey) < 0) {
return send(res, 400, {
error: apiMessage('Invalid'),
error: keys.message('Invalid'),
});
}

Expand Down Expand Up @@ -115,16 +115,13 @@ async function handler(req, res) {
}
}

// This function verify if the hash of the api key passed by the user
// request matches a key stored inside keys.js file
function verifyKey(key) {
const shasum = crypto.createHash('sha256');
shasum.update(key);
const digest = shasum.digest('hex');
return allKeys.indexOf(digest);
}

function apiMessage(wrong) {
return wrong + ' API key. Please email Téo or Julien at [email protected] or [email protected].'
}


module.exports = cors(handler);
12 changes: 12 additions & 0 deletions keys_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file should be renamed as keys.js
// It is aimed to store an array of hashed keys to be compared against the hashed api key
// received from the user request.
// A custom message can be returned in case of 'Missing' or 'Invalid' api key.
module.exports = {
get : function() {
return ['first-hashed-key-goes-here', 'second-hashed-key-goes-here'];
},
message : function(wrong) {
return wrong + ' API key. Custom message... Wrong is either invalid or missing';
},
}

0 comments on commit 21ada84

Please sign in to comment.