-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better doc. Custom error message from keys.js.
- Loading branch information
Showing
4 changed files
with
22 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'), | ||
}); | ||
} | ||
|
||
|
@@ -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'), | ||
}); | ||
} | ||
|
||
|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}, | ||
} |