Skip to content

Commit 82c7664

Browse files
committed
Additional documentation for TokenStrategy.
1 parent 436e774 commit 82c7664

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/passport-http-oauth/strategies/consumer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var passport = require('passport')
8484
* if (err) { return done(err); }
8585
* if (!token) { return done(null, false); }
8686
* // third argument is optional info. typically used to pass
87-
* // details needed to authorize the request (ex: `scope`)
87+
* // details needed to authorize the request (ex: `verifier`)
8888
* return done(null, token.secret, { verifier: token.verifier });
8989
* });
9090
* },

lib/passport-http-oauth/strategies/token.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ var passport = require('passport')
1515
* located in an `Authorization` header field, the request entity body, or the
1616
* URL query parameters.
1717
*
18+
* This strategy requires three functions as callbacks, referred to as
19+
* `consumer`, `verify` and `validate`.
20+
*
21+
* The `consumer` callback accepts `consumerKey` and must call `done` supplying
22+
* a `consumer` and `consumerSecret`. The strategy will use the secret to
23+
* compute the signature, failing authentication if it does not match the
24+
* request's signature. If an exception occured, `err` should be set.
25+
*
26+
* The `verify` callback accepts `accessToken` and must call `done` supplying
27+
* a `user`, `tokenSecret` and optional `info`. Note that `user` is the
28+
* authenticating entity of this strategy, and will be set by Passport at
29+
* `req.user` upon success. The strategy will use the secret to compute the
30+
* signature, failing authentication if it does not match the request's
31+
* signature. The optional `info` is typically used to carry additional
32+
* authorization details associated with the token (scope of access, for
33+
* example). `info` will be set by Passport at `req.authInfo`, where it can be
34+
* used by later middleware, avoiding the need to re-query a database for the
35+
* same information. If an exception occured, `err` should be set.
36+
*
37+
* The `validate` callback is optional, accepting `timestamp` and `nonce` as a
38+
* means to protect against replay attacks.
39+
*
40+
*
1841
* Note that despite defining a single authentication scheme, OAuth
1942
* authentication serves two distinct purposes:
2043
* 1. Authenticating consumers (aka clients) that are requesting access to
@@ -37,6 +60,35 @@ var passport = require('passport')
3760
*
3861
* This strategy is inteded to be employed in routes for protected resources.
3962
*
63+
* Examples:
64+
*
65+
* passport.use('token', new TokenStrategy(
66+
* function(consumerKey, done) {
67+
* Consumer.findByKey({ key: consumerKey }, function (err, consumer) {
68+
* if (err) { return done(err); }
69+
* if (!consumer) { return done(null, false); }
70+
* return done(null, consumer, consumer.secret);
71+
* });
72+
* },
73+
* function(accessToken, done) {
74+
* AccessToken.findOne(accessToken, function (err, token) {
75+
* if (err) { return done(err); }
76+
* if (!token) { return done(null, false); }
77+
* Users.findOne(token.userId, function(err, user) {
78+
* if (err) { return done(err); }
79+
* if (!user) { return done(null, false); }
80+
* // fourth argument is optional info. typically used to pass
81+
* // details needed to authorize the request (ex: `scope`)
82+
* return done(null, user, token.secret, { scope: token.scope });
83+
* });
84+
* });
85+
* },
86+
* function(timestamp, nonce, done) {
87+
* // validate the timestamp and nonce as necessary
88+
* done(null, true)
89+
* }
90+
* ));
91+
*
4092
* References:
4193
* - [Authenticated Requests](http://tools.ietf.org/html/rfc5849#section-3)
4294
* - [Accessing Protected Resources](http://oauth.net/core/1.0a/#anchor12)

0 commit comments

Comments
 (0)