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

(4.9) fix: handleLoginRedirect should not redirect after error #731

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 4.9.1

### Bug Fixes

- [#731](https://github.com/okta/okta-auth-js/pull/731) Fixes issue with `handleLoginRedirect` where a redirect could occur after an exception was thrown.

## 4.9.0

### Bug Fixes
Expand Down
7 changes: 6 additions & 1 deletion lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,12 @@ class OktaAuthBrowser extends OktaAuthBase implements OktaAuth, SignoutAPI {
if (tokens) {
this.tokenManager.setTokens(tokens);
} else if (this.isLoginRedirect()) {
await this.storeTokensFromRedirect();
try {
await this.storeTokensFromRedirect();
} catch (e) {
this.authStateManager.unsubscribe(handleRedirect);
throw e;
}
} else {
this.authStateManager.unsubscribe(handleRedirect);
}
Expand Down
28 changes: 28 additions & 0 deletions test/spec/OktaAuth/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,34 @@ describe('Browser', function() {
}, 100);
});
});

it('will not redirect if parseFromUrl throws an error', async () => {
const error = new Error('mock error');
auth.token.parseFromUrl = jest.fn().mockImplementation(async () => {
throw error;
});
auth.isLoginRedirect = jest.fn().mockReturnValue(true);
let errorThrown = false;
try {
await auth.handleLoginRedirect();
} catch (e) {
expect(e).toBe(error);
errorThrown = true;
}
expect(errorThrown).toBe(true);
await auth.authStateManager.updateAuthState(); // this will call subscribe handler if still listening

return new Promise(resolve => {
// wait for the next emitted authState
setTimeout(() => {
expect(auth.authStateManager.unsubscribe).toHaveBeenCalled();
expect(auth.getOriginalUri).not.toHaveBeenCalled();
expect(auth.removeOriginalUri).not.toHaveBeenCalled();
expect(window.location.replace).not.toHaveBeenCalled();
resolve(undefined);
}, 100);
});
});
});

describe('isPKCE', () => {
Expand Down
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10761,14 +10761,6 @@ no-case@^3.0.3:
lower-case "^2.0.1"
tslib "^1.10.0"

node-cache@^4.2.0:
version "4.2.1"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-4.2.1.tgz#efd8474dee4edec4138cdded580f5516500f7334"
integrity sha512-BOb67bWg2dTyax5kdef5WfU3X8xu4wPg+zHzkvls0Q/QpYycIFRLEEIdAx9Wma43DxG6Qzn4illdZoYseKWa4A==
dependencies:
clone "2.x"
lodash "^4.17.15"

node-cache@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d"
Expand Down