16848: Checks cached token validity before using it.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 8 Mar 2021 15:22:06 +0000 (12:22 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 8 Mar 2021 15:22:06 +0000 (12:22 -0300)
The cached token could be invalid or have its expiration date changed from an
API call outside Workbench2.

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/store/auth/auth-action.ts

index 2819364fb1958925c88335a153a9d8238444ff64..27558618d8017b17465e06ea388f03240a77b249 100644 (file)
@@ -98,8 +98,21 @@ export const saveApiToken = (token: string) => async (dispatch: Dispatch, getSta
 
 export const getNewExtraToken = (reuseStored: boolean = false) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        if (reuseStored && getState().auth.extraApiToken !== undefined) {
-            return getState().auth.extraApiToken;
+        const extraToken = getState().auth.extraApiToken;
+        if (reuseStored && extraToken !== undefined) {
+            const config = dispatch<any>(getConfig);
+            const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
+            setAuthorizationHeader(svc, extraToken);
+            try {
+                // Check the extra token's validity before using it. Refresh its
+                // expiration date just in case it changed.
+                const client = await svc.apiClientAuthorizationService.get('current');
+                dispatch(authActions.SET_EXTRA_TOKEN({
+                    extraApiToken: extraToken,
+                    extraApiTokenExpiration: client.expiresAt ? new Date(client.expiresAt): undefined,
+                }));
+                return extraToken;
+            } catch (e) { }
         }
         const user = getState().auth.user;
         const loginCluster = getState().auth.config.clusterConfig.Login.LoginCluster;