-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
86 lines (74 loc) · 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"use strict";
var connect = require("connect"),
browseridVerify = require("browserid-verify");
var defaultOptions = {
audience: "",
logoutPath: "/persona/logout",
sessionKey: "email",
verifierURI: "https://verifier.login.persona.org/verify",
verifyPath: "/persona/verify",
verifyResponse: function(error, req, res, email) {
var out;
if (error) {
out = { status: "failure", reason: error };
res.status(500);
} else {
out = { status: "okay", email: email };
}
res.json(out);
},
logoutResponse: function(error, req, res) {
var out;
if (error) {
out = { status: "failure", reason: error };
} else {
out = { status: "okay" };
}
res.json(out);
},
middleware: function(req, res, next) {
next();
}
};
module.exports = function(app, options) {
options = options || {};
var personaOpts = {};
Object.keys(defaultOptions).forEach(function(key) {
if (typeof options[key] === typeof defaultOptions[key]) {
personaOpts[key] = options[key];
} else {
personaOpts[key] = defaultOptions[key];
}
});
var verify = browseridVerify({
url: personaOpts.verifierURI
});
app.post(personaOpts.verifyPath, connect.json(), connect.urlencoded(), personaOpts.middleware, function(req, res) {
// If the body can't be parsed then we can't get the assertion
if (!req.body) {
personaOpts.verifyResponse("Server-side exception", req, res);
return;
}
verify(req.body.assertion, personaOpts.audience, function(err, email, response) {
if (err) {
if (err instanceof Error) {
err = err.message;
}
return personaOpts.verifyResponse(err, req, res);
}
if (response && response.status !== "okay") {
return personaOpts.verifyResponse(response.reason, req, res, email);
}
if (req.session) {
req.session[personaOpts.sessionKey] = email;
}
personaOpts.verifyResponse(null, req, res, email);
});
});
app.post(personaOpts.logoutPath, personaOpts.middleware, function(req, res) {
if (req.session) {
req.session[personaOpts.sessionKey] = null;
}
personaOpts.logoutResponse(null, req, res);
});
};