From becf93a5f9d1f7d96a3ce868c9b70c3d0094cf45 Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Tue, 23 Feb 2021 13:10:47 -0300 Subject: [PATCH] 16848: Only request an extra token when needed. Instead of preemptively asking for the extra token at session init, ask for one every time it's needed, avoiding creating unused tokens. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- src/store/auth/auth-action.ts | 17 +++++++++-------- .../collections/collection-info-actions.ts | 4 +++- .../main-app-bar/account-menu.tsx | 7 +++++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/store/auth/auth-action.ts b/src/store/auth/auth-action.ts index fb94746f..faf098f7 100644 --- a/src/store/auth/auth-action.ts +++ b/src/store/auth/auth-action.ts @@ -82,22 +82,23 @@ export const getConfig = (dispatch: Dispatch, getState: () => RootState, service return state.remoteHostsConfig[state.localCluster]; }; -export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise => { +export const saveApiToken = (token: string) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise => { const config = dispatch(getConfig); const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } }); setAuthorizationHeader(svc, token); - return svc.authService.getUserDetails().then((user: User) => { + try { + const user = await svc.authService.getUserDetails(); dispatch(authActions.INIT_USER({ user, token })); - // Upon user init, request an extra token that won't be expired on logout - // for other uses like the "get token" dialog, or S3 URL building. - dispatch(getNewExtraToken()); - }).catch(() => { + } catch (e) { dispatch(authActions.LOGOUT({ deleteLinkData: false })); - }); + } }; -export const getNewExtraToken = () => +export const getNewExtraToken = (reuseStored: boolean = false) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + if (reuseStored && getState().auth.extraApiToken !== undefined) { + return getState().auth.extraApiToken; + } const user = getState().auth.user; const loginCluster = getState().auth.config.clusterConfig.Login.LoginCluster; if (user === undefined) { return; } diff --git a/src/store/collections/collection-info-actions.ts b/src/store/collections/collection-info-actions.ts index b904da70..29dc6b87 100644 --- a/src/store/collections/collection-info-actions.ts +++ b/src/store/collections/collection-info-actions.ts @@ -6,6 +6,7 @@ import { Dispatch } from "redux"; import { RootState } from "~/store/store"; import { ServiceRepository } from "~/services/services"; import { dialogActions } from '~/store/dialog/dialog-actions'; +import { getNewExtraToken } from "../auth/auth-action"; export const COLLECTION_WEBDAV_S3_DIALOG_NAME = 'collectionWebdavS3Dialog'; @@ -21,7 +22,8 @@ export interface WebDavS3InfoDialogData { } export const openWebDavS3InfoDialog = (uuid: string, activeTab?: number) => - (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + await dispatch(getNewExtraToken(true)); dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_WEBDAV_S3_DIALOG_NAME, data: { diff --git a/src/views-components/main-app-bar/account-menu.tsx b/src/views-components/main-app-bar/account-menu.tsx index 58ed7b84..ea3a2dd9 100644 --- a/src/views-components/main-app-bar/account-menu.tsx +++ b/src/views-components/main-app-bar/account-menu.tsx @@ -9,7 +9,7 @@ import { User, getUserDisplayName } from "~/models/user"; import { DropdownMenu } from "~/components/dropdown-menu/dropdown-menu"; import { UserPanelIcon } from "~/components/icon/icon"; import { DispatchProp, connect } from 'react-redux'; -import { authActions } from '~/store/auth/auth-action'; +import { authActions, getNewExtraToken } from '~/store/auth/auth-action'; import { RootState } from "~/store/store"; import { openTokenDialog } from '~/store/token-dialog/token-dialog-actions'; import { openRepositoriesPanel } from "~/store/repositories/repositories-actions"; @@ -70,7 +70,10 @@ export const AccountMenuComponent = {user.isActive ? <> dispatch(openUserVirtualMachines())}>Virtual Machines {!user.isAdmin && dispatch(openRepositoriesPanel())}>Repositories} - dispatch(openTokenDialog)}>Get API token + { + dispatch(getNewExtraToken(true)); + dispatch(openTokenDialog); + }}>Get API token dispatch(navigateToSshKeysUser)}>Ssh Keys dispatch(navigateToSiteManager)}>Site Manager dispatch(navigateToMyAccount)}>My account -- 2.30.2