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

Clear local state when checkSession call fails #758

Merged
merged 2 commits into from
May 23, 2018
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/build": true
"**/build": true,
"**/docs": true
}
}
6 changes: 5 additions & 1 deletion src/web-auth/transaction-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ TransactionManager.prototype.getStoredTransaction = function(state) {
var transactionData;

transactionData = storage.getItem(this.namespace + state);
storage.removeItem(this.namespace + state);
this.clearTransaction(state);
return transactionData;
};

TransactionManager.prototype.clearTransaction = function(state) {
storage.removeItem(this.namespace + state);
};

module.exports = TransactionManager;
17 changes: 9 additions & 8 deletions src/web-auth/web-message-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function runWebMessageFlow(authorizeUrl, options, callback) {
timeoutCallback: function() {
callback({
error: 'timeout',
error_description: 'Timeout during executing web_message communication'
error_description: 'Timeout during executing web_message communication',
state: options.state
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find odd having an error object having "extra values" just for the sake of your code simplicity. It's easy to forget to filter the attributes returned to the user later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the server also sends the same object {error, error_description, state}. I'm just replicating what the server does

});
}
});
Expand Down Expand Up @@ -58,22 +59,22 @@ WebMessageHandler.prototype.run = function(options, cb) {
) {
var error = err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be

var error = err ? err : eventData.event.data.response.error
if (!error) {
  parsedHash =..
  return validateAuthenticationResponse(...);
}

//continue handling errors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see error = eventData.event.data.response;the eventData.event.data.response.error property is a string.

if (!err && eventData.event.data.response.error) {
error = objectHelper.pick(eventData.event.data.response, ['error', 'error_description']);
error = eventData.event.data.response;
}
if (!error) {
var parsedHash = eventData.event.data.response;
return _this.webAuth.validateAuthenticationResponse(options, parsedHash, cb);
}
if (
error &&
error.error === 'consent_required' &&
windowHelper.getWindow().location.hostname === 'localhost'
) {
_this.warn.warning(
"Consent Required. Consent can't be skipped on localhost. Read more here: https://auth0.com/docs/api-auth/user-consent#skipping-consent-for-first-party-clients"
);
}
if (error) {
return cb(error);
}
var parsedHash = eventData.event.data.response;
_this.webAuth.validateAuthenticationResponse(options, parsedHash, cb);
_this.webAuth.transactionManager.clearTransaction(error.state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. This is the goal of this PR. However this clearTransaction won't get called if error!=null (see line 66, happy path). Is that fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because then we'll call validateAuthenticationResponse, which needs the transaction to work (and it will be removed later)

return cb(objectHelper.pick(error, ['error', 'error_description']));
});
};

Expand Down
18 changes: 15 additions & 3 deletions test/web-auth/transaction-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,34 @@ context('TransactionManager', function() {
});
context('getStoredTransaction', function() {
beforeEach(function() {
stub(storage, 'removeItem');
stub(storage, 'getItem', function(state) {
expect(state).to.be('com.auth0.auth.state');
return { from: 'storage' };
});
spy(TransactionManager.prototype, 'clearTransaction');
});
afterEach(function() {
storage.removeItem.restore();
storage.getItem.restore();
TransactionManager.prototype.clearTransaction.restore();
});
it('returns transaction data from storage', function() {
var state = this.tm.getStoredTransaction('state');
expect(state).to.be.eql({ from: 'storage' });
});
it('removes data from storage', function() {
var state = this.tm.getStoredTransaction('state');
this.tm.getStoredTransaction('state');
expect(TransactionManager.prototype.clearTransaction.firstCall.args[0]).to.be('state');
});
});
context('clearTransaction', function() {
beforeEach(function() {
stub(storage, 'removeItem');
});
afterEach(function() {
storage.removeItem.restore();
});
it('removes data from storage', function() {
this.tm.clearTransaction('state');
expect(storage.removeItem.calledOnce).to.be(true);
expect(storage.removeItem.lastCall.args[0]).to.be('com.auth0.auth.state');
});
Expand Down
29 changes: 28 additions & 1 deletion test/web-auth/web-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2302,6 +2302,7 @@ describe('auth0.WebAuth', function() {
stub(TransactionManager.prototype, 'process', function(params) {
return Object.assign({}, params, { from: 'transaction-manager' });
});
spy(TransactionManager.prototype, 'clearTransaction');
stub(windowHelper, 'getOrigin', function() {
return 'https://test-origin.com';
});
Expand All @@ -2311,6 +2312,7 @@ describe('auth0.WebAuth', function() {
});
afterEach(function() {
TransactionManager.prototype.process.restore();
TransactionManager.prototype.clearTransaction.restore();
if (IframeHandler.prototype.init.restore) {
IframeHandler.prototype.init.restore();
}
Expand Down Expand Up @@ -2402,7 +2404,7 @@ describe('auth0.WebAuth', function() {
stub(IframeHandler.prototype, 'init', function() {
this.timeoutCallback();
});
this.auth0.checkSession({}, function(err, data) {
this.auth0.checkSession({ state: 'foobar' }, function(err, data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why has this changed? If I read the tests below (line 2452) it seems it's using this key as default state key value to remove when no error is provided (because of timeout). What would happen if this value is not given, like in the previous diff, when an error is also not provided (because of timeout)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the underlying code now depends on a state and my stub for the transactionManager.process method doesn't return a state:

return Object.assign({}, params, { from: 'transaction-manager' });

Since I already tested (elsewhere)[https://github.com/auth0/auth0.js/blob/6ac59c4faedc003820f3909aef5b5d57a572fad7/test/web-auth/web-auth.test.js#L2362] that we handle those params correctly, there's no need to stub the method again, so I just added the state

expect(err).to.be.eql({
error: 'timeout',
error_description: 'Timeout during executing web_message communication'
Expand All @@ -2427,6 +2429,31 @@ describe('auth0.WebAuth', function() {
done();
});
});
it('callback clears transaction on error response', function(done) {
var errorResponse = {
error: 'the-error',
error_description: 'error description',
state: 'foobar'
};
stub(IframeHandler.prototype, 'init', function() {
this.callback({ event: { data: { response: errorResponse } } });
});
this.auth0.checkSession({}, function(err, data) {
expect(TransactionManager.prototype.clearTransaction.firstCall.args[0]).to.be(
errorResponse.state
);
done();
});
});
it('callback clears transaction on timeout', function(done) {
stub(IframeHandler.prototype, 'init', function() {
this.timeoutCallback();
});
this.auth0.checkSession({ state: 'foobar' }, function(err, data) {
expect(TransactionManager.prototype.clearTransaction.firstCall.args[0]).to.be('foobar');
done();
});
});
it('callback writes to console when consent_required + hostname===localhost', function(done) {
var errorResponse = {
error: 'consent_required'
Expand Down