Permalink
Browse files

modified to accept passReqToCallback in options to return request

  • Loading branch information...
1 parent 9f27680 commit 43ccaa7b4da09bb46ca7a200af09f24d18ee20f0 Shao Chen committed Dec 20, 2012
Showing with 38 additions and 3 deletions.
  1. +27 −0 examples/bearer/app.js
  2. +11 −3 lib/passport-http-bearer/strategy.js
View
@@ -43,6 +43,33 @@ passport.use(new BearerStrategy({
}
));
+/*
+// Return back the request object example by passing in options "passReqToCallback": true
+// Use the BearerStrategy within Passport.
+// Strategies in Passport require a `validate` function, which accept
+// credentials (in this case, a token), and invoke a callback with a user
+// object.
+passport.use(new BearerStrategy({ "passReqToCallback": true },
+ function(req, token, done) {
+
+ //req is passed back here
+ console.log(req);
+ // asynchronous validation, for effect...
+ process.nextTick(function () {
+
+ // Find the user by token. If there is no user with the given token, set
+ // the user to `false` to indicate failure. Otherwise, return the
+ // authenticated `user`. Note that in a production-ready application, one
+ // would want to validate the token for authenticity.
+ findByToken(token, function(err, user) {
+ if (err) { return done(err); }
+ if (!user) { return done(null, false); }
+ return done(null, user);
+ })
+ });
+ }
+));
+*/
@@ -55,6 +55,7 @@ function Strategy(options, verify) {
if (options.scope) {
this._scope = (Array.isArray(options.scope)) ? options.scope : [ options.scope ];
}
+ this._passReqToCallback = options.passReqToCallback;
}
/**
@@ -98,12 +99,19 @@ Strategy.prototype.authenticate = function(req) {
if (!token) { return this.fail(this._challenge()); }
- var self = this;
- this._verify(token, function(err, user, info) {
+ function verified(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(self._challenge('invalid_token')); }
self.success(user, info);
- });
+ }
+
+ var self = this;
+ if (self._passReqToCallback) {
+ this._verify(req, token, verified);
+ } else {
+ this._verify(token, verified);
+ }
+
}
/**

0 comments on commit 43ccaa7

Please sign in to comment.