Skip to content

Commit

Permalink
Invoke verify callback inside try/catch.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Sep 11, 2013
1 parent f8982b1 commit 55eea52
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,24 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
self.success(user, info);
}

if (self._passReqToCallback) {
var arity = self._verify.length;
if (arity == 6) {
self._verify(req, accessToken, refreshToken, params, profile, verified);
} else { // arity == 5
self._verify(req, accessToken, refreshToken, profile, verified);
}
} else {
var arity = self._verify.length;
if (arity == 5) {
self._verify(accessToken, refreshToken, params, profile, verified);
} else { // arity == 4
self._verify(accessToken, refreshToken, profile, verified);
try {
if (self._passReqToCallback) {
var arity = self._verify.length;
if (arity == 6) {
self._verify(req, accessToken, refreshToken, params, profile, verified);
} else { // arity == 5
self._verify(req, accessToken, refreshToken, profile, verified);
}
} else {
var arity = self._verify.length;
if (arity == 5) {
self._verify(accessToken, refreshToken, params, profile, verified);
} else { // arity == 4
self._verify(accessToken, refreshToken, profile, verified);
}
}
} catch (ex) {
return self.error(ex);
}
});
}
Expand Down
45 changes: 45 additions & 0 deletions test/oauth2.error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,49 @@ describe('OAuth2Strategy', function() {
});
});

describe('that encounters a thrown error during verification', function() {

var strategy = new OAuth2Strategy({
authorizationURL: 'https://www.example.com/oauth2/authorize',
tokenURL: 'https://www.example.com/oauth2/token',
clientID: 'ABC123',
clientSecret: 'secret',
callbackURL: 'https://www.example.net/auth/example/callback',
},
function(accessToken, refreshToken, params, profile, done) {
throw new Error('something was thrown');
});

// inject a "mock" oauth2 instance
strategy._oauth2.getOAuthAccessToken = function(code, options, callback) {
if (code == 'SplxlOBeZQQYbYS6WxSbIA' && options.grant_type == 'authorization_code') {
return callback(null, '2YotnFZFEjr1zCsicMWpAA', 'tGzv3JOkF0XG5Qx2TlKWIA', { token_type: 'example' });
} else {
return callback(null, 'wrong-access-token', 'wrong-refresh-token');
}
}

describe('handling an authorized return request', function() {
var err;

before(function(done) {
chai.passport(strategy)
.error(function(e) {
err = e;
done();
})
.req(function(req) {
req.query = {};
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
})
.authenticate();
});

it('should error', function() {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('something was thrown');
});
});
});

});

0 comments on commit 55eea52

Please sign in to comment.