Merge branch '13708-process-form-runtime-constraints'
[arvados-workbench2.git] / src / store / auth / auth-action-session.ts
index 6ffca6b5220f459135d253c01963bf02bf48ebbc..d36d5a335c3b5d8f45f5eb7c1e6bfb14df4391d6 100644 (file)
@@ -12,7 +12,7 @@ import { authActions } from "~/store/auth/auth-action";
 import { Config, DISCOVERY_URL } from "~/common/config";
 import { Session, SessionStatus } from "~/models/session";
 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
-import { UserDetailsResponse } from "~/services/auth-service/auth-service";
+import { AuthService, UserDetailsResponse } from "~/services/auth-service/auth-service";
 import * as jsSHA from "jssha";
 
 const getRemoteHostBaseUrl = async (remoteHost: string): Promise<string | null> => {
@@ -80,11 +80,9 @@ const getSaltedToken = (clusterId: string, tokenUuid: string, token: string) =>
     return `v2/${tokenUuid}/${hmac}`;
 };
 
-const clusterLogin = async (clusterId: string, baseUrl: string, activeSession: Session): Promise<{user: User, token: string}> => {
+const clusterLogin = async (clusterId: string, baseUrl: string, activeSession: Session): Promise<{ user: User, token: string }> => {
     const tokenUuid = await getTokenUuid(activeSession.baseUrl, activeSession.token);
-    console.log(">> Cluster", clusterId);
     const saltedToken = getSaltedToken(clusterId, tokenUuid, activeSession.token);
-    console.log(">> Salted token", saltedToken);
     const user = await getUserDetails(baseUrl, saltedToken);
     return {
         user: {
@@ -93,7 +91,9 @@ const clusterLogin = async (clusterId: string, baseUrl: string, activeSession: S
             uuid: user.uuid,
             ownerUuid: user.owner_uuid,
             email: user.email,
-            isAdmin: user.is_admin
+            isAdmin: user.is_admin,
+            identityUrl: user.identity_url,
+            prefs: user.prefs
         },
         token: saltedToken
     };
@@ -111,7 +111,7 @@ export const validateCluster = async (remoteHost: string, clusterId: string, act
 };
 
 export const validateSession = (session: Session, activeSession: Session) =>
-    async (dispatch: Dispatch) => {
+    async (dispatch: Dispatch): Promise<Session> => {
         dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED }));
         session.loggedIn = false;
         try {
@@ -156,9 +156,9 @@ export const addSession = (remoteHost: string) =>
                 return Promise.reject("Cluster already exists");
             }
             try {
-                const { baseUrl, user, token } = await dispatch(validateCluster(remoteHost, clusterId, activeSession));
+                const { baseUrl, user, token } = await validateCluster(remoteHost, clusterId, activeSession);
                 const session = {
-                    loggedIn: false,
+                    loggedIn: true,
                     status: SessionStatus.VALIDATED,
                     active: false,
                     email: user.email,
@@ -174,17 +174,40 @@ export const addSession = (remoteHost: string) =>
 
                 return session;
             } catch (e) {
-                console.error(e);
             }
         }
-        debugger;
         return Promise.reject("Could not validate cluster");
     };
 
+export const toggleSession = (session: Session) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        let s = { ...session };
+
+        if (session.loggedIn) {
+            s.loggedIn = false;
+        } else {
+            const sessions = getState().auth.sessions;
+            const activeSession = getActiveSession(sessions);
+            if (activeSession) {
+                s = await dispatch<any>(validateSession(s, activeSession)) as Session;
+            }
+        }
+
+        dispatch(authActions.UPDATE_SESSION(s));
+        services.authService.saveSessions(getState().auth.sessions);
+    };
+
+export const initSessions = (authService: AuthService, config: Config, user: User) =>
+    (dispatch: Dispatch<any>) => {
+        const sessions = authService.buildSessions(config, user);
+        authService.saveSessions(sessions);
+        dispatch(authActions.SET_SESSIONS(sessions));
+    };
+
 export const loadSiteManagerPanel = () =>
     async (dispatch: Dispatch<any>) => {
         try {
-            dispatch(setBreadcrumbs([{ label: 'Site Manager'}]));
+            dispatch(setBreadcrumbs([{ label: 'Site Manager' }]));
             dispatch(validateSessions());
         } catch (e) {
             return;