Skip to content

Commit

Permalink
use storageManager configuration if tokenManager.storage is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandrpravosudko-okta committed Apr 21, 2021
1 parent 105ffe6 commit e0ae3fa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ const storageProvider = {
}

var config = {
storage: {
storageManager: {
token: {
storageProvider: storageProvider
}
Expand Down
4 changes: 3 additions & 1 deletion lib/TokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ export class TokenManager {
_emitEventsForCrossTabsStorageUpdate: (newValue: string, oldValue: string) => void;

constructor(sdk: OktaAuth, options: TokenManagerOptions) {
options = Object.assign({}, DEFAULT_OPTIONS, removeNils(options));
const { storageType, storageTypes } = sdk.storageManager.getOptionsForSection('token');
const storageManagerStorage = storageType || storageTypes && storageTypes[0];
options = Object.assign({}, DEFAULT_OPTIONS, removeNils({storage: storageManagerStorage}), removeNils(options));

const emitter = (sdk as any).emitter;
if (!emitter) {
Expand Down
4 changes: 3 additions & 1 deletion lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class OktaAuthBrowser extends OktaAuthBase implements OktaAuth, SignoutAPI {
httpRequestClient: fetchRequest,
storageUtil: args.storageUtil || browserStorage,
cookies: getCookieSettings(args, OktaAuthBrowser.features.isHTTPS()),
}, {
...args,
storageManager: Object.assign({
token: {
storageTypes: [
Expand All @@ -154,7 +156,7 @@ class OktaAuthBrowser extends OktaAuthBase implements OktaAuth, SignoutAPI {
]
}
}, args.storageManager)
}, args));
}));

// Add shims for compatibility, these will be removed in next major version. OKTA-362589
Object.assign(this.options.storageUtil, {
Expand Down
47 changes: 42 additions & 5 deletions test/spec/tokenManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ var secureCookieSettings = {
};

function createAuth(options) {
options = options || {};
options.tokenManager = options.tokenManager || {};
options = Object.assign({}, options);
options.tokenManager = Object.assign({}, options.tokenManager);
options.cookies = Object.assign({}, options.cookies);
options.storageManager = Object.assign({}, options.storageManager);
jest.spyOn(SdkClock, 'create').mockReturnValue(new SdkClock(options.localClockOffset));
return new OktaAuth({
cookies: options.cookies || {},
cookies: options.cookies,
pkce: false,
issuer: 'https://auth-js-test.okta.com',
clientId: 'NPSfOkH5eZrTy8PMDlvx',
redirectUri: 'https://example.com/redirect',
storageManager: options.storageManager,
tokenManager: {
expireEarlySeconds: options.tokenManager.expireEarlySeconds || 0,
storage: options.tokenManager.storage,
Expand Down Expand Up @@ -140,7 +143,6 @@ describe('TokenManager', function() {
},
tokenManager: {
storage: 'cookie',
sessionCookie: true
}
});
var setCookieMock = util.mockSetCookie();
Expand Down Expand Up @@ -229,6 +231,40 @@ describe('TokenManager', function() {
'test-idToken': tokens.standardIdTokenParsed
});
});
it('honors StorageManager configuration', function () {
setupSync({
storageManager: {
token: {
storageTypes: ['cookie'],
useMultipleCookies: true,
}
}
});
var setCookieMock = util.mockSetCookie();
client.tokenManager.add('test-idToken', tokens.standardIdTokenParsed);
expect(setCookieMock).toHaveBeenCalledWith(
'okta-token-storage_test-idToken',
JSON.stringify(tokens.standardIdTokenParsed),
'2200-01-01T00:00:00.000Z',
secureCookieSettings
);
});
it('favors token.storage configuration over storageManager.token', function () {
setupSync({
storageManager: {
token: {
storageTypes: ['cookie'],
}
},
tokenManager: {
storage: 'sessionStorage',
}
});
client.tokenManager.add('test-idToken', tokens.standardIdTokenParsed);
oauthUtil.expectTokenStorageToEqual(sessionStorage, {
'test-idToken': tokens.standardIdTokenParsed
});
});
it('defaults to sessionStorage if localStorage isn\'t available', function() {
jest.spyOn(window.console, 'warn');
oauthUtil.mockLocalStorageError();
Expand Down Expand Up @@ -1665,7 +1701,8 @@ describe('TokenManager', function() {
sdkMock = {
options: {},
storageManager: {
getTokenStorage: jest.fn()
getTokenStorage: jest.fn(),
getOptionsForSection: jest.fn().mockReturnValue({}),
},
emitter
};
Expand Down

0 comments on commit e0ae3fa

Please sign in to comment.