1 | # Passport |
---|
2 | |
---|
3 | Passport is [Express](http://expressjs.com/)-compatible authentication |
---|
4 | middleware for [Node.js](http://nodejs.org/). |
---|
5 | |
---|
6 | Passport's sole purpose is to authenticate requests, which it does through an |
---|
7 | extensible set of plugins known as _strategies_. Passport does not mount |
---|
8 | routes or assume any particular database schema, which maximizes flexiblity and |
---|
9 | allows application-level decisions to be made by the developer. The API is |
---|
10 | simple: you provide Passport a request to authenticate, and Passport provides |
---|
11 | hooks for controlling what occurs when authentication succeeds or fails. |
---|
12 | |
---|
13 | ## Install |
---|
14 | |
---|
15 | $ npm install passport |
---|
16 | |
---|
17 | ## Usage |
---|
18 | |
---|
19 | #### Strategies |
---|
20 | |
---|
21 | Passport uses the concept of strategies to authenticate requests. Strategies |
---|
22 | can range from verifying username and password credentials, delegated |
---|
23 | authentication using [OAuth](http://oauth.net/) (for example, via [Facebook](http://www.facebook.com/) |
---|
24 | or [Twitter](http://twitter.com/)), or federated authentication using [OpenID](http://openid.net/). |
---|
25 | |
---|
26 | Before authenticating requests, the strategy (or strategies) used by an |
---|
27 | application 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 | |
---|
39 | Passport will maintain persistent login sessions. In order for persistent |
---|
40 | sessions to work, the authenticated user must be serialized to the session, and |
---|
41 | deserialized when subsequent requests are made. |
---|
42 | |
---|
43 | Passport does not impose any restrictions on how your user records are stored. |
---|
44 | Instead, you provide functions to Passport which implements the necessary |
---|
45 | serialization and deserialization logic. In a typical application, this will be |
---|
46 | as simple as serializing the user ID, and finding the user by ID when |
---|
47 | deserializing. |
---|
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 | |
---|
61 | To use Passport in an [Express](http://expressjs.com/) or |
---|
62 | [Connect](http://senchalabs.github.com/connect/)-based application, configure it |
---|
63 | with the required `passport.initialize()` middleware. If your application uses |
---|
64 | persistent login sessions (recommended, but not required), `passport.session()` |
---|
65 | middleware 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 | |
---|
79 | Passport provides an `authenticate()` function, which is used as route |
---|
80 | middleware 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 | |
---|
90 | For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-local/tree/master/examples/login) |
---|
91 | included in [passport-local](https://github.com/jaredhanson/passport-local). |
---|
92 | |
---|
93 | ## Strategies |
---|
94 | |
---|
95 | Passport has a comprehensive set of **over 120** authentication strategies |
---|
96 | covering social networking, enterprise integration, API services, and more. |
---|
97 | The [complete list](https://github.com/jaredhanson/passport/wiki/Strategies) is |
---|
98 | available on the [wiki](https://github.com/jaredhanson/passport/wiki). |
---|
99 | |
---|
100 | The 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 | |
---|
119 | The [modules](https://github.com/jaredhanson/passport/wiki/Modules) page on the |
---|
120 | [wiki](https://github.com/jaredhanson/passport/wiki) lists other useful modules |
---|
121 | that build upon or integrate with Passport. |
---|
122 | |
---|
123 | ## Tests |
---|
124 | |
---|
125 | $ npm install --dev |
---|
126 | $ make test |
---|
127 | |
---|
128 | [](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 | |
---|
138 | Copyright (c) 2011-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)> |
---|