Merge branch '18284-vm-listing' into main. Closes #18284
[arvados-workbench2.git] / src / store / auth / auth-action-session.ts
index 4568d77eafb572ff1e62eff77fc5fcea4ee7a9d9..7e81f2d952ebed3bf81353a4e6a6e2518a91044d 100644 (file)
@@ -3,30 +3,31 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { Dispatch } from "redux";
-import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
-import { RootState } from "~/store/store";
-import { ServiceRepository, createServices, setAuthorizationHeader } from "~/services/services";
-import Axios from "axios";
-import { User, getUserDisplayName } from "~/models/user";
-import { authActions } from "~/store/auth/auth-action";
+import { setBreadcrumbs } from "store/breadcrumbs/breadcrumbs-actions";
+import { RootState } from "store/store";
+import { ServiceRepository, createServices, setAuthorizationHeader } from "services/services";
+import Axios, { AxiosInstance } from "axios";
+import { User, getUserDisplayName } from "models/user";
+import { authActions } from "store/auth/auth-action";
 import {
     Config, ClusterConfigJSON, CLUSTER_CONFIG_PATH, DISCOVERY_DOC_PATH,
     buildConfig, mockClusterConfigJSON
-} from "~/common/config";
-import { normalizeURLPath } from "~/common/url";
-import { Session, SessionStatus } from "~/models/session";
-import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
-import { AuthService } from "~/services/auth-service/auth-service";
-import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
-import * as jsSHA from "jssha";
-
-const getClusterConfig = async (origin: string): Promise<Config | null> => {
+} from "common/config";
+import { normalizeURLPath } from "common/url";
+import { Session, SessionStatus } from "models/session";
+import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
+import { AuthService } from "services/auth-service/auth-service";
+import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
+import jsSHA from "jssha";
+
+const getClusterConfig = async (origin: string, apiClient: AxiosInstance): Promise<Config | null> => {
     let configFromDD: Config | undefined;
     try {
-        const dd = (await Axios.get<any>(`${origin}/${DISCOVERY_DOC_PATH}`)).data;
+        const dd = (await apiClient.get<any>(`${origin}/${DISCOVERY_DOC_PATH}`)).data;
         configFromDD = {
             baseUrl: normalizeURLPath(dd.baseUrl),
             keepWebServiceUrl: dd.keepWebServiceUrl,
+            keepWebInlineServiceUrl: dd.keepWebInlineServiceUrl,
             remoteHosts: dd.remoteHosts,
             rootUrl: dd.rootUrl,
             uuidPrefix: dd.uuidPrefix,
@@ -41,9 +42,9 @@ const getClusterConfig = async (origin: string): Promise<Config | null> => {
         };
     } catch { }
 
-    // Try the new public config endpoint
+    // Try public config endpoint
     try {
-        const config = (await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_PATH}`)).data;
+        const config = (await apiClient.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_PATH}`)).data;
         return { ...buildConfig(config), apiRevision: configFromDD ? configFromDD.apiRevision : 0 };
     } catch { }
 
@@ -55,7 +56,9 @@ const getClusterConfig = async (origin: string): Promise<Config | null> => {
     return null;
 };
 
