I am trying to change the header challenge sent in WWW-Authenticate header in Nodejs.
Using passportjs, my current server code is the following:
router.route('/login/')
.get(function(req, res){
if(req.isAuthenticated()) res.redirect('/admin/');
else res.render('admin/partials/login.html');
})
.post(passport.authenticate('digest'));
The problem I am facing is that digest authorization first send a 401 code response (that's the normal way to go), but most browsers (webkit at least), launch a prompt popin asking user to authenticate when they detect such challenge (Digest or Basic) in header.
I read that a solution could be to change the header, modifying
WWW-Authenticate:Digest to WWW-Authenticate:xDigest for example, but I don't find a way to do it in Express.
I think that the middleware passport.authenticate must set the header and I can not find a way to change it before the response is sent to the client (in the same middleware I think).
Thanks for any ideas.
Ok, I just find that passportjs provides an easy way to adjust the response as whishes with Custom Callback.
If it can help anyone, here is the way I had to go :
router.route('/login/')
.post(function(req, res, next){
passport.authenticate('digest', function(err, user, info){
if(err) return console.log(err);
if(!user){
res.set('WWW-Authenticate', 'x'+info);
return res.send(401);
}
req.login(user, function(err){
if(err) return console.log(err);
res.redirect('/admin/');
});
})(req, res, next);
});
As you can see, the authenticate method is called inside a middleware and not as a middleware itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With