Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending all /co/authenticate errors to the error callback #443

Merged
merged 1 commit into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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