Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

fix[react]: store secureRouterReferrerPath in sessionStorage #884

Merged
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
6 changes: 6 additions & 0 deletions packages/okta-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 3.0.6

### Bug Fixes

- [#884](https://github.com/okta/okta-oidc-js/pull/884) Stores `secureReferrerPath` in sessionStorage to avoid race condition for multiple tabs

# 3.0.5

### Bug Fixes
Expand Down
4 changes: 2 additions & 2 deletions packages/okta-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okta/okta-react",
"version": "3.0.5",
"version": "3.0.6",
"description": "React support for Okta",
"main": "./dist/index.js",
"scripts": {
Expand Down Expand Up @@ -34,7 +34,7 @@
"homepage": "https://github.com/okta/okta-oidc-js#readme",
"dependencies": {
"@okta/configuration-validation": "^0.4.1",
"@okta/okta-auth-js": "^3.2.2",
"@okta/okta-auth-js": "^3.2.3",
"babel-runtime": "^6.26.0",
"prop-types": "^15.5.10"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/okta-react/src/AuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ class AuthService {
if (fromUri.charAt(0) === '/') {
fromUri = window.location.origin + fromUri;
}
localStorage.setItem( 'secureRouterReferrerPath', fromUri );
sessionStorage.setItem( 'secureRouterReferrerPath', fromUri );
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like the kind of issue that blew us up when we had conflicting versions of Auth JS between the SIW and outside the SIW.

@aarongranick-okta - as I recall, the solution was to first check for the localStorage version, then fall back to the sessionStorage, but that won't work for this scenario (which is specifically multiple tabs), right? Am I overthinking this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, but I saw okta-angular is implemented with sessionStorage.

Copy link
Contributor

Choose a reason for hiding this comment

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

@swiftone I don't think this will affect the widget. Unlike the PKCE meta which is stored by auth flow initializer (SIW) and read by callback handler (SDK). This value is stored by the SDK before the redirect to SIW, and read by the SDK after the callback has been handled.

}

getFromUri() {
const referrerKey = 'secureRouterReferrerPath';
const location = localStorage.getItem(referrerKey) || window.location.origin;
localStorage.removeItem(referrerKey);
const location = sessionStorage.getItem(referrerKey) || window.location.origin;
sessionStorage.removeItem(referrerKey);
return location;
}

Expand Down
20 changes: 10 additions & 10 deletions packages/okta-react/test/jest/authService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,35 +430,35 @@ describe('AuthService', () => {
});

describe('setFromUri', () => {
it('Saves the fromUri in localStorage', () => {
localStorage.setItem('secureRouterReferrerPath', '');
expect(localStorage.getItem('secureRouterReferrerPath')).toBe('');
it('Saves the fromUri in sessionStorage', () => {
sessionStorage.setItem('secureRouterReferrerPath', '');
expect(sessionStorage.getItem('secureRouterReferrerPath')).toBe('');
const fromUri = 'http://localhost/foo/random';
const authService = new AuthService(validConfig);
authService.setFromUri(fromUri);
const val = localStorage.getItem('secureRouterReferrerPath');
const val = sessionStorage.getItem('secureRouterReferrerPath');
expect(val).toBe(fromUri);
});

it('Saves the window.location.href by default', () => {
localStorage.setItem('secureRouterReferrerPath', '');
expect(localStorage.getItem('secureRouterReferrerPath')).toBe('');
sessionStorage.setItem('secureRouterReferrerPath', '');
expect(sessionStorage.getItem('secureRouterReferrerPath')).toBe('');
const authService = new AuthService(validConfig);
authService.setFromUri();
const val = localStorage.getItem('secureRouterReferrerPath');
const val = sessionStorage.getItem('secureRouterReferrerPath');
expect(val).toBe(window.location.href);
});

});

describe('getFromUri', () => {
test('clears referrer from localStorage', () => {
test('clears referrer from sessionStorage', () => {
const TEST_VALUE = 'foo-bar';
localStorage.setItem('secureRouterReferrerPath', TEST_VALUE );
sessionStorage.setItem('secureRouterReferrerPath', TEST_VALUE );
const authService = new AuthService(validConfig);
const res = authService.getFromUri();
expect(res).toBe(TEST_VALUE);
expect(localStorage.getItem('referrerPath')).not.toBeTruthy();
expect(sessionStorage.getItem('referrerPath')).not.toBeTruthy();
});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/okta-react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@
version "0.4.1"
resolved "https://registry.yarnpkg.com/@okta/configuration-validation/-/configuration-validation-0.4.1.tgz#6fa4520bc96c27b3d7aedcb0523de1fbceee9105"

"@okta/okta-auth-js@^3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@okta/okta-auth-js/-/okta-auth-js-3.2.2.tgz#a6af150b76741ebe16d3541db182387172daf5f1"
integrity sha512-1pq1l8FVQsrtC4P2eSsWVByIODg4/93g+KJ0XWi6s4Pl6C52EZayvFkac8+FfOfy5CSnAaYQij7bD/V39AuROQ==
"@okta/okta-auth-js@^3.2.3":
version "3.2.3"
resolved "https://registry.yarnpkg.com/@okta/okta-auth-js/-/okta-auth-js-3.2.3.tgz#3bae9aa24eeac23b9d86504df346c514b62a6abf"
integrity sha512-lPKcITlHhfNhrGhnL8+zzlk86u2tZnXAahUPtiyEwFr+ktTpo8vWhraCR13hw0z46rTRVZ1lCMtedJt/wzMaoQ==
dependencies:
Base64 "0.3.0"
cross-fetch "^3.0.0"
Expand Down