-
Notifications
You must be signed in to change notification settings - Fork 493
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
}); | ||
} | ||
}); | ||
|
@@ -58,22 +59,22 @@ WebMessageHandler.prototype.run = function(options, cb) { | |
) { | ||
var error = err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. This is the goal of this PR. However this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because then we'll call |
||
return cb(objectHelper.pick(error, ['error', 'error_description'])); | ||
}); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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'; | ||||
}); | ||||
|
@@ -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(); | ||||
} | ||||
|
@@ -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) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the underlying code now depends on a state and my auth0.js/test/web-auth/web-auth.test.js Line 2303 in 6ac59c4
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' | ||||
|
@@ -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' | ||||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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