Sorry if this is a bonehead question, but I'm having some trouble understanding how I might redirect the client browser back to whatever URL was originally requested after a successful authentication with our SAML identity provider (IdP). I'm using the latest versions of passport-saml, passport, and express.
For example, say the client originally requested /foo/bar
from a link on another unprotected page, but since that is a protected resource, I respond with a redirect to /login
, which is where I call passport.authenticate('saml')
.
app.get('/login', passport.authenticate('saml'));
function ensureAuth(req, res, next) {
if (req.user.isAuthenticated()) {return next();}
else {res.redirect('/login');}
}
app.get('/foo/bar', ensureAuth, function(req, res) {
...
});
That call will redirect the browser to my IdP's sign-on page, and after a successful authentication, the IdP POSTs back to my /login/callback
route. In that route, I again use passport.authenticate(saml)
to validate the response SAML, and if all is good, I then get to redirect the browser back to the requested resource...but how do I know what that requested resource was? Because it's a POST callback, I've lost any state associated with the original request.
app.post('/login/callback', passport.authenticate('saml'), function(req, res) {
res.redirect('...can I know which url to redirect back to?...');
});
The example in the passport-saml readme just shows a hard-coded redirect back to the root resource, but I would want to redirect back to the originally-requested URL (/foo/bar
).
Can I send a url, or some other value, to the IdP that will get round-tripped and POSTed back in the response SAML? And if so, how can I access it in my /login/callback
route?
Or is there some better, express/passport way to do this that I'm missing?
Any help you can provide would be most appreciated!