15736: Fallback behavior to log in to remote clusters
[arvados-workbench2.git] / src / store / auth / auth-action-session.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
7 import { RootState } from "~/store/store";
8 import { ServiceRepository } from "~/services/services";
9 import Axios from "axios";
10 import { getUserFullname, User } from "~/models/user";
11 import { authActions } from "~/store/auth/auth-action";
12 import { Config, ClusterConfigJSON, CLUSTER_CONFIG_PATH, DISCOVERY_DOC_PATH, ARVADOS_API_PATH } from "~/common/config";
13 import { normalizeURLPath } from "~/common/url";
14 import { Session, SessionStatus } from "~/models/session";
15 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
16 import { AuthService, UserDetailsResponse } from "~/services/auth-service/auth-service";
17 import * as jsSHA from "jssha";
18
19 const getClusterInfo = async (origin: string): Promise<{ clusterId: string, baseUrl: string } | null> => {
20     // Try the new public config endpoint
21     try {
22         const config = (await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_PATH}`)).data;
23         return {
24             clusterId: config.ClusterID,
25             baseUrl: normalizeURLPath(`${config.Services.Controller.ExternalURL}/${ARVADOS_API_PATH}`)
26         };
27     } catch { }
28
29     // Fall back to discovery document
30     try {
31         const config = (await Axios.get<any>(`${origin}/${DISCOVERY_DOC_PATH}`)).data;
32         return {
33             clusterId: config.uuidPrefix,
34             baseUrl: normalizeURLPath(config.baseUrl)
35         };
36     } catch { }
37
38     return null;
39 };
40
41 interface RemoteHostInfo {
42     clusterId: string;
43     baseUrl: string;
44 }
45
46 const getRemoteHostInfo = async (remoteHost: string): Promise<RemoteHostInfo | null> => {
47     let url = remoteHost;
48     if (url.indexOf('://') < 0) {
49         url = 'https://' + url;
50     }
51     const origin = new URL(url).origin;
52
53     // Maybe it is an API server URL, try fetching config and discovery doc
54     let r = getClusterInfo(origin);
55     if (r !== null) {
56         return r;
57     }
58
59     // Maybe it is a Workbench2 URL, try getting config.json
60     try {
61         r = getClusterInfo((await Axios.get<any>(`${origin}/config.json`)).data.API_HOST);
62         if (r !== null) {
63             return r;
64         }
65     } catch { }
66
67     // Maybe it is a Workbench1 URL, try getting status.json
68     try {
69         r = getClusterInfo((await Axios.get<any>(`${origin}/status.json`)).data.apiBaseURL);
70         if (r !== null) {
71             return r;
72         }
73     } catch { }
74
75     return null;
76 };
77
78 const getUserDetails = async (baseUrl: string, token: string): Promise<UserDetailsResponse> => {
79     const resp = await Axios.get<UserDetailsResponse>(`${baseUrl}/users/current`, {
80         headers: {
81             Authorization: `OAuth2 ${token}`
82         }
83     });
84     return resp.data;
85 };
86
87 export const getSaltedToken = (clusterId: string, token: string) => {
88     const shaObj = new jsSHA("SHA-1", "TEXT");
89     const [ver, uuid, secret] = token.split("/");
90     if (ver !== "v2") {
91         throw new Error("Must be a v2 token");
92     }
93     let salted = secret;
94     if (uuid.substr(0, 5) !== clusterId) {
95         shaObj.setHMACKey(secret, "TEXT");
96         shaObj.update(clusterId);
97         salted = shaObj.getHMAC("HEX");
98     }
99     return `v2/${uuid}/${salted}`;
100 };
101
102 export const getActiveSession = (sessions: Session[]): Session | undefined => sessions.find(s => s.active);
103
104 export const validateCluster = async (info: RemoteHostInfo, useToken: string):
105     Promise<{ user: User; token: string }> => {
106
107     const saltedToken = getSaltedToken(info.clusterId, useToken);
108     const user = await getUserDetails(info.baseUrl, saltedToken);
109     return {
110         user: {
111             firstName: user.first_name,
112             lastName: user.last_name,
113             uuid: user.uuid,
114             ownerUuid: user.owner_uuid,
115             email: user.email,
116             isAdmin: user.is_admin,
117             isActive: user.is_active,
118             username: user.username,
119             prefs: user.prefs
120         },
121         token: saltedToken,
122     };
123 };
124
125 export const validateSession = (session: Session, activeSession: Session) =>
126     async (dispatch: Dispatch): Promise<Session> => {
127         dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED }));
128         session.loggedIn = false;
129
130         const setupSession = (baseUrl: string, user: User, token: string) => {
131             session.baseUrl = baseUrl;
132             session.token = token;
133             session.email = user.email;
134             session.uuid = user.uuid;
135             session.name = getUserFullname(user);
136             session.loggedIn = true;
137         };
138
139         const info = await getRemoteHostInfo(session.remoteHost);
140         if (!info) {
141             throw new Error(`Could not get config for ${session.remoteHost}`);
142         }
143         try {
144             const { user, token } = await validateCluster(info, session.token);
145             setupSession(info.baseUrl, user, token);
146         } catch {
147             try {
148                 const { user, token } = await validateCluster(info, activeSession.token);
149                 setupSession(info.baseUrl, user, token);
150             } catch { }
151         }
152
153         session.status = SessionStatus.VALIDATED;
154         dispatch(authActions.UPDATE_SESSION(session));
155
156         return session;
157     };
158
159 export const validateSessions = () =>
160     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
161         const sessions = getState().auth.sessions;
162         const activeSession = getActiveSession(sessions);
163         if (activeSession) {
164             dispatch(progressIndicatorActions.START_WORKING("sessionsValidation"));
165             for (const session of sessions) {
166                 if (session.status === SessionStatus.INVALIDATED) {
167                     await dispatch(validateSession(session, activeSession));
168                 }
169             }
170             services.authService.saveSessions(sessions);
171             dispatch(progressIndicatorActions.STOP_WORKING("sessionsValidation"));
172         }
173     };
174
175 export const addSession = (remoteHost: string, token?: string, sendToLogin?: boolean) =>
176     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
177         const sessions = getState().auth.sessions;
178         const activeSession = getActiveSession(sessions);
179         let useToken: string | null = null;
180         if (token) {
181             useToken = token;
182         } else if (activeSession) {
183             useToken = activeSession.token;
184         }
185
186         if (useToken) {
187             const info = await getRemoteHostInfo(remoteHost);
188             if (!info) {
189                 return Promise.reject(`Could not get config for ${remoteHost}`);
190             }
191
192             try {
193                 const { user, token } = await validateCluster(info, useToken);
194                 const session = {
195                     loggedIn: true,
196                     status: SessionStatus.VALIDATED,
197                     active: false,
198                     email: user.email,
199                     name: getUserFullname(user),
200                     uuid: user.uuid,
201                     baseUrl: info.baseUrl,
202                     clusterId: info.clusterId,
203                     remoteHost,
204                     token
205                 };
206
207                 if (sessions.find(s => s.clusterId === info.clusterId)) {
208                     dispatch(authActions.UPDATE_SESSION(session));
209                 } else {
210                     dispatch(authActions.ADD_SESSION(session));
211                 }
212                 services.authService.saveSessions(getState().auth.sessions);
213
214                 return session;
215             } catch {
216                 if (sendToLogin) {
217                     const rootUrl = new URL(info.baseUrl);
218                     rootUrl.pathname = "";
219                     window.location.href = `${rootUrl.toString()}/login?return_to=` + encodeURI(`${window.location.protocol}//${window.location.host}/add-session?baseURL=` + encodeURI(rootUrl.toString()));
220                     return;
221                 }
222             }
223         }
224         return Promise.reject("Could not validate cluster");
225     };
226
227 export const toggleSession = (session: Session) =>
228     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
229         let s = { ...session };
230
231         if (session.loggedIn) {
232             s.loggedIn = false;
233         } else {
234             const sessions = getState().auth.sessions;
235             const activeSession = getActiveSession(sessions);
236             if (activeSession) {
237                 s = await dispatch<any>(validateSession(s, activeSession)) as Session;
238             }
239         }
240
241         dispatch(authActions.UPDATE_SESSION(s));
242         services.authService.saveSessions(getState().auth.sessions);
243     };
244
245 export const initSessions = (authService: AuthService, config: Config, user: User) =>
246     (dispatch: Dispatch<any>) => {
247         const sessions = authService.buildSessions(config, user);
248         authService.saveSessions(sessions);
249         dispatch(authActions.SET_SESSIONS(sessions));
250         dispatch(validateSessions());
251     };
252
253 export const loadSiteManagerPanel = () =>
254     async (dispatch: Dispatch<any>) => {
255         try {
256             dispatch(setBreadcrumbs([{ label: 'Site Manager' }]));
257             dispatch(validateSessions());
258         } catch (e) {
259             return;
260         }
261     };