-const getRemoteHostConfig = async (remoteHost: string): Promise<Config | null> => {
+export const getRemoteHostConfig = async (remoteHost: string, useApiClient?: AxiosInstance): Promise<Config | null> => {
+    const apiClient = useApiClient || Axios.create({ headers: {} });
+
     let url = remoteHost;
     if (url.indexOf('://') < 0) {
         url = 'https://' + url;
@@ -63,14 +66,14 @@ const getRemoteHostConfig = async (remoteHost: string): Promise<Config | null> =
     const origin = new URL(url).origin;
 
     // Maybe it is an API server URL, try fetching config and discovery doc
-    let r = await getClusterConfig(origin);
+    let r = await getClusterConfig(origin, apiClient);
     if (r !== null) {
         return r;
     }
 
     // Maybe it is a Workbench2 URL, try getting config.json
     try {
-        r = await getClusterConfig((await Axios.get<any>(`${origin}/config.json`)).data.API_HOST);
+        r = await getClusterConfig((await apiClient.get<any>(`${origin}/config.json`)).data.API_HOST, apiClient);
         if (r !== null) {
             return r;
         }
@@ -78,7 +81,7 @@ const getRemoteHostConfig = async (remoteHost: string): Promise<Config | null> =
 
     // Maybe it is a Workbench1 URL, try getting status.json
     try {
-        r = await getClusterConfig((await Axios.get<any>(`${origin}/status.json`)).data.apiBaseURL);
+        r = await getClusterConfig((await apiClient.get<any>(`${origin}/status.json`)).data.apiBaseURL, apiClient);
         if (r !== null) {
             return r;
         }
@@ -96,7 +99,7 @@ export const getSaltedToken = (clusterId: string, token: string) => {
         throw new Error(invalidV2Token);
     }
     let salted = secret;
-    if (uuid.substr(0, 5) !== clusterId) {
+    if (uuid.substring(0, 5) !== clusterId) {
         shaObj.setHMACKey(secret, "TEXT");
         shaObj.update(clusterId);
         salted = shaObj.getHMAC("HEX");
@@ -114,14 +117,14 @@ export const validateCluster = async (config: Config, useToken: string):
     const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
     setAuthorizationHeader(svc, saltedToken);
 
-    const user = await svc.authService.getUserDetails();
+    const user = await svc.authService.getUserDetails(false);
     return {
         user,
         token: saltedToken,
     };
 };
 
-export const validateSession = (session: Session, activeSession: Session) =>
+export const validateSession = (session: Session, activeSession: Session, useApiClient?: AxiosInstance) =>
     async (dispatch: Dispatch): Promise<Session> => {
         dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED }));
         session.loggedIn = false;
@@ -130,6 +133,7 @@ export const validateSession = (session: Session, activeSession: Session) =>
             session.baseUrl = baseUrl;
             session.token = token;
             session.email = user.email;
+            session.userIsActive = user.isActive;
             session.uuid = user.uuid;
             session.name = getUserDisplayName(user);
             session.loggedIn = true;
@@ -137,7 +141,7 @@ export const validateSession = (session: Session, activeSession: Session) =>
         };
 
         let fail: Error | null = null;
-        const config = await getRemoteHostConfig(session.remoteHost);
+        const config = await getRemoteHostConfig(session.remoteHost, useApiClient);
         if (config !== null) {
             dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config }));
             try {
@@ -168,7 +172,7 @@ export const validateSession = (session: Session, activeSession: Session) =>
         return session;
     };
 
-export const validateSessions = () =>
+export const validateSessions = (useApiClient?: AxiosInstance) =>
     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
         const sessions = getState().auth.sessions;
         const activeSession = getActiveSession(sessions);
@@ -186,12 +190,13 @@ export const validateSessions = () =>
                           override it using Dispatch<any>.  This
                           pattern is used in a bunch of different
                           places in Workbench2. */
-                        await dispatch(validateSession(session, activeSession));
+                        await dispatch(validateSession(session, activeSession, useApiClient));
                     } catch (e) {
-                        dispatch(snackbarActions.OPEN_SNACKBAR({
-                            message: e.message,
-                            kind: SnackbarKind.ERROR
-                        }));
+                        // Don't do anything here.  User may get
+                        // spammed with multiple messages that are not
+                        // helpful.  They can see the individual
+                        // errors by going to site manager and trying
+                        // to toggle the session.
                     }
                 }
             }
@@ -242,6 +247,7 @@ export const addSession = (remoteHost: string, token?: string, sendToLogin?: boo
                     status: SessionStatus.VALIDATED,
                     active: false,
                     email: user.email,
+                    userIsActive: user.isActive,
                     name: getUserDisplayName(user),
                     uuid: user.uuid,
                     baseUrl: config.baseUrl,
@@ -309,7 +315,7 @@ export const initSessions = (authService: AuthService, config: Config, user: Use
     (dispatch: Dispatch<any>) => {
         const sessions = authService.buildSessions(config, user);
         dispatch(authActions.SET_SESSIONS(sessions));
-        dispatch(validateSessions());
+        dispatch(validateSessions(authService.getApiClient()));
     };
 
 export const loadSiteManagerPanel = () =>