Permalink
Browse files

Accept `profileFields` strategy parameter

  • Loading branch information...
1 parent 4ba37f6 commit ba026adb7b8ed9c93cf580456f981e8b210137fd @stevebest stevebest committed Jun 28, 2012
Showing with 114 additions and 8 deletions.
  1. +55 −8 lib/passport-facebook/strategy.js
  2. +59 −0 test/strategy-test.js
@@ -45,7 +45,9 @@ function Strategy(options, verify) {
options.authorizationURL = options.authorizationURL || 'https://www.facebook.com/dialog/oauth';
options.tokenURL = options.tokenURL || 'https://graph.facebook.com/oauth/access_token';
options.scopeSeparator = options.scopeSeparator || ',';
-
+
+ this.profileFields = options.profileFields || [];
+
OAuth2Strategy.call(this, options, verify);
this.name = 'facebook';
}
@@ -98,7 +100,13 @@ Strategy.prototype.authorizationParams = function (options) {
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
- this._oauth2.getProtectedResource('https://graph.facebook.com/me', accessToken, function (err, body, res) {
+ var self = this,
+ fields = this._convertProfileFields();
+
+ var url = 'https://graph.facebook.com/me'
+ + (fields !== '' ? '?fields=' + fields : '');
+
+ this._oauth2.getProtectedResource(url, accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
try {
@@ -108,23 +116,62 @@ Strategy.prototype.userProfile = function(accessToken, done) {
profile.id = json.id;
profile.username = json.username;
profile.displayName = json.name;
- profile.name = { familyName: json.last_name,
- givenName: json.first_name,
- middleName: json.middle_name };
+
+ if (self._includeProfileField('name')) {
+ profile.name = { familyName: json.last_name,
+ givenName: json.first_name,
+ middleName: json.middle_name };
+ }
+
profile.gender = json.gender;
profile.profileUrl = json.link;
- profile.emails = [{ value: json.email }];
-
+
+ if (self._includeProfileField('emails')) {
+ profile.emails = [{ value: json.email }];
+ }
+ if (self._includeProfileField('photos')) {
+ profile.photos = [{ value: json.picture }];
+ }
+
profile._raw = body;
profile._json = json;
-
+
done(null, profile);
} catch(e) {
done(e);
}
});
}
+Strategy.prototype._includeProfileField = function(field) {
+ return this.profileFields.length === 0
+ || this.profileFields.indexOf(field) !== -1;
+}
+
+Strategy.prototype._convertProfileFields = function() {
+ var fields = [];
+ var fieldsMap = {
+ 'id': 'id',
+ 'username': 'username',
+ 'displayName': 'name',
+ 'name': ['last_name', 'first_name', 'middle_name'],
+ 'gender': 'gender',
+ 'profileUrl': 'link',
+ 'emails': 'email',
+ 'photos': 'picture'
+ };
+ this.profileFields.forEach(function (f) {
+ if (typeof fieldsMap[f] === 'undefined') return;
+
+ if (typeof fieldsMap[f] === 'object') { // actually, array
+ Array.prototype.push.apply(fields, fieldsMap[f]);
+ } else {
+ fields.push(fieldsMap[f]);
+ }
+ });
+
+ return fields.join(',');
+}
/**
* Expose `Strategy`.
View
@@ -163,5 +163,64 @@ vows.describe('FacebookStrategy').addBatch({
},
},
},
+
+ 'strategy when configured to load specific profile fields': {
+ topic: function() {
+ var strategy = new FacebookStrategy({
+ clientID: 'ABC123',
+ clientSecret: 'secret',
+ profileFields: ['id', 'photos']
+ },
+ function() {});
+
+ // mock
+ strategy._oauth2.getProtectedResource = function(url, accessToken, callback) {
+ var body = '{"id":"500308595","picture":"http://profile.ak.fbcdn.net/blahblahblah.jpg"}';
+
+ callback(null, body, undefined);
+ }
+
+ return strategy;
+ },
+
+ 'when converting field names to facebook api': {
+ topic: function(strategy) {
+ this.callback(null, strategy._convertProfileFields());
+ },
+
+ 'should return a string of comma-separated field names': function(err, fields) {
+ assert.equal(fields, 'id,picture');
+ }
+ },
+
+ '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('access-token', done);
+ });
+ },
+
+ 'should not error' : function(err, req) {
+ assert.isNull(err);
+ },
+ 'should load specific fields': function(err, profile) {
+ assert.equal(profile.provider, 'facebook');
+ assert.equal(profile.id, '500308595');
+ assert.isUndefined(profile.username);
+ assert.isUndefined(profile.displayName);
+ assert.isUndefined(profile.name);
+ assert.isUndefined(profile.gender);
+ assert.isUndefined(profile.profileUrl);
+ assert.isUndefined(profile.emails);
+ assert.deepEqual(profile.photos, [{value: 'http://profile.ak.fbcdn.net/blahblahblah.jpg' }]);
+ }
+ }
+
+ },
}).export(module);

0 comments on commit ba026ad

Please sign in to comment.