Skip to content

Commit e85ffb3

Browse files
committed
Fix #88 - Pass errors to next in express
1 parent 98641f7 commit e85ffb3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/samlp.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ module.exports.auth = function(options) {
136136

137137
function execute (postUrl, audience, req, res, next) {
138138
var user = opts.getUserFromRequest(req);
139-
if (!user) return res.send(401);
139+
if (!user) {
140+
const err = new Error('SAML unauthorized');
141+
err.status = 401;
142+
return next(err);
143+
}
140144

141145
opts.audience = audience;
142146
opts.postUrl = postUrl;
@@ -176,8 +180,12 @@ module.exports.auth = function(options) {
176180
}
177181

178182
opts.getPostURL(audience, samlRequestDom, req, function (err, postUrl) {
179-
if (err) { return res.send(500, err); }
180-
if (!postUrl) { return res.send(401); }
183+
if (err) { return next(err); }
184+
if (!postUrl) {
185+
const error = new Error('SAML unauthorized error, postUrl not received');
186+
error.status = 401;
187+
return next(error);
188+
}
181189

182190
execute(postUrl, audience, req, res, next);
183191
});

test/fixture/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ module.exports.start = function(options, callback){
105105
});
106106
});
107107

108+
app.use(function (error, req, res, next) {
109+
return res.status(error.status || 500).send(error.message);
110+
});
111+
108112
var server = http.createServer(app).listen(5050, callback);
109113
module.exports.close = server.close.bind(server);
110114
};

0 commit comments

Comments
 (0)