Permalink
Browse files

verifier now works

  • Loading branch information...
1 parent 53740c0 commit 8dece103fa84c21ba442d42bcce32444964e77f6 @darobin committed Jan 17, 2012
Showing with 30 additions and 29 deletions.
  1. +30 −28 lib/express-browserid.js
  2. +0 −1 package.json
@@ -1,7 +1,8 @@
var path = require("path")
-, Shred = require("shred")
-, surf = new Shred
+, https = require("https")
+, url = require("url")
+, qs = require("querystring")
;
exports.plugAll = function (app, opts) {
@@ -23,37 +24,38 @@ exports.plugHelperScript = function (app, opts) {
exports.plugVerifier = function (app, opts) {
opts = opts || {};
var route = opts.verifierPath || makePath("/verify", opts);
- console.log("setting up route: " + route);
app.post(route, function (req, res, next) {
- console.log("contacting verifier");
- surf.post({
- url: opts.verifier || "https://browserid.org/verify"
- , headers: {
- accept: "application/json"
- }
- , content: {
- audience: opts.audience || req.body.audience
- , assertion: req.body.assertion
- }
- , on: {
- 200: function (data) {
- console.log("verifier replied:", data);
- if (data && "okay" === data.status) {
- if (opts.verifyCB) opts.verifyCB(null, data);
+ // this code "stolen" from the BrowserID project
+ var reqParam = url.parse(opts.verifier || "https://browserid.org/verify");
+ reqParam.method = "POST";
+ var vreq = https.request(reqParam, function (vres) {
+ var body = "";
+ vres.on('data', function (chunk) { body += chunk; } )
+ .on('end', function () {
+ try {
+ var verifierResp = JSON.parse(body)
+ , valid = verifierResp && verifierResp.status === "okay"
+ , email = valid ? verifierResp.email : null;
+ if (req.session) req.session.email = email;
+ // if (valid) console.log("assertion verified successfully for email:", email);
+ // else console.log("failed to verify assertion:", verifierResp.reason);
+ res.json({ status: "okay", email: email });
}
- else {
- if (opts.verifyCB) opts.verifyCB(new Error("BrowserID verification failed"), data);
+ catch(e) {
+ // 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) {
View
@@ -4,7 +4,6 @@
, "version": "0.0.1"
, "author": "Robin Berjon <robin@berjon.com>"
, "dependencies": {
- "shred": "= 0.6.4"
}
, "devDependencies": {
"mocha": "*"

0 comments on commit 8dece10

Please sign in to comment.