Skip to content

Commit 61c34e7

Browse files
committed
Add usage of token strategy to README.
1 parent cffbf30 commit 61c34e7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,60 @@ application:
6969
// ...
7070
});
7171

72+
## Usage of Token Strategy
73+
74+
#### Configure Strategy
75+
76+
The OAuth token authentication strategy authenticates users based on an
77+
access token issued to a consumer. The strategy requires a `consumer` callback,
78+
`verify` callback, and `validate` callback. The secrets supplied by the
79+
`consumer` and `verify` callbacks are used to compute a signature, and
80+
authentication fails if it does not match the request signature. `user` as
81+
supplied by the `verify` callback is the authenticating entity of this strategy,
82+
and will be set by Passport at `req.user`.
83+
84+
passport.use('token', new TokenStrategy(
85+
function(consumerKey, done) {
86+
Consumer.findByKey({ key: consumerKey }, function (err, consumer) {
87+
if (err) { return done(err); }
88+
if (!consumer) { return done(null, false); }
89+
return done(null, consumer, consumer.secret);
90+
});
91+
},
92+
function(accessToken, done) {
93+
AccessToken.findOne(accessToken, function (err, token) {
94+
if (err) { return done(err); }
95+
if (!token) { return done(null, false); }
96+
Users.findOne(token.userId, function(err, user) {
97+
if (err) { return done(err); }
98+
if (!user) { return done(null, false); }
99+
// fourth argument is optional info. typically used to pass
100+
// details needed to authorize the request (ex: `scope`)
101+
return done(null, user, token.secret, { scope: token.scope });
102+
});
103+
});
104+
},
105+
function(timestamp, nonce, done) {
106+
// validate the timestamp and nonce as necessary
107+
done(null, true)
108+
}
109+
));
110+
111+
#### Authenticate Requests
112+
113+
Use `passport.authenticate()`, specifying the `'token'` strategy, to
114+
authenticate requests. This strategy is intended for use in protected API
115+
endpoints, so the `session` option can be set to `false`.
116+
117+
For example, as route middleware in an [Express](http://expressjs.com/)
118+
application:
119+
120+
app.get('/api/userinfo',
121+
passport.authenticate('token', { session: false }),
122+
function(req, res) {
123+
res.json(req.user);
124+
});
125+
72126
## Examples
73127

74128
The [example](https://github.com/jaredhanson/oauthorize/tree/master/examples/express2)

0 commit comments

Comments
 (0)