Skip to content

Commit

Permalink
Sending all /co/authenticate errors to the error callback
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge authored May 24, 2017
1 parent 24caaaf commit ca624d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 91 deletions.
15 changes: 2 additions & 13 deletions src/web-auth/cross-origin-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,10 @@ CrossOriginAuthentication.prototype.login = function(options, cb) {
this.request.post(url).withCredentials().send(authenticateBody).end(function(err, data) {
if (err) {
var errorObject = (err.response && err.response.body) || {
error: 'Request Error',
error: 'request_error',
error_description: JSON.stringify(err)
};
var authorizationErrorCodes = ['access_denied'];
var isAuthorizationError = authorizationErrorCodes.indexOf(errorObject.error) > -1;
if (cb && isAuthorizationError) {
return cb(errorObject);
}
var redirectUrl = _this.baseOptions.redirectUri || options.redirectUri;
var errorHash =
'#error=' +
encodeURI(errorObject.error) +
'&error_description=' +
encodeURI(errorObject.error_description);
return windowHelper.redirect(redirectUrl + errorHash);
return cb(errorObject);
}
options = objectHelper.blacklist(options, ['username', 'password']);
var authorizeOptions = objectHelper
Expand Down
116 changes: 38 additions & 78 deletions test/web-auth/cross-origin-authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
context(
'should call callback and not redirect to authorize when it is an authentication error',
function() {
it('access_denied', function(done) {
it('with error_description', function(done) {
stub(request, 'post', function(url) {
expect(url).to.be('https://me.auth0.com/co/authenticate');
return new RequestMock({
Expand All @@ -158,8 +158,8 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
cb({
response: {
body: {
error: 'access_denied',
error_description: 'access denied'
error: 'any_error',
error_description: 'any error'
}
}
});
Expand All @@ -174,89 +174,49 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
anotherOption: 'foobar'
},
function(err) {
expect(err).to.be.eql({ error: 'access_denied', error_description: 'access denied' });
expect(err).to.be.eql({ error: 'any_error', error_description: 'any error' });
expect(_this.webAuthSpy.authorize.called).to.be.eql(false);
done();
}
);
});
}
);
it('should call /co/authenticate and redirect to options.redirectUri when an error WITH description occur', function(
done
) {
stub(request, 'post', function(url) {
expect(url).to.be('https://me.auth0.com/co/authenticate');
return new RequestMock({
body: {
client_id: '...',
credential_type: 'password',
username: 'me@example.com',
password: '123456'
},
headers: {
'Content-Type': 'application/json'
},
cb: function(cb) {
cb({
response: {
body: {
error: 'Ops',
error_description: 'Something happened'
}
it('without error_description', function(done) {
stub(request, 'post', function(url) {
expect(url).to.be('https://me.auth0.com/co/authenticate');
return new RequestMock({
body: {
client_id: '...',
credential_type: 'password',
username: 'me@example.com',
password: '123456'
},
headers: {
'Content-Type': 'application/json'
},
cb: function(cb) {
cb({ some: 'error' });
}
});
}
});
});
stub(windowHelper, 'redirect', function(url) {
expect(url).to.be.equal(
'https://page.com/callback#error=Ops&error_description=Something%20happened'
);
done();
});

this.co.login({
username: 'me@example.com',
password: '123456',
anotherOption: 'foobar'
});
});
it('should call /co/authenticate and redirect to options.redirectUri when an error WITHOUT description occur', function(
done
) {
stub(request, 'post', function(url) {
expect(url).to.be('https://me.auth0.com/co/authenticate');
return new RequestMock({
body: {
client_id: '...',
credential_type: 'password',
username: 'me@example.com',
password: '123456'
},
headers: {
'Content-Type': 'application/json'
},
cb: function(cb) {
cb({
foo: 'bar'
});
}
});
var _this = this;
this.co.login(
{
username: 'me@example.com',
password: '123456',
anotherOption: 'foobar'
},
function(err) {
expect(err).to.be.eql({
error: 'request_error',
error_description: '{"some":"error"}'
});
expect(_this.webAuthSpy.authorize.called).to.be.eql(false);
done();
}
);
});
});
stub(windowHelper, 'redirect', function(url) {
expect(url).to.be.equal(
'https://page.com/callback#error=Request%20Error&error_description=%7B%22foo%22:%22bar%22%7D'
);
done();
});

this.co.login({
username: 'me@example.com',
password: '123456',
anotherOption: 'foobar'
});
});
}
);
});
context('callback', function() {
before(function() {
Expand Down

0 comments on commit ca624d4

Please sign in to comment.