Merge branch '15530-wb2-logincluster' refs #15530
[arvados-workbench2.git] / src / services / auth-service / auth-service.ts
index eae219dd0ad2a547883b94047a961d10048f5019..da96f1629b2fc9e3cf76ba91c3215df43bd17666 100644 (file)
@@ -20,6 +20,7 @@ export const USER_IS_ADMIN = 'isAdmin';
 export const USER_IS_ACTIVE = 'isActive';
 export const USER_USERNAME = 'username';
 export const USER_PREFS = 'prefs';
+export const HOME_CLUSTER = 'homeCluster';
 
 export interface UserDetailsResponse {
     email: string;
@@ -42,6 +43,7 @@ export class AuthService {
 
     public saveApiToken(token: string) {
         localStorage.setItem(API_TOKEN_KEY, token);
+        localStorage.setItem(HOME_CLUSTER, token.split('/')[1].substr(0, 5));
     }
 
     public removeApiToken() {
@@ -52,6 +54,10 @@ export class AuthService {
         return localStorage.getItem(API_TOKEN_KEY) || undefined;
     }
 
+    public getHomeCluster() {
+        return localStorage.getItem(HOME_CLUSTER) || undefined;
+    }
+
     public getUuid() {
         return localStorage.getItem(USER_UUID_KEY) || undefined;
     }
@@ -108,9 +114,10 @@ export class AuthService {
         localStorage.removeItem(USER_PREFS);
     }
 
-    public login(uuidPrefix: string, homeCluster: string) {
+    public login(uuidPrefix: string, homeCluster: string, loginCluster: string, remoteHosts: { [key: string]: string }) {
         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
-        window.location.assign(`https://${homeCluster}/login?remote=${uuidPrefix}&return_to=${currentUrl}`);
+        const homeClusterHost = remoteHosts[homeCluster];
+        window.location.assign(`https://${homeClusterHost}/login?${(uuidPrefix !== homeCluster && homeCluster !== loginCluster) ? "remote=" + uuidPrefix + "&" : ""}return_to=${currentUrl}`);
     }
 
     public logout() {
@@ -176,7 +183,12 @@ export class AuthService {
             active: true,
             status: SessionStatus.VALIDATED
         } as Session;
-        const localSessions = this.getSessions();
+        const localSessions = this.getSessions().map(s => ({
+            ...s,
+            active: false,
+            status: SessionStatus.INVALIDATED
+        }));
+
         const cfgSessions = Object.keys(cfg.remoteHosts).map(clusterId => {
             const remoteHost = cfg.remoteHosts[clusterId];
             return {
@@ -192,8 +204,9 @@ export class AuthService {
             } as Session;
         });
         const sessions = [currentSession]
+            .concat(cfgSessions)
             .concat(localSessions)
-            .concat(cfgSessions);
+            .filter((r: Session) => r.clusterId !== "*");
 
         const uniqSessions = uniqBy(sessions, 'clusterId');