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

Last change on this file was 489, checked in by hendrikvanantwerpen, 11 years ago

Update node modules

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