HTTP Basic and Digest authentication strategies for Passport.
This module lets you authenticate HTTP requests using the standard basic and digest schemes in your Node.js applications. By plugging into Passport, support for these schemes can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
$ npm install passport-http
The HTTP Basic authentication strategy authenticates users using a userid and
password. The strategy requires a verify
callback, which accepts these
credentials and calls done
providing a user.
passport.use(new BasicStrategy(
function(userid, password, done) {
User.findOne({ username: userid }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
Use passport.authenticate()
, specifying the 'basic'
strategy, to
authenticate requests. Requests containing an 'Authorization' header do not
require session support, so the session
option can be set to false
.
For example, as route middleware in an Express application:
app.get('/private',
passport.authenticate('basic', { session: false }),
function(req, res) {
res.json(req.user);
});
For a complete, working example, refer to the Basic example.
The HTTP Digest authentication strategy authenticates users using a username and
password (aka shared secret). The strategy requires a secret
callback, which
accepts a username
and calls done
providing a user and password known to the
server. The password is used to compute a hash, and authentication fails if it
does not match that contained in the request.
The strategy also accepts an optional validate
callback, which receives
nonce-related params
that can be further inspected to determine if the request
is valid.
passport.use(new DigestStrategy({ qop: 'auth' },
function(username, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
return done(null, user, user.password);
});
},
function(params, done) {
// validate nonces as necessary
done(null, true)
}
));
Use passport.authenticate()
, specifying the 'digest'
strategy, to
authenticate requests. Requests containing an 'Authorization' header do not
require session support, so the session
option can be set to false
.
For example, as route middleware in an Express application:
app.get('/private',
passport.authenticate('digest', { session: false }),
function(req, res) {
res.json(req.user);
});
For a complete, working example, refer to the Digest example.
$ npm install --dev
$ make test
(The MIT License)
Copyright (c) 2011 Jared Hanson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.