From e3b29a40d3ffb7624f2492a1f7d9943e73184ba7 Mon Sep 17 00:00:00 2001 From: Addison Beck Date: Fri, 11 Feb 2022 09:38:00 -0500 Subject: [PATCH] [bug] Also check for a never lock timeout when determining where to pull tokens (#673) --- common/src/services/state.service.ts | 60 +++++++++------------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/common/src/services/state.service.ts b/common/src/services/state.service.ts index 023e7c9bb9b..ac1d8d8d8a4 100644 --- a/common/src/services/state.service.ts +++ b/common/src/services/state.service.ts @@ -151,20 +151,12 @@ export class StateService< } async getAccessToken(options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); return (await this.getAccount(options))?.tokens?.accessToken; } async setAccessToken(value: string, options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); const account = await this.getAccount(options); account.tokens.accessToken = value; await this.saveAccount(account, options); @@ -202,40 +194,24 @@ export class StateService< } async getApiKeyClientId(options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); return (await this.getAccount(options))?.profile?.apiKeyClientId; } async setApiKeyClientId(value: string, options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); const account = await this.getAccount(options); account.profile.apiKeyClientId = value; await this.saveAccount(account, options); } async getApiKeyClientSecret(options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); return (await this.getAccount(options))?.keys?.apiKeyClientSecret; } async setApiKeyClientSecret(value: string, options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); const account = await this.getAccount(options); account.keys.apiKeyClientSecret = value; await this.saveAccount(account, options); @@ -1880,20 +1856,12 @@ export class StateService< } async getRefreshToken(options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); return (await this.getAccount(options))?.tokens?.refreshToken; } async setRefreshToken(value: string, options?: StorageOptions): Promise { - const defaultOptions = - (await this.getVaultTimeoutAction({ userId: options?.userId })) === "logOut" - ? this.defaultInMemoryOptions - : await this.defaultOnDiskOptions(); - options = this.reconcileOptions(options, defaultOptions); + options = await this.getTimeoutBasedStorageOptions(options); const account = await this.getAccount(options); account.tokens.refreshToken = value; await this.saveAccount(account, options); @@ -2272,7 +2240,7 @@ export class StateService< await this.storageService.remove(keys.tempAccountSettings); } account.settings.environmentUrls = environmentUrls; - if (account.settings.vaultTimeoutAction === "logOut") { + if (account.settings.vaultTimeoutAction === "logOut" && account.settings.vaultTimeout != null) { account.tokens.accessToken = null; account.tokens.refreshToken = null; account.profile.apiKeyClientId = null; @@ -2497,4 +2465,14 @@ export class StateService< await this.setActiveUser(null); } } + + private async getTimeoutBasedStorageOptions(options?: StorageOptions): Promise { + const timeoutAction = await this.getVaultTimeoutAction({ userId: options?.userId }); + const timeout = await this.getVaultTimeout({ userId: options?.userId }); + const defaultOptions = + timeoutAction === "logOut" && timeout != null + ? this.defaultInMemoryOptions + : await this.defaultOnDiskOptions(); + return this.reconcileOptions(options, defaultOptions); + } }