Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to add more userProfile parser to use other google API #36

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ reports/
# Node.js
node_modules
npm-debug.log

package-lock.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/

.jshintrc
.travis.yml
package-lock.json
23 changes: 23 additions & 0 deletions lib/errors/parsererror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* `ParserError` error.
*
* @constructor
* @param {string} [message]
* @param {string} [code]
* @access public
*/
function ParserError(message, code) {
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
this.name = 'ParserError';
this.message = message;
this.code = code;
}

// Inherit from `Error`.
ParserError.prototype.__proto__ = Error.prototype;


// Expose constructor.
module.exports = ParserError;

35 changes: 21 additions & 14 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var OAuth2Strategy = require('passport-oauth2')
, OpenIDProfile = require('./profile/openid')
, InternalOAuthError = require('passport-oauth2').InternalOAuthError
, GooglePlusAPIError = require('./errors/googleplusapierror')
, ParserError = require('./errors/parsererror')
, UserInfoError = require('./errors/userinfoerror');


Expand All @@ -24,6 +25,7 @@ var OAuth2Strategy = require('passport-oauth2')
* - `clientID` your Google application's client id
* - `clientSecret` your Google application's client secret
* - `callbackURL` URL to which Google will redirect the user after granting authorization
* - `profileParser` an object containing a name and a parse(json) function if you want to consume another google API (ie. People API)
*
* Examples:
*
Expand Down Expand Up @@ -52,12 +54,22 @@ function Strategy(options, verify) {
OAuth2Strategy.call(this, options, verify);
this.name = 'google';
this._userProfileURL = options.userProfileURL || 'https://www.googleapis.com/plus/v1/people/me';

var url = uri.parse(this._userProfileURL);
if (url.pathname.indexOf('/userinfo') == (url.pathname.length - '/userinfo'.length)) {
this._userProfileFormat = 'openid';

this.profileParsers = {
'openid': OpenIDProfile,
'google+': GooglePlusProfile
};

if (options.profileParser) {
this._userProfileFormat = options.profileParser.name || 'custom';
this.profileParsers[this._userProfileFormat] = options.profileParser;
} else {
this._userProfileFormat = 'google+'; // Google Sign-In
var url = uri.parse(this._userProfileURL);
if (url.pathname.indexOf('/userinfo') == (url.pathname.length - '/userinfo'.length)) {
this._userProfileFormat = 'openid';
} else {
this._userProfileFormat = 'google+'; // Google Sign-In
}
}
}

Expand Down Expand Up @@ -105,16 +117,11 @@ Strategy.prototype.userProfile = function(accessToken, done) {
return done(new Error('Failed to parse user profile'));
}

var profile;
switch (self._userProfileFormat) {
case 'openid':
profile = OpenIDProfile.parse(json);
break;
default: // Google Sign-In
profile = GooglePlusProfile.parse(json);
break;
var parser = self.profileParsers[self._userProfileFormat];
if (!parser) {
return done(new ParserError('Parser not found for profile format '+ self._userProfileFormat));
}

var profile = parser.parse(json);
profile.provider = 'google';
profile._raw = body;
profile._json = json;
Expand Down