Skip to content

Commit

Permalink
Fail or error depending on error code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Aug 2, 2013
1 parent b4d2657 commit 61845a7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 22 deletions.
33 changes: 33 additions & 0 deletions lib/errors/authorizationerror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* `AuthorizationError` error.
*
* @api public
*/
function AuthorizationError(message, code, uri, status) {
if (!status) {
switch (code) {
case 'access_denied': status = 403; break;
case 'server_error': status = 502; break;
case 'temporarily_unavailable': status = 503; break;
}
}

Error.call(this);
Error.captureStackTrace(this, arguments.callee);
this.name = 'AuthorizationError';
this.message = message || 'OAuth 2.0 authorization error';
this.code = code || 'server_error';
this.uri = uri;
this.status = status || 500;
};

/**
* Inherit from `Error`.
*/
AuthorizationError.prototype.__proto__ = Error.prototype;


/**
* Expose `AuthorizationError`.
*/
module.exports = AuthorizationError;
7 changes: 6 additions & 1 deletion lib/strategies/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var passport = require('passport-strategy')
, util = require('util')
, utils = require('./utils')
, OAuth2 = require('oauth').OAuth2
, AuthorizationError = require('../errors/authorizationerror')
, InternalOAuthError = require('../errors/internaloautherror');


Expand Down Expand Up @@ -103,7 +104,11 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
var self = this;

if (req.query && req.query.error) {
return this.fail({ code: req.query.error, message: req.query.error_description, helpURL: req.query.error_uri });
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;
Expand Down
90 changes: 69 additions & 21 deletions test/strategies/oauth2.default.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var chai = require('chai')
, OAuth2Strategy = require('../../lib/strategies/oauth2');
, OAuth2Strategy = require('../../lib/strategies/oauth2')
, AuthorizationError = require('../../lib/errors/authorizationerror');


describe('OAuth2Strategy with default options', function() {
Expand Down Expand Up @@ -225,7 +226,7 @@ describe('OAuth2Strategy with default options', function() {
});
});

describe('handling a request that has been denied by the user', function() {
describe('handling a request that has been denied by the user without description', function() {
var info;

before(function(done) {
Expand All @@ -243,13 +244,11 @@ describe('OAuth2Strategy with default options', function() {

it('should supply info', function() {
expect(info).to.not.be.undefined;
expect(info.code).to.equal('access_denied');
expect(info.message).to.be.undefined;
expect(info.helpURL).to.be.undefined;
});
});

describe('handling a request that has been denied by the user and has description', function() {
describe('handling a request that has been denied by the user with description', function() {
var info;

before(function(done) {
Expand All @@ -260,43 +259,92 @@ describe('OAuth2Strategy with default options', function() {
})
.req(function(req) {
req.query = {};
req.query.error = 'temporarily_unavailable';
req.query.error_description = 'Try again later';
req.query.error = 'access_denied';
req.query.error_description = 'Why oh why?';
})
.authenticate();
});

it('should supply info', function() {
expect(info).to.not.be.undefined;
expect(info.code).to.equal('temporarily_unavailable');
expect(info.message).to.equal('Try again later');
expect(info.helpURL).to.be.undefined;
expect(info.message).to.equal('Why oh why?');
});
});

describe('handling a request that has been denied by the user and has description and help link', function() {
var info;
describe('handling a request that indicates a server error without description', function() {
var err;

before(function(done) {
chai.passport(strategy)
.fail(function(i) {
info = i;
.error(function(e) {
err = e;
done();
})
.req(function(req) {
req.query = {};
req.query.error = 'invalid_scope';
})
.authenticate();
});

it('should error', function() {
expect(err).to.be.an.instanceof(AuthorizationError)
expect(err.message).to.equal('OAuth 2.0 authorization error');
expect(err.code).to.equal('invalid_scope');
expect(err.uri).to.be.undefined;
expect(err.status).to.equal(500);
});
});

describe('handling a request that indicates a server error with description', function() {
var err;

before(function(done) {
chai.passport(strategy)
.error(function(e) {
err = e;
done();
})
.req(function(req) {
req.query = {};
req.query.error = 'temporarily_unavailable';
req.query.error_description = 'Try again later';
req.query.error = 'invalid_scope';
req.query.error_description = 'The scope is invalid';
})
.authenticate();
});

it('should error', function() {
expect(err).to.be.an.instanceof(AuthorizationError)
expect(err.message).to.equal('The scope is invalid');
expect(err.code).to.equal('invalid_scope');
expect(err.uri).to.be.undefined;
expect(err.status).to.equal(500);
});
});

describe('handling a request that indicates a server error with description and link', function() {
var err;

before(function(done) {
chai.passport(strategy)
.error(function(e) {
err = e;
done();
})
.req(function(req) {
req.query = {};
req.query.error = 'invalid_scope';
req.query.error_description = 'The scope is invalid';
req.query.error_uri = 'http://www.example.com/oauth2/help';
})
.authenticate();
});

it('should supply info', function() {
expect(info).to.not.be.undefined;
expect(info.code).to.equal('temporarily_unavailable');
expect(info.message).to.equal('Try again later');
expect(info.helpURL).to.equal('http://www.example.com/oauth2/help');
it('should error', function() {
expect(err).to.be.an.instanceof(AuthorizationError)
expect(err.message).to.equal('The scope is invalid');
expect(err.uri).to.equal('http://www.example.com/oauth2/help');
expect(err.status).to.equal(500);
});
});

Expand Down

0 comments on commit 61845a7

Please sign in to comment.