UNPKG

4.14 kBMarkdownView Raw
1passport-token
2==============
3
4Username and token authentication strategy for Passport - modified from [passport-local](https://github.com/jaredhanson/passport-local).
5
6## Installation
7 $ npm install passport-token
8
9## Usage
10
11The token authentication strategy authenticates users using a username and token. The strategy requires a verify callback, which accepts these credentials and calls done providing a user.
12
13 var TokenStrategy = require('passport-token').Strategy;
14
15 passport.use(new TokenStrategy(
16 function (username, token, done) {
17 User.findOne({username: username}, function (err, user) {
18 if (err) {
19 return done(err);
20 }
21
22 if (!user) {
23 return done(null, false);
24 }
25
26 if (!user.verifyToken(token)) {
27 return done(null, false);
28 }
29
30 return done(null, user);
31 });
32 }
33 ));
34
35By default, passport-token checks for `username` and `token` credentials in either the header or request body in these locations:
36
37### Headers
38
39 x-username
40 x-token
41
42### Body fields
43
44 username
45 token
46
47### Configure
48
49These credential locations can be configured when defining the strategy as follows:
50
51 var TokenStrategy = require('passport-token').Strategy;
52 var strategyOptions = {
53 usernameHeader: 'x-custom-username',
54 tokenHeader: 'x-custom-token',
55 usernameField: 'custom-username',
56 tokenField: 'custom-token'
57 };
58
59 passport.use(new TokenStrategy(strategyOptions,
60 function (username, token, done) {
61 User.findOne({username: username}, function (err, user) {
62 if (err) {
63 return done(err);
64 }
65
66 if (!user) {
67 return done(null, false);
68 }
69
70 if (!user.verifyToken(token)) {
71 return done(null, false);
72 }
73
74 return done(null, user);
75 });
76 }
77
78
79## Authenticate
80
81Use `passport.authenticate()`, specifying the `token` strategy to authenticate requests.
82
83For example, as route middleware in an [Express](http://expressjs.com/) application:
84
85 app.put('/animals/dogs', passport.authenticate('token'), function (req, res) {
86 // User authenticated and can be found in req.user
87 });
88
89If authentication fails in the above example then a 401 response will be given. However there may be times you wish a bit more control and delegate the failure to your application:
90
91 app.put('/animals/dogs', authenticate, function (req, res) {
92 // User authenticated and can be found in req.user
93 });
94
95 function authenticate(req, res, next) {
96 passport.authenticate('token', function (err, user, info) {
97 if (err) {
98 return next(err);
99 }
100
101 if (!user) {
102 return res.status(401).json({message: "Incorrect token credentials"});
103 }
104
105 req.user = user;
106 next();
107 });
108 }
109
110## Credits
111[Jared Hanson](http://github.com/jaredhanson)
112
113## License
114(The MIT License)
115
116Copyright (c) 2011 Jared Hanson
117
118Permission 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:
119
120The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
121
122THE 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.