diff --git a/lib/storageUtil.js b/lib/storageUtil.js index 3eee94458..bf9573999 100644 --- a/lib/storageUtil.js +++ b/lib/storageUtil.js @@ -22,11 +22,8 @@ var storageUtil = {}; // https://connect.microsoft.com/IE/Feedback/Details/1496040 storageUtil.browserHasLocalStorage = function() { try { - if (storageUtil.getLocalStorage()) { - return true; - } else { - return false; - } + var storage = storageUtil.getLocalStorage(); + return storageUtil.testStorage(storage); } catch (e) { return false; } @@ -34,11 +31,8 @@ storageUtil.browserHasLocalStorage = function() { storageUtil.browserHasSessionStorage = function() { try { - if (storageUtil.getSessionStorage()) { - return true; - } else { - return false; - } + var storage = storageUtil.getSessionStorage(); + return storageUtil.testStorage(storage); } catch (e) { return false; } @@ -73,4 +67,15 @@ storageUtil.getCookieStorage = function() { }; }; +storageUtil.testStorage = function(storage) { + var key = 'okta-test-storage'; + try { + storage.setItem(key, key); + storage.removeItem(key); + return true; + } catch (e) { + return false; + } +}; + module.exports = storageUtil; diff --git a/test/spec/tokenManager.js b/test/spec/tokenManager.js index dbf336f1a..7413d33f4 100644 --- a/test/spec/tokenManager.js +++ b/test/spec/tokenManager.js @@ -44,6 +44,19 @@ describe('TokenManager', function() { 'test-idToken': tokens.standardIdTokenParsed }); }); + it('defaults to sessionStorage if localStorage cannot be written to', function() { + jest.spyOn(window.console, 'log'); + oauthUtil.mockStorageSetItemError(); + var client = setupSync(); + expect(window.console.log).toHaveBeenCalledWith( + '[okta-auth-sdk] WARN: This browser doesn\'t ' + + 'support localStorage. Switching to sessionStorage.' + ); + client.tokenManager.add('test-idToken', tokens.standardIdTokenParsed); + oauthUtil.expectTokenStorageToEqual(sessionStorage, { + 'test-idToken': tokens.standardIdTokenParsed + }); + }); it('defaults to cookie-based storage if localStorage and sessionStorage are not available', function() { jest.spyOn(window.console, 'log'); oauthUtil.mockLocalStorageError(); @@ -61,6 +74,27 @@ describe('TokenManager', function() { '2038-01-19T03:14:07.000Z' ); }); + it('defaults to cookie-based storage if sessionStorage cannot be written to', function() { + jest.spyOn(window.console, 'log'); + oauthUtil.mockLocalStorageError(); + oauthUtil.mockStorageSetItemError(); + var client = setupSync({ + tokenManager: { + storage: 'sessionStorage' + } + }); + expect(window.console.log).toHaveBeenCalledWith( + '[okta-auth-sdk] WARN: This browser doesn\'t ' + + 'support sessionStorage. Switching to cookie-based storage.' + ); + var setCookieMock = util.mockSetCookie(); + client.tokenManager.add('test-idToken', tokens.standardIdTokenParsed); + expect(setCookieMock).toHaveBeenCalledWith( + 'okta-token-storage', + JSON.stringify({'test-idToken': tokens.standardIdTokenParsed}), + '2038-01-19T03:14:07.000Z' + ); + }); }); describe('add', function() { diff --git a/test/util/oauthUtil.js b/test/util/oauthUtil.js index 6deae95d3..72c6392c1 100644 --- a/test/util/oauthUtil.js +++ b/test/util/oauthUtil.js @@ -29,6 +29,12 @@ oauthUtil.mockLocalStorageError = function() { }); }; +oauthUtil.mockStorageSetItemError = function() { + jest.spyOn(Storage.prototype, 'setItem').mockImplementationOnce(function() { + throw 'This function is not supported on this system.'; + }); +}; + oauthUtil.mockSessionStorageError = function() { jest.spyOn(storageUtil, 'getSessionStorage').mockImplementation(function() { throw 'This function is not supported on this system.';