Skip to content

Commit

Permalink
verifier now works
Browse files Browse the repository at this point in the history
  • Loading branch information
darobin committed Jan 17, 2012
1 parent 53740c0 commit 8dece10
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
58 changes: 30 additions & 28 deletions lib/express-browserid.js
@@ -1,7 +1,8 @@


var path = require("path") var path = require("path")
, Shred = require("shred") , https = require("https")
, surf = new Shred , url = require("url")
, qs = require("querystring")
; ;


exports.plugAll = function (app, opts) { exports.plugAll = function (app, opts) {
Expand All @@ -23,37 +24,38 @@ exports.plugHelperScript = function (app, opts) {
exports.plugVerifier = function (app, opts) { exports.plugVerifier = function (app, opts) {
opts = opts || {}; opts = opts || {};
var route = opts.verifierPath || makePath("/verify", opts); var route = opts.verifierPath || makePath("/verify", opts);
console.log("setting up route: " + route);
app.post(route, function (req, res, next) { app.post(route, function (req, res, next) {
console.log("contacting verifier"); // this code "stolen" from the BrowserID project
surf.post({ var reqParam = url.parse(opts.verifier || "https://browserid.org/verify");
url: opts.verifier || "https://browserid.org/verify" reqParam.method = "POST";
, headers: { var vreq = https.request(reqParam, function (vres) {
accept: "application/json" var body = "";
} vres.on('data', function (chunk) { body += chunk; } )
, content: { .on('end', function () {
audience: opts.audience || req.body.audience try {
, assertion: req.body.assertion var verifierResp = JSON.parse(body)
} , valid = verifierResp && verifierResp.status === "okay"
, on: { , email = valid ? verifierResp.email : null;
200: function (data) { if (req.session) req.session.email = email;
console.log("verifier replied:", data); // if (valid) console.log("assertion verified successfully for email:", email);
if (data && "okay" === data.status) { // else console.log("failed to verify assertion:", verifierResp.reason);
if (opts.verifyCB) opts.verifyCB(null, data); res.json({ status: "okay", email: email });
} }
else { catch(e) {
if (opts.verifyCB) opts.verifyCB(new Error("BrowserID verification failed"), data); // console.log("non-JSON response from verifier");
res.json(null);
} }
res.json(data); });
}
, response: function () {
res.send("BrowserID verification failed", 500);
}
}
}); });
console.log("verifier request sent"); vreq.setHeader("Content-Type", "application/x-www-form-urlencoded");
var data = qs.stringify({
assertion: req.body.assertion
, audience: opts.audience || req.body.audience
});
vreq.setHeader("Content-Length", data.length);
vreq.write(data);
vreq.end();
}); });
console.log("route okay");
}; };


function makePath (path, opts) { function makePath (path, opts) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,6 @@
, "version": "0.0.1" , "version": "0.0.1"
, "author": "Robin Berjon <robin@berjon.com>" , "author": "Robin Berjon <robin@berjon.com>"
, "dependencies": { , "dependencies": {
"shred": "= 0.6.4"
} }
, "devDependencies": { , "devDependencies": {
"mocha": "*" "mocha": "*"
Expand Down

0 comments on commit 8dece10

Please sign in to comment.