Skip to content

Commit

Permalink
verification is now implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
julien51 committed Apr 7, 2012
1 parent da7859c commit 8704cf6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -14,4 +14,4 @@ You need:
## TODO

* Use https to susbcribe
* Implement signatures to veirify origin of content
* Implement signatures to verify origin of content (we can use a secret set in the heroku:config for example.)
15 changes: 10 additions & 5 deletions app.js
Expand Up @@ -32,15 +32,20 @@ app.get('/', require('./routes/index.js').index);
app.get('/atom', require('./routes/atom.js').atom); // Serves the agregated feed as Atom
app.get('/json', require('./routes/json.js').json); // Serves the agregated feed as json. Supports the optional jsonp argument
app.get('/feed/:id', require('./routes/verification.js').verification); // PubSubhubbub verification mechanism
app.post('/feed/:id', require('./routes/verification.js').notification); // PubSubHubbub notification mechanism
app.post('/feed/:id', require('./routes/notification.js').notification); // PubSubHubbub notification mechanism

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

// When starting, we need to subscribe to all the feeds in the configuration.
for(var i in feeds) {
subscribe(feeds[i], function(res) {
if(res)
console.log("Subscribed to " + feeds[i]);
for(var i=0; i<feeds.length; i++) {
var url = feeds[i];
subscribe(url, i, function(err, url) {
if(err) {
console.error(err);
}
else {
console.log("Subscribed to", url);
}
});
}
4 changes: 2 additions & 2 deletions config/feeds.js
@@ -1,5 +1,5 @@
exports.feeds = [
// "http://push-pub.appspot.com/feed",
// "http://blog.msgboy.com/rss",
"http://push-pub.appspot.com/feed",
"http://blog.msgboy.com/rss",
"http://blog.superfeedr.com/atom"
]
18 changes: 9 additions & 9 deletions lib/subscriber.js
Expand Up @@ -10,41 +10,41 @@ var options = {
path: '/hubbub',
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': auth
}
};

exports.subscribe = function(url, done) {
exports.subscribe = function(url, id, done) {

if(typeof(process.env.APP_NAME) === "undefined") {
console.error("Please set your application's name as an environment variable: heroku config:add APP_NAME=weird-name-1337");
}

done("Please set your application's name as an environment variable: heroku config:add APP_NAME=weird-name-1337", url);
}

var params = querystring.stringify({
'hub.mode': 'subscribe',
'hub.callback': 'http://mycallback.com/',
'hub.callback': 'http://' + process.env.APP_NAME + '/feed/' + id,
'hub.topic': url
})

options.headers['Content-Length'] = params.length;

var req = http.request(options, function(res) {
if(res.statusCode === 401) {
console.error("Make sure you have installed the Superfeedr plugin: $ heroku addons:add superfeedr");
done("Make sure you have installed the Superfeedr plugin: $ heroku addons:add superfeedr", url);
}
else if(res.statusCode === 422) {
res.on('data', function (chunk) {
console.error(chunk.toString());
done(chunk.toString(), url);
});
}
else if(res.statusCode === 204) {
done(true);
done(null, url);
}
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
done(e.message, url);
});

req.write(params);
Expand Down
6 changes: 5 additions & 1 deletion routes/notification.js
Expand Up @@ -3,5 +3,9 @@
*/

exports.notification = function(req, res){
res.render('index', { title: 'Express' })
console.log(req.params.id);
req.on('data', function (chunk) {
console.log(chunk.toString());
});
res.send("Thanks");
};
8 changes: 7 additions & 1 deletion routes/verification.js
@@ -1,7 +1,13 @@
/*
* GET /feed/:id Verified the intent for a feed.
*/
var feeds = require('../config/feeds.js').feeds;

exports.verification = function(req, res){
res.render('index', { title: 'Express' })
if(req.query['hub.mode'] === "subscribe" && feeds[req.params.id] === req.query['hub.topic']) {
res.send(req.query['hub.challenge']);
}
else {
res.send("Nope, can't confirm that");
}
};

0 comments on commit 8704cf6

Please sign in to comment.