Skip to content

Commit

Permalink
Merge a2662f6 into b3d886f
Browse files Browse the repository at this point in the history
  • Loading branch information
Furze committed Jul 7, 2015
2 parents b3d886f + a2662f6 commit 2fc1048
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/strategy.js
Expand Up @@ -76,13 +76,13 @@ function OAuth2Strategy(options, verify) {
options = undefined;
}
options = options || {};

if (!verify) { throw new TypeError('OAuth2Strategy requires a verify callback'); }
if (!options.authorizationURL) { throw new TypeError('OAuth2Strategy requires a authorizationURL option'); }
if (!options.tokenURL) { throw new TypeError('OAuth2Strategy requires a tokenURL option'); }
if (!options.clientID) { throw new TypeError('OAuth2Strategy requires a clientID option'); }
if (!options.clientSecret) { throw new TypeError('OAuth2Strategy requires a clientSecret option'); }

passport.Strategy.call(this);
this.name = 'oauth2';
this._verify = verify;
Expand All @@ -97,6 +97,7 @@ function OAuth2Strategy(options, verify) {
this._scope = options.scope;
this._scopeSeparator = options.scopeSeparator || ' ';
this._state = options.state;
this._team = options.team;
this._key = options.sessionKey || ('oauth2:' + url.parse(options.authorizationURL).hostname);
this._trustProxy = options.proxy;
this._passReqToCallback = options.passReqToCallback;
Expand All @@ -118,15 +119,15 @@ util.inherits(OAuth2Strategy, passport.Strategy);
OAuth2Strategy.prototype.authenticate = function(req, options) {
options = options || {};
var self = this;

if (req.query && req.query.error) {
if (req.query.error == 'access_denied') {
return this.fail({ message: req.query.error_description });
} else {
return this.error(new AuthorizationError(req.query.error_description, req.query.error, req.query.error_uri));
}
}

var callbackURL = options.callbackURL || this._callbackURL;
if (callbackURL) {
var parsed = url.parse(callbackURL);
Expand All @@ -136,13 +137,13 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
callbackURL = url.resolve(utils.originalURL(req, { proxy: this._trustProxy }), callbackURL);
}
}

if (req.query && req.query.code) {
var code = req.query.code;

if (this._state) {
if (!req.session) { return this.error(new Error('OAuth2Strategy requires session support when using state. Did you forget app.use(express.session(...))?')); }

var key = this._key;
if (!req.session[key]) {
return this.fail({ message: 'Unable to verify authorization request state.' }, 403);
Expand All @@ -151,12 +152,12 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
if (!state) {
return this.fail({ message: 'Unable to verify authorization request state.' }, 403);
}

delete req.session[key].state;
if (Object.keys(req.session[key]).length === 0) {
delete req.session[key];
}

if (state !== req.query.state) {
return this.fail({ message: 'Invalid authorization request state.' }, 403);
}
Expand All @@ -169,16 +170,16 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
this._oauth2.getOAuthAccessToken(code, params,
function(err, accessToken, refreshToken, params) {
if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); }

self._loadUserProfile(accessToken, function(err, profile) {
if (err) { return self.error(err); }

function verified(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
self.success(user, info);
}

try {
if (self._passReqToCallback) {
var arity = self._verify.length;
Expand Down Expand Up @@ -210,19 +211,23 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
if (Array.isArray(scope)) { scope = scope.join(this._scopeSeparator); }
params.scope = scope;
}
var team = options.team || this._team;
if(team){
params.team = team;
}
var state = options.state;
if (state) {
params.state = state;
} else if (this._state) {
if (!req.session) { return this.error(new Error('OAuth2Strategy requires session support when using state. Did you forget app.use(express.session(...))?')); }

var key = this._key;
state = uid(24);
if (!req.session[key]) { req.session[key] = {}; }
req.session[key].state = state;
params.state = state;
}

var location = this._oauth2.getAuthorizeUrl(params);
this.redirect(location);
}
Expand Down Expand Up @@ -309,14 +314,14 @@ OAuth2Strategy.prototype.parseErrorResponse = function(body, status) {
*/
OAuth2Strategy.prototype._loadUserProfile = function(accessToken, done) {
var self = this;

function loadIt() {
return self.userProfile(accessToken, done);
}
function skipIt() {
return done(null);
}

if (typeof this._skipUserProfile == 'function' && this._skipUserProfile.length > 1) {
// async
this._skipUserProfile(accessToken, function(err, skip) {
Expand Down

0 comments on commit 2fc1048

Please sign in to comment.