diff --git a/lib/strategy.js b/lib/strategy.js index 01d78b1..3e4faef 100644 --- a/lib/strategy.js +++ b/lib/strategy.js @@ -163,8 +163,13 @@ OAuth2Strategy.prototype.authenticate = function(req, options) { self._oauth2.getOAuthAccessToken(code, params, function(err, accessToken, refreshToken, params) { - if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); } - + if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err));} + if (params.error) { + var error = new Object(); + error.statusCode = params.error; + error.data = params.error_description; + return self.error(self._createOAuthError('Failed to obtain access token', error)); + } self._loadUserProfile(accessToken, function(err, profile) { if (err) { return self.error(err); } diff --git a/package.json b/package.json index 3ee8418..3ff1383 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "passport-oauth2", - "version": "1.3.0", + "version": "1.3.1", "description": "OAuth 2.0 authentication strategy for Passport.", "keywords": [ "passport", diff --git a/test/oauth2.test.js b/test/oauth2.test.js index f3949f0..0a223e3 100644 --- a/test/oauth2.test.js +++ b/test/oauth2.test.js @@ -1168,6 +1168,46 @@ describe('OAuth2Strategy', function() { expect(err.oauthError.data).to.equal('Something went wrong'); }); }); // that errors due to token request error, in node-oauth object literal form with text body + + describe('that errors due to token request error, param object contains errors', 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) { + return done(new Error('verify callback should not be called')); + }); + + strategy._oauth2.getOAuthAccessToken = function(code, options, callback) { + return callback(null, '2YotnFZFEjr1zCsicMWpAA', 'tGzv3JOkF0XG5Qx2TlKWIA', { error: 500, error_description: 'Something went wrong' }); + } + + + var param; + + before(function(done) { + chai.passport.use(strategy) + .error(function(e) { + param = e; + done(); + }) + .req(function(req) { + req.query = {}; + req.query.code = 'SplxlOBeZQQYbYS6WxSbIA'; + }) + .authenticate(); + }); + + it('should error', function() { + expect(param).to.be.an.instanceof(InternalOAuthError) + expect(param.message).to.equal('Failed to obtain access token'); + expect(param.oauthError.statusCode).to.equal(500); + expect(param.oauthError.data).to.equal('Something went wrong'); + }); + }); // that errors due to token request error, in node-oauth object literal form with text body describe('that errors due to verify callback supplying error', function() { var strategy = new OAuth2Strategy({