flatiron-persona
Version:
Flatiron plugin for user authentication with Mozilla Persona
62 lines (55 loc) • 1.82 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var $, debug, request;
request = require("request");
debug = require("debug");
$ = debug("persona:login");
module.exports = function() {
var verifier,
_this = this;
if (this.req.session == null) {
throw new Error("flatiron-persona: There is no session object in request.\n\n Flatiron Persona plugin needs to set @req.session.username to indicate that user is authenticated.\n\n Make sure your application supports session. You may use connect session middleware, as shown here: https://github.com/lzrski/flatiron-persona#flatiron-persona");
}
if (!this.req.body || !this.req.body.assertion) {
$("No assertion given. Body is:", this.req.body);
this.res.json(400, {
error: "No assertion given"
});
return;
}
$("Authenticating user to %s", this.persona.audience);
verifier = {
url: "https://verifier.login.persona.org/verify",
json: true,
body: {
assertion: this.req.body.assertion,
audience: this.persona.audience
}
};
return request.post(verifier, function(error, response, body) {
$ = debug("persona:login:verification");
if (error) {
$("Error: %j", error);
_this.res.json(500, {
error: "Verification error"
});
return;
}
$("Verification response body %j", body);
if (body.status === "okay") {
$("User %s authorized", body.email);
_this.req.session.username = body.email;
_this.req.session.save();
return _this.res.json(200, {
username: body.email
});
} else {
$("Authorization failed");
return _this.res.json(401, {});
}
});
};
}).call(this);
/*
//@ sourceMappingURL=login.map
*/