Skip to content

Commit

Permalink
BREAKING CHANGE: removes 'useMultipleStorage' option
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredperreault-okta committed Jan 10, 2022
1 parent 9469d4a commit 3dad085
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/TokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class TokenManager implements TokenManagerInterface {
storageOptions.storageType = options.storage as StorageType;
}

this.storage = sdk.storageManager.getTokenStorage(storageOptions);
this.storage = sdk.storageManager.getTokenStorage({...storageOptions, useSeparateCookies: true});
this.clock = SdkClock.create(/* sdk, options */);
this.state = defaultState();

Expand Down
4 changes: 4 additions & 0 deletions lib/browser/browserStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ var storageUtil: BrowserStorageUtil = {
}
};

if (!options.useSeparateCookies) {
return storage;
}

// Tokens are stored separately because cookies have size limits.
// Can only be used when storing an object value. Object properties will be saved to separate cookies.
// Each property of the object must also be an object.
Expand Down
1 change: 1 addition & 0 deletions lib/types/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface StorageOptions extends CookieOptions {
storageTypes?: StorageType[];
storageProvider?: SimpleStorage;
storageKey?: string;
useSeparateCookies?: boolean;
}

export type StorageType = 'memory' | 'sessionStorage' | 'localStorage' | 'cookie' | 'custom' | 'auto';
Expand Down
1 change: 1 addition & 0 deletions test/spec/TokenManager/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ describe('TokenManager (browser)', function() {
storageManager: {
token: {
storageTypes: ['cookie'],
useSeparateCookies: true
}
}
});
Expand Down
83 changes: 54 additions & 29 deletions test/spec/browserStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ describe('browserStorage', () => {
});

describe('getCookieStorage', () => {

it('requires an options object', () => {
const fn = function() {
browserStorage.getCookieStorage();
Expand Down Expand Up @@ -144,37 +143,63 @@ describe('browserStorage', () => {
};
expect(fn).not.toThrow();
});

it('getItem: will call storage.get', () => {
const retVal = { fakeCookie: true };
jest.spyOn(browserStorage.storage, 'get').mockReturnValue(retVal);
const storage = browserStorage.getCookieStorage({ secure: true, sameSite: 'strict' });
const key = 'fake-key';
expect(storage.getItem(key)).toBe(retVal);
expect(browserStorage.storage.get).toHaveBeenCalledWith(key);
});

it('setItem: without sessionCookie set, it will call storage.set, passing secure, sameSite and infinite expiration date options', () => {
jest.spyOn(browserStorage.storage, 'set').mockReturnValue(null);
const storage = browserStorage.getCookieStorage({ secure: 'fakey', sameSite: 'strictly fakey' });
const key = 'fake-key';
const val = { fakeValue: true };
storage.setItem(key, val);
expect(browserStorage.storage.set).toHaveBeenCalledWith(key, val, '2200-01-01T00:00:00.000Z', {
secure: 'fakey',
sameSite: 'strictly fakey'

describe('useSeparateCookies: false', () => {
it('getItem: will call storage.get', () => {
const retVal = { fakeCookie: true };
jest.spyOn(browserStorage.storage, 'get').mockReturnValue(retVal);
const storage = browserStorage.getCookieStorage({ secure: true, sameSite: 'strict' });
const key = 'fake-key';
expect(storage.getItem(key)).toBe(retVal);
expect(browserStorage.storage.get).toHaveBeenCalledWith(key);
});

it('setItem: without sessionCookie set, it will call storage.set, passing secure, sameSite and infinite expiration date options', () => {
jest.spyOn(browserStorage.storage, 'set').mockReturnValue(null);
const storage = browserStorage.getCookieStorage({ secure: 'fakey', sameSite: 'strictly fakey' });
const key = 'fake-key';
const val = { fakeValue: true };
storage.setItem(key, val);
expect(browserStorage.storage.set).toHaveBeenCalledWith(key, val, '2200-01-01T00:00:00.000Z', {
secure: 'fakey',
sameSite: 'strictly fakey'
});
});

it('setItem: when sessionCookie is set, it will call storage.set, passing secure, sameSite and session-limited expiration date(null) options ', () => {
jest.spyOn(browserStorage.storage, 'set').mockReturnValue(null);
const storage = browserStorage.getCookieStorage({ secure: 'fakey', sameSite: 'strictly fakey', sessionCookie: true });
const key = 'fake-key';
const val = { fakeValue: true };
storage.setItem(key, val);
expect(browserStorage.storage.set).toHaveBeenCalledWith(key, val, null, {
secure: 'fakey',
sameSite: 'strictly fakey'
});
});
});

it('setItem: when sessionCookie is set, it will call storage.set, passing secure, sameSite and session-limited expiration date(null) options ', () => {
jest.spyOn(browserStorage.storage, 'set').mockReturnValue(null);
const storage = browserStorage.getCookieStorage({ secure: 'fakey', sameSite: 'strictly fakey', sessionCookie: true });
const key = 'fake-key';
const val = { fakeValue: true };
storage.setItem(key, val);
expect(browserStorage.storage.set).toHaveBeenCalledWith(key, val, null, {
secure: 'fakey',
sameSite: 'strictly fakey'
describe('useSeparateCookies: true', () => {
it('getItem: will use storage.get internally, but not directly', () => {
const retVal = { fakeCookie: true };
jest.spyOn(browserStorage.storage, 'get');
const storage = browserStorage.getCookieStorage({ secure: true, sameSite: 'strict', useSeparateCookies: true });
jest.spyOn(storage, 'getItem').mockReturnValue(retVal);
const key = 'fake-key';
expect(storage.getItem(key)).toBe(retVal);
expect(storage.getItem).toHaveBeenCalledWith(key);
expect(browserStorage.storage.get).not.toHaveBeenCalledWith(key);
});

it('setItem: will use storage.get and storage.set internally, but not directly', () => {
jest.spyOn(browserStorage.storage, 'set');
const storage = browserStorage.getCookieStorage({ secure: 'fakey', sameSite: 'strictly fakey' });
jest.spyOn(storage, 'setItem').mockReturnValue(null);
const key = 'fake-key';
const val = { fakeValue: true };
storage.setItem(key, val);
expect(storage.setItem).toHaveBeenCalledWith(key, val);
expect(browserStorage.storage.set).not.toHaveBeenCalled();
});
});
});
Expand Down

0 comments on commit 3dad085

Please sign in to comment.