Passport is an authentication framework for Connect and Express, which is extensible through "plugins" known as strategies.
Passport is designed to be a general-purpose, yet simple, modular, and unobtrusive, authentication framework. Passport's sole purpose is to authenticate requests. In being modular, it doesn't force any particular authentication strategy on your application. In being unobtrusive, it doesn't mount routes in your application. The API is simple: you give Passport a request to authenticate, and Passport provides hooks for controlling what occurs when authentication succeeds or fails.
$ npm install passport
Passport uses the concept of strategies to authenticate requests. Strategies can range from verifying username and password credentials, delegated authentication using OAuth (for example, via Facebook or Twitter), or federated authentication using OpenID.
Before asking passport to authenticate a request, the strategy (or strategies) used by an application must be configured.
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username, password: password }, function (err, user) {
done(err, user);
});
}
));
Passport will maintain persistent login sessions. In order for persistent sessions to work, the authenticated user must be serialized to the session, and deserialized when subsequent requests are made.
Passport does not impose any restrictions on how your user records are stored. Instead, you provide a function to Passport which implements the necessary serialization and deserialization logic. In typical applications, this will be as simple as serializing the user ID, and finding the user by ID when deserializing.
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
To use Passport in a Connect or
Express-based application, configure it with the
required passport.initialize()
middleware. If your applications uses
persistent login sessions (recommended, but not required), passport.session()
middleware must also be used.
app.configure(function() {
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/../../public'));
});
Passport provides an authenticate()
function (which is standard
Connect/Express middleware), which is utilized to authenticate requests.
For example, it can be used as route middleware in an Express application:
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
For a complete, working example, refer to the login example included in Passport-Local.
- Local (username and password)
- BrowserID
- OpenID
- OAuth (OAuth 1.0 and 2.0)
- SAML by Henri Bergius
- WS-Federation + SAML 2.0 by Auth10
- WebID by Baptiste Lafontaine
- 23andMe by Michael Owens
- 37signals
- 500px by Jeremy Benaim
- AngelList
- AOL
- App.net by Michael Owens
- Bitbucket
- Buffer by Sébastien De Bollivier
- DailyCred by Hank Stoever
- Digg
- doctape
- Dropbox
- Dwolla
- Evernote
- FamilySearch
- Fitbit
- Flattr by Johan Uhle
- Flickr by Johnny Halife
- Force.com (Salesforce, Database.com) by Joshua Birk
- Foursquare
- FreedomWorks by Carlos Rodriguez
- Geeklist by Sébastien De Bollivier
- Geoloqi
- GitHub
- Goodreads
- Google (OpenID)
- Google (OAuth 1.0 and 2.0)
- Gowalla
- HackID by Hackers @ Berkeley
- Intuit (OpenID)
- Intuit (OAuth 1.0)
- Justin.tv
- me2day by JeongHoon Byun
- Meetup
- Netflix
- Odnoklassniki by Alexey Kozlov
- Ohloh
- OpenStreetMap
- PayPal (OpenID)
- PayPal (OAuth 2.0)
- picplz
- Raven
- Rdio
- Readability
- RunKeeper
- SmugMug
- SharePoint by QuePort
- SoundCloud
- StatusNet by ZooWar
- Steam by Liam Curry
- SUPINFO by Vincent PEYROUSE
- Trade Me by Tumunu
- TripIt
- Tumblr
- TxSSC by Texas School Safety Center
- Urlship by Urlship
- Vimeo
- VKontakte by Stepan Stolyarov
- Windows Live
- Withings by Michael Owens
- Yahoo! (OpenID)
- Yahoo! (OAuth 1.0)
- Yammer
- Yandex by Sergey Sergeev
- Atlassian Crowd by Andreas Knecht
- OpenSSO by Róbert Oroszi
- OpenAM by Alesium
- Drupal by Victor Kareh (for Drupal-powered sites using OAuth Login Provider module)
- HTTP (HTTP Basic and Digest schemes)
- HTTP-Bearer (HTTP Bearer scheme)
- HTTP-OAuth (HTTP OAuth scheme)
- OAuth2-Client-Password (OAuth 2.0 client password)
- Hash (hash parameter) by Yuri Karadzhov
- Anonymous
- Dummy by Development Seed
Attention Developers: If you implement a new authentication strategy for Passport, send me a message and I will update the list.
$ npm install --dev
$ make test
Copyright (c) 2011-2013 Jared Hanson <http://jaredhanson.net/>