Skip to content

Commit 5554db7

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

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports.start = function(options, callback){
6363
key: credentials.key
6464
}, module.exports.options))(req, res, function(err){
6565
if (err) {
66-
return res.send(400, err.message);
66+
return res.send(err.status || 400, err.message);
6767
}
6868
next();
6969
});
@@ -85,7 +85,7 @@ module.exports.start = function(options, callback){
8585
key: credentials.key
8686
}, module.exports.options))(req, res, function (err) {
8787
if (err) {
88-
return res.send(400, err.message);
88+
return res.send(err.status || 400, err.message);
8989
}
9090
next();
9191
});
@@ -99,12 +99,16 @@ module.exports.start = function(options, callback){
9999
key: credentials.key
100100
}, module.exports.options))(req, res, function (err) {
101101
if (err) {
102-
return res.send(400, err.message);
102+
return res.send(err.status || 400, err.message);
103103
}
104104
next();
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)