Skip to content

Commit

Permalink
Update dependencies. Replace global variable with environment variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
aw-asana committed Oct 7, 2024
1 parent 2c30e5f commit bee6393
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
15 changes: 7 additions & 8 deletions javascript/webhooks-nodejs/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
const express = require("express");
const crypto = require("crypto");
require("dotenv").config();

// Initializes Express app.
const app = express();

// Parses JSON bodies.
app.use(express.json());

// Global variable to store the x-hook-secret
// Read more about the webhook "handshake" here: https://developers.asana.com/docs/webhooks-guide#the-webhook-handshake
let secret = "";

// Local endpoint for receiving events
app.post("/receiveWebhook", (req, res) => {
if (req.headers["x-hook-secret"]) {
console.log("This is a new webhook");
secret = req.headers["x-hook-secret"];
// Environment variable to store the X-Hook-Secret
// Read more about the webhook "handshake" here: https://developers.asana.com/docs/webhooks-guide#the-webhook-handshake
process.env.X_HOOK_SECRET = req.headers["x-hook-secret"];

res.setHeader("X-Hook-Secret", secret);
res.setHeader("X-Hook-Secret", process.env.X_HOOK_SECRET);
res.sendStatus(200);
} else if (req.headers["x-hook-signature"]) {
const computedSignature = crypto
.createHmac("SHA256", secret)
.createHmac("SHA256", process.env.X_HOOK_SECRET)
.update(JSON.stringify(req.body))
.digest("hex");

Expand All @@ -45,5 +44,5 @@ app.post("/receiveWebhook", (req, res) => {
});

app.listen(8080, () => {
console.log(`Server started on port 8080`);
console.log("Server started on port 8080");
});
19 changes: 19 additions & 0 deletions javascript/webhooks-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions javascript/webhooks-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.18.1",
"nodemon": "^2.0.16"
}
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bee6393

Please sign in to comment.