Skip to content

Commit a59c4ba

Browse files
AnuiSharkadamgross42
authored andcommitted
Adding request header X-Hub-Signature validation (fbsamples#4)
* added .gitignore and updated index.js to validate header X-Hub-Signature * added .gitignore and updated index.js to validate header X-Hub-Signature * - Updated sample to directly use process.env.APP_SECRET - Updated Heroku read me with quick instructions on config vars - Fixed a couple of if spacing nits
1 parent 31ac9f4 commit a59c4ba

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Node build artifacts
2+
node_modules
3+
npm-debug.log
4+
5+
# Local development
6+
*.env
7+
*.dev
8+
.DS_Store
9+
10+
# Docker
11+
Dockerfile
12+
docker-compose.yml

heroku/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This is a sample client for [Facebook's Graph API Webhooks](https://developers.f
77
### Heroku
88
1. Deploy with this button: [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/fbsamples/graph-api-webhooks-samples)
99
1. Test your deployment with `curl https://<your-subdomain>.herokuapp.com` - you should see "It works!".
10-
10+
1. For handling webhook post request validation, ensure your Heroku app has an `APP_SECRET` config var that you can obtain from your Facebook app settings.
1111

1212
### Facebook
1313
1. Create a new [Facebook application](https://developers.facebook.com/apps).

heroku/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
var bodyParser = require('body-parser');
1010
var express = require('express');
1111
var app = express();
12+
var xhub = require('express-x-hub');
1213

1314
app.set('port', (process.env.PORT || 5000));
1415
app.listen(app.get('port'));
1516

1617
app.use(bodyParser.json());
18+
app.use(xhub({ algorithm: 'sha1', secret: process.env.APP_SECRET }));
1719

1820
app.get('/', function(req, res) {
1921
console.log(req);
@@ -33,7 +35,22 @@ app.get(['/facebook', '/instagram'], function(req, res) {
3335

3436
app.post('/facebook', function(req, res) {
3537
console.log('Facebook request body:');
38+
39+
if (req.isXHub) {
40+
console.log('request header X-Hub-Signature found, validating');
41+
if (req.isXHubValid()) {
42+
console.log('request header X-Hub-Signature validated');
43+
res.send('Verified!\n');
44+
}
45+
}
46+
else {
47+
console.log('Warning - request header X-Hub-Signature not present or invalid');
48+
res.send('Failed to verify!\n');
49+
// recommend sending 401 status in production for non-validated signatures
50+
// res.sendStatus(401);
51+
}
3652
console.log(req.body);
53+
3754
// Process the Facebook updates here
3855
res.sendStatus(200);
3956
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"dependencies": {
1010
"body-parser": "~1.15.0",
11-
"express": "~4.13.3"
11+
"express": "~4.13.3",
12+
"express-x-hub": "^1.0.4"
1213
},
1314
"engines": {
1415
"node": "0.12.7"

0 commit comments

Comments
 (0)