Skip to content

Commit

Permalink
fix: Fixes an issue where default storage was read-only (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelberg-okta authored Nov 7, 2018
1 parent 6744e18 commit c384006
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/storageUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ 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;
}
};

storageUtil.browserHasSessionStorage = function() {
try {
if (storageUtil.getSessionStorage()) {
return true;
} else {
return false;
}
var storage = storageUtil.getSessionStorage();
return storageUtil.testStorage(storage);
} catch (e) {
return false;
}
Expand Down Expand Up @@ -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;
34 changes: 34 additions & 0 deletions test/spec/tokenManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions test/util/oauthUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down

0 comments on commit c384006

Please sign in to comment.