-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.js
39 lines (33 loc) · 1.1 KB
/
hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require('dotenv').config();
const crypto = require('crypto');
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type"
};
exports.handler = function(event, context, callback) {
let nonce = event.queryStringParameters.nonce;
let output;
if (nonce) {
output = getJsonOutput("200", "OK", hashWithNonce(nonce));
} else {
output = getJsonOutput("400", "Required querystring property is missing.", "");
}
if (process.env.DEBUG === "true") {
output.debug = getDebugInfo(nonce, event, context);
output.env = process.env;
}
callback(null, {
statusCode: 200,
headers: headers,
body: JSON.stringify(output)
});
}
function hashWithNonce(nonce) {
return crypto.createHmac("SHA256", process.env.API_SECRET).update(nonce).digest("base64");
}
function getJsonOutput(status, message, hash) {
return { "status": status, "message": message, "hash": hash };
}
function getDebugInfo(nonce, event, context) {
return { "apiKey": process.env.API_KEY, "event": event, "context": context };
}