source: Dev/trunk/src/node_modules/passport/README.md @ 488

Last change on this file since 488 was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 5.8 KB
Line 
1# Passport
2
3Passport is [Express](http://expressjs.com/)-compatible authentication
4middleware for [Node.js](http://nodejs.org/).
5
6Passport's sole purpose is to authenticate requests, which it does through an
7extensible set of plugins known as _strategies_.  Passport does not mount
8routes or assume any particular database schema, which maximizes flexiblity and
9allows application-level decisions to be made by the developer.  The API is
10simple: you provide Passport a request to authenticate, and Passport provides
11hooks for controlling what occurs when authentication succeeds or fails.
12
13## Install
14
15    $ npm install passport
16
17## Usage
18
19#### Strategies
20
21Passport uses the concept of strategies to authenticate requests.  Strategies
22can range from verifying username and password credentials, delegated
23authentication using [OAuth](http://oauth.net/) (for example, via [Facebook](http://www.facebook.com/)
24or [Twitter](http://twitter.com/)), or federated authentication using [OpenID](http://openid.net/).
25
26Before authenticating requests, the strategy (or strategies) used by an
27application must be configured.
28
29    passport.use(new LocalStrategy(
30      function(username, password, done) {
31        User.findOne({ username: username, password: password }, function (err, user) {
32          done(err, user);
33        });
34      }
35    ));
36
37#### Sessions
38
39Passport will maintain persistent login sessions.  In order for persistent
40sessions to work, the authenticated user must be serialized to the session, and
41deserialized when subsequent requests are made.
42
43Passport does not impose any restrictions on how your user records are stored.
44Instead, you provide functions to Passport which implements the necessary
45serialization and deserialization logic.  In a typical application, this will be
46as simple as serializing the user ID, and finding the user by ID when
47deserializing.
48
49    passport.serializeUser(function(user, done) {
50      done(null, user.id);
51    });
52
53    passport.deserializeUser(function(id, done) {
54      User.findById(id, function (err, user) {
55        done(err, user);
56      });
57    });
58
59#### Middleware
60
61To use Passport in an [Express](http://expressjs.com/) or
62[Connect](http://senchalabs.github.com/connect/)-based application, configure it
63with the required `passport.initialize()` middleware.  If your application uses
64persistent login sessions (recommended, but not required), `passport.session()`
65middleware must also be used.
66
67    app.configure(function() {
68      app.use(express.static(__dirname + '/../../public'));
69      app.use(express.cookieParser());
70      app.use(express.bodyParser());
71      app.use(express.session({ secret: 'keyboard cat' }));
72      app.use(passport.initialize());
73      app.use(passport.session());
74      app.use(app.router);
75    });
76
77#### Authenticate Requests
78
79Passport provides an `authenticate()` function, which is used as route
80middleware to authenticate requests.
81
82    app.post('/login',
83      passport.authenticate('local', { failureRedirect: '/login' }),
84      function(req, res) {
85        res.redirect('/');
86      });
87
88## Examples
89
90For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-local/tree/master/examples/login)
91included in [passport-local](https://github.com/jaredhanson/passport-local).
92
93## Strategies
94
95Passport has a comprehensive set of **over 120** authentication strategies
96covering social networking, enterprise integration, API services, and more.
97The [complete list](https://github.com/jaredhanson/passport/wiki/Strategies) is
98available on the [wiki](https://github.com/jaredhanson/passport/wiki).
99
100The following table lists commonly used strategies:
101
102|Strategy                                                       | Protocol                 |Developer                                       |
103|---------------------------------------------------------------|--------------------------|------------------------------------------------|
104|[Local](https://github.com/jaredhanson/passport-local)         | HTML form                |[Jared Hanson](https://github.com/jaredhanson)  |
105|[OpenID](https://github.com/jaredhanson/passport-openid)       | OpenID                   |[Jared Hanson](https://github.com/jaredhanson)  |
106|[BrowserID](https://github.com/jaredhanson/passport-browserid) | BrowserID                |[Jared Hanson](https://github.com/jaredhanson)  |
107|[Facebook](https://github.com/jaredhanson/passport-facebook)   | OAuth 2.0                |[Jared Hanson](https://github.com/jaredhanson)  |
108|[Google](https://github.com/jaredhanson/passport-google)       | OpenID                   |[Jared Hanson](https://github.com/jaredhanson)  |
109|[Google](https://github.com/jaredhanson/passport-google-oauth) | OAuth / OAuth 2.0        |[Jared Hanson](https://github.com/jaredhanson)  |
110|[Twitter](https://github.com/jaredhanson/passport-twitter)     | OAuth                    |[Jared Hanson](https://github.com/jaredhanson)  |
111
112## Related Modules
113
114- [Locomotive](https://github.com/jaredhanson/locomotive) — Powerful MVC web framework
115- [OAuthorize](https://github.com/jaredhanson/oauthorize) — OAuth service provider toolkit
116- [OAuth2orize](https://github.com/jaredhanson/oauth2orize) — OAuth 2.0 authorization server toolkit
117- [connect-ensure-login](https://github.com/jaredhanson/connect-ensure-login)  — middleware to ensure login sessions
118
119The [modules](https://github.com/jaredhanson/passport/wiki/Modules) page on the
120[wiki](https://github.com/jaredhanson/passport/wiki) lists other useful modules
121that build upon or integrate with Passport.
122
123## Tests
124
125    $ npm install --dev
126    $ make test
127
128[![Build Status](https://secure.travis-ci.org/jaredhanson/passport.png)](http://travis-ci.org/jaredhanson/passport)
129
130## Credits
131
132  - [Jared Hanson](http://github.com/jaredhanson)
133
134## License
135
136[The MIT License](http://opensource.org/licenses/MIT)
137
138Copyright (c) 2011-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>
Note: See TracBrowser for help on using the repository browser.