Permalink
Browse files

Add bearer token strategy and API endpoint to example.

  • Loading branch information...
1 parent c2e5ca6 commit 501bf2bffba5e10a036c8242940e6949585c3464 @jaredhanson committed Jul 10, 2012
Showing with 43 additions and 1 deletion.
  1. +3 −1 examples/express2/app.js
  2. +18 −0 examples/express2/auth.js
  3. +1 −0 examples/express2/package.json
  4. +21 −0 examples/express2/user.js
View
4 examples/express2/app.js
@@ -5,7 +5,7 @@ var express = require('express')
, passport = require('passport')
, site = require('./site')
, oauth2 = require('./oauth2')
- //, user = require('./user')
+ , user = require('./user')
, util = require('util')
@@ -44,4 +44,6 @@ app.get('/dialog/authorize', oauth2.authorization);
app.post('/dialog/authorize/decision', oauth2.decision);
app.post('/oauth/token', oauth2.token);
+app.get('/api/userinfo', user.info);
+
app.listen(3000);
View
18 examples/express2/auth.js
@@ -4,6 +4,7 @@
var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy
+ , BearerStrategy = require('passport-http-bearer').Strategy
, db = require('./db')
@@ -47,3 +48,20 @@ passport.use(new ClientPasswordStrategy(
});
}
));
+
+passport.use(new BearerStrategy(
+ function(accessToken, done) {
+ db.accessTokens.find(accessToken, function(err, token) {
+ if (err) { return done(err); }
+ if (!token) { return done(null, false); }
+
+ db.users.find(token.userID, function(err, user) {
+ if (err) { return done(err); }
+ if (!user) { return done(null, false); }
+ // to keep this example simple, restricted scopes are not implemented
+ var info = { scope: '*' }
+ done(null, user, info);
+ });
+ });
+ }
+));
View
1 examples/express2/package.json
@@ -7,6 +7,7 @@
"oauth2orize": "0.x.x",
"passport": "0.1.x",
"passport-local": "0.1.x",
+ "passport-http-bearer": "0.x.x",
"passport-oauth2-client-password": "0.1.x",
"connect-ensure-login": "0.1.x"
}
View
21 examples/express2/user.js
@@ -0,0 +1,21 @@
+/**
+ * Module dependencies.
+ */
+var passport = require('passport')
+
+exports.info = [
+ /*
+ function(req, res, next) {
+ console.log('!!!! API REQUEST !!!!');
+ console.dir(req.headers)
+ next();
+ },
+ */
+ passport.authenticate('bearer', { session: false }),
+ function(req, res) {
+ // TODO: Set scope in response, for demonstration purposes
+ console.log('AUTH INFO');
+ console.dir(req.authInfo)
+ res.json({ user_id: req.user.id, name: req.user.name })
+ }
+]

0 comments on commit 501bf2b

Please sign in to comment.