Permalink
Browse files

Implement userProfile function for Twitter strategy.

  • Loading branch information...
1 parent 9e3f782 commit f8f2da990590fa8120b7917495ddfeb34f3e9654 @jaredhanson committed Oct 19, 2011
Showing with 47 additions and 1 deletion.
  1. +26 −0 lib/passport-twitter/strategy.js
  2. +1 −1 package.json
  3. +20 −0 test/strategy-test.js
@@ -79,6 +79,32 @@ Strategy.prototype.authenticate = function(req) {
OAuthStrategy.prototype.authenticate.call(this, req);
}
+/**
+ * Retrieve user profile from Twitter.
+ *
+ * This function constructs a normalized profile, with the following properties:
+ *
+ * - `id` (equivalent to `user_id`)
+ * - `username` (equivalent to `screen_name`)
+ *
+ * Note that because Twitter supplies basic profile information in query
+ * parameters when redirecting back to the application, loading of Twitter
+ * profiles *does not* result in an additional HTTP request.
+ *
+ * @param {String} token
+ * @param {String} tokenSecret
+ * @param {Object} params
+ * @param {Function} done
+ * @api protected
+ */
+Strategy.prototype.userProfile = function(token, tokenSecret, params, done) {
+ var profile = { provider: 'twitter' };
+ profile.id = params.user_id;
+ profile.username = params.screen_name;
+
+ return done(null, profile);
+}
+
/**
* Expose `Strategy`.
View
@@ -12,5 +12,5 @@
"passport-oauth": ">= 0.0.1"
},
"engines": { "node": ">= 0.4.0" },
- "keywords": ["passport", "twitter", "auth", "authn", "authentication"]
+ "keywords": ["passport", "twitter", "auth", "authn", "authentication", "identity"]
}
View
@@ -18,6 +18,26 @@ vows.describe('TwitterStrategy').addBatch({
'should be named twitter': function (strategy) {
assert.equal(strategy.name, 'twitter');
},
+
+ 'when told to load user profile': {
+ topic: function(strategy) {
+ var self = this;
+ function done(err, profile) {
+ self.callback(err, profile);
+ }
+ process.nextTick(function () {
+ strategy.userProfile('', '', {"user_id":"1705","screen_name":"jaredhanson"}, done);
+ });
+ },
+
+ 'should not error' : function(err, req) {
+ assert.isNull(err);
+ },
+ 'should load profile' : function(err, profile) {
+ assert.equal(profile.id, '1705');
+ assert.equal(profile.username, 'jaredhanson');
+ },
+ },
},
'strategy handling a request that has been denied': {

0 comments on commit f8f2da9

Please sign in to comment.