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

fix: Fixes an issue where default storage was read-only #172

Merged
merged 3 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions lib/storageUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var storageUtil = {};
// https://connect.microsoft.com/IE/Feedback/Details/1496040
storageUtil.browserHasLocalStorage = function() {
try {
if (storageUtil.getLocalStorage()) {
var storage = storageUtil.getLocalStorage();
if (storageUtil.testStorage(storage)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: return storageUtil.testStorage(storage)

return true;
} else {
return false;
Expand All @@ -34,7 +35,8 @@ storageUtil.browserHasLocalStorage = function() {

storageUtil.browserHasSessionStorage = function() {
try {
if (storageUtil.getSessionStorage()) {
var storage = storageUtil.getSessionStorage();
if (storageUtil.testStorage(storage)) {
return true;
} else {
return false;
Expand Down Expand Up @@ -73,4 +75,15 @@ storageUtil.getCookieStorage = function() {
};
};

storageUtil.testStorage = function(storage) {
Copy link
Contributor

Choose a reason for hiding this comment

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

wonder if it's worth to check null-able of storage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that as well. Attempting to call setItem on undefined or null will throw a TypeError, which will result in returning false. I think we should be safe thanks to wrapping this in a try catch

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