Skip to content

Commit a0cfba2

Browse files
committed
🎉 Initial commit
0 parents  commit a0cfba2

9 files changed

+173
-0
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: [
3+
"airbnb-base",
4+
"plugin:prettier/recommended"
5+
]
6+
};

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage
2+
node_modules
3+
yarn.lock

.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__tests__
2+
coverage
3+
.eslintignore
4+
.eslintrc.js
5+
jest.config.js
6+
yarn.lock
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* global test:false, expect:false */
2+
const getChallenge = require("../src/get-verification-challenge");
3+
4+
test("it can get webhook verification challenge", () => {
5+
expect(
6+
getChallenge(
7+
{
8+
query: {
9+
"hub.mode": "subscribe",
10+
"hub.verify_token": "secret",
11+
"hub.challenge": "accepted"
12+
}
13+
},
14+
"secret"
15+
)
16+
).toBe("accepted");
17+
});
18+
19+
test("it throws error if hub.mode is not subscribe", () => {
20+
expect(() =>
21+
getChallenge({
22+
query: {
23+
"hub.mode": "foo"
24+
}
25+
})
26+
).toThrow(/mode/i);
27+
});
28+
29+
test("it throws error if hub.verify_token does not match", () => {
30+
expect(() =>
31+
getChallenge(
32+
{
33+
query: {
34+
"hub.mode": "subscribe",
35+
"hub.verify_token": "secret"
36+
}
37+
},
38+
"foo"
39+
)
40+
).toThrow(/token/i);
41+
});
42+
43+
test("it throws error if hub.challenge is missing", () => {
44+
expect(() =>
45+
getChallenge(
46+
{
47+
query: {
48+
"hub.mode": "subscribe",
49+
"hub.verify_token": "secret"
50+
}
51+
},
52+
"secret"
53+
)
54+
).toThrow(/challenge/i);
55+
});

jest.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
testEnvironment: "node",
3+
verbose: true
4+
};

package.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "fbm-webhook",
3+
"description": "Facebook Messenger webhook middleware for Express",
4+
"version": "1.0.0-alpha.1",
5+
"license": "MIT",
6+
"homepage": "https://github.com/risan/fbm-webhook",
7+
"author": {
8+
"name": "Risan Bagja Pradana",
9+
"email": "[email protected]",
10+
"url": "https://bagja.net"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/risan/fbm-webhook.git"
15+
},
16+
"bugs": {
17+
"url": "https://github.com/risan/fbm-webhook/issues"
18+
},
19+
"keywords": [
20+
"facebook",
21+
"messenger",
22+
"webhook"
23+
],
24+
"main": "src/index.js",
25+
"engines": {
26+
"node": ">=8.0.0"
27+
},
28+
"scripts": {
29+
"lint": "eslint ./",
30+
"lint-fix": "eslint ./ --fix",
31+
"test": "jest"
32+
},
33+
"dependencies": {
34+
"express": "4.x",
35+
"lodash": "^4.17.11"
36+
},
37+
"devDependencies": {
38+
"eslint": "^5.9.0",
39+
"eslint-config-airbnb-base": "^13.1.0",
40+
"eslint-config-prettier": "^3.3.0",
41+
"eslint-plugin-import": "^2.14.0",
42+
"eslint-plugin-prettier": "^3.0.0",
43+
"jest": "^23.6.0",
44+
"prettier": "^1.15.2"
45+
}
46+
}

src/get-verification-challenge.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const get = require("lodash/get");
2+
const has = require("lodash/has");
3+
4+
/**
5+
* Get webhook verification challenge.
6+
*
7+
* @param {Object} req
8+
* @param {String} token
9+
* @return {String}
10+
*/
11+
const getVerificationChallenge = (req, token) => {
12+
if (get(req, ["query", "hub.mode"]) !== "subscribe") {
13+
throw new Error("Invalid hub.mode.");
14+
}
15+
16+
if (get(req, ["query", "hub.verify_token"]) !== token) {
17+
throw new Error("Invalid hub.verify_token.");
18+
}
19+
20+
if (!has(req, ["query", "hub.challenge"])) {
21+
throw new Error("hub.challenge is missing");
22+
}
23+
24+
return get(req, ["query", "hub.challenge"]);
25+
};
26+
27+
module.exports = getVerificationChallenge;

src/verification.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const express = require("express");
2+
const getChallenge = require("./get-verification-challenge");
3+
4+
/**
5+
* Get webhook verification middleware.
6+
*
7+
* @param {String} token
8+
* @return {Router}
9+
*/
10+
const verification = (token = process.env.FB_VERIFY_TOKEN) => {
11+
const router = express.Router();
12+
13+
router.get("/", (req, res) => {
14+
try {
15+
const challenge = getChallenge(req, token);
16+
res.status(200).send(challenge);
17+
} catch (error) {
18+
res.status(403).send("Forbidden");
19+
}
20+
});
21+
22+
return router;
23+
};
24+
25+
module.exports = verification;

0 commit comments

Comments
 (0)