1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
7 import { RootState } from "~/store/store";
8 import { ServiceRepository, createServices, setAuthorizationHeader } from "~/services/services";
9 import Axios from "axios";
10 import { getUserFullname, User } from "~/models/user";
11 import { authActions } from "~/store/auth/auth-action";
13 Config, ClusterConfigJSON, CLUSTER_CONFIG_PATH, DISCOVERY_DOC_PATH,
14 buildConfig, mockClusterConfigJSON
15 } from "~/common/config";
16 import { normalizeURLPath } from "~/common/url";
17 import { Session, SessionStatus } from "~/models/session";
18 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
19 import { AuthService } from "~/services/auth-service/auth-service";
20 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
21 import * as jsSHA from "jssha";
23 const getClusterConfig = async (origin: string): Promise<Config | null> => {
24 // Try the new public config endpoint
26 const config = (await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_PATH}`)).data;
27 return buildConfig(config);
30 // Fall back to discovery document
32 const config = (await Axios.get<any>(`${origin}/${DISCOVERY_DOC_PATH}`)).data;
34 baseUrl: normalizeURLPath(config.baseUrl),
35 keepWebServiceUrl: config.keepWebServiceUrl,
36 remoteHosts: config.remoteHosts,
37 rootUrl: config.rootUrl,
38 uuidPrefix: config.uuidPrefix,
39 websocketUrl: config.websocketUrl,
40 workbenchUrl: config.workbenchUrl,
41 workbench2Url: config.workbench2Url,
44 fileViewersConfigUrl: "",
45 clusterConfig: mockClusterConfigJSON({})
52 const getRemoteHostConfig = async (remoteHost: string): Promise<Config | null> => {
54 if (url.indexOf('://') < 0) {
55 url = 'https://' + url;
57 const origin = new URL(url).origin;
59 // Maybe it is an API server URL, try fetching config and discovery doc
60 let r = await getClusterConfig(origin);
65 // Maybe it is a Workbench2 URL, try getting config.json
67 r = await getClusterConfig((await Axios.get<any>(`${origin}/config.json`)).data.API_HOST);
73 // Maybe it is a Workbench1 URL, try getting status.json
75 r = await getClusterConfig((await Axios.get<any>(`${origin}/status.json`)).data.apiBaseURL);
84 const invalidV2Token = "Must be a v2 token";
86 export const getSaltedToken = (clusterId: string, token: string) => {
87 const shaObj = new jsSHA("SHA-1", "TEXT");
88 const [ver, uuid, secret] = token.split("/");
90 throw new Error(invalidV2Token);
93 if (uuid.substr(0, 5) !== clusterId) {
94 shaObj.setHMACKey(secret, "TEXT");
95 shaObj.update(clusterId);
96 salted = shaObj.getHMAC("HEX");
98 return `v2/${uuid}/${salted}`;
101 export const getActiveSession = (sessions: Session[]): Session | undefined => sessions.find(s => s.active);
103 export const validateCluster = async (config: Config, useToken: string):
104 Promise<{ user: User; token: string }> => {
106 const saltedToken = getSaltedToken(config.uuidPrefix, useToken);
108 const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
109 setAuthorizationHeader(svc, saltedToken);
111 const user = await svc.authService.getUserDetails();
118 export const validateSession = (session: Session, activeSession: Session) =>
119 async (dispatch: Dispatch): Promise<Session> => {
120 dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED }));
121 session.loggedIn = false;
123 const setupSession = (baseUrl: string, user: User, token: string) => {
124 session.baseUrl = baseUrl;
125 session.token = token;
126 session.email = user.email;
127 session.uuid = user.uuid;
128 session.name = getUserFullname(user);
129 session.loggedIn = true;
132 let fail: Error | null = null;
133 const config = await getRemoteHostConfig(session.remoteHost);
134 if (config !== null) {
135 dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config }));
137 const { user, token } = await validateCluster(config, session.token);
138 setupSession(config.baseUrl, user, token);
140 fail = new Error(`Getting current user for ${session.remoteHost}: ${e.message}`);
142 const { user, token } = await validateCluster(config, activeSession.token);
143 setupSession(config.baseUrl, user, token);
146 if (e.message === invalidV2Token) {
147 fail = new Error(`Getting current user for ${session.remoteHost}: ${e2.message}`);
152 fail = new Error(`Could not get config for ${session.remoteHost}`);
154 session.status = SessionStatus.VALIDATED;
155 dispatch(authActions.UPDATE_SESSION(session));
164 export const validateSessions = () =>
165 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
166 const sessions = getState().auth.sessions;
167 const activeSession = getActiveSession(sessions);
169 dispatch(progressIndicatorActions.START_WORKING("sessionsValidation"));
170 for (const session of sessions) {
171 if (session.status === SessionStatus.INVALIDATED) {
173 /* Here we are dispatching a function, not an
174 action. This is legal (it calls the
175 function with a 'Dispatch' object as the
176 first parameter) but the typescript
177 annotations don't understand this case, so
178 we get an error from typescript unless
179 override it using Dispatch<any>. This
180 pattern is used in a bunch of different
181 places in Workbench2. */
182 await dispatch(validateSession(session, activeSession));
184 dispatch(snackbarActions.OPEN_SNACKBAR({
186 kind: SnackbarKind.ERROR
191 services.authService.saveSessions(getState().auth.sessions);
192 dispatch(progressIndicatorActions.STOP_WORKING("sessionsValidation"));
196 export const addSession = (remoteHost: string, token?: string, sendToLogin?: boolean) =>
197 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
198 const sessions = getState().auth.sessions;
199 const activeSession = getActiveSession(sessions);
200 let useToken: string | null = null;
203 } else if (activeSession) {
204 useToken = activeSession.token;
208 const config = await getRemoteHostConfig(remoteHost);
210 dispatch(snackbarActions.OPEN_SNACKBAR({
211 message: `Could not get config for ${remoteHost}`,
212 kind: SnackbarKind.ERROR
218 dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config }));
219 const { user, token } = await validateCluster(config, useToken);
222 status: SessionStatus.VALIDATED,
225 name: getUserFullname(user),
227 baseUrl: config.baseUrl,
228 clusterId: config.uuidPrefix,
233 if (sessions.find(s => s.clusterId === config.uuidPrefix)) {
234 await dispatch(authActions.UPDATE_SESSION(session));
236 await dispatch(authActions.ADD_SESSION(session));
238 services.authService.saveSessions(getState().auth.sessions);
243 const rootUrl = new URL(config.baseUrl);
244 rootUrl.pathname = "";
245 window.location.href = `${rootUrl.toString()}/login?return_to=` + encodeURI(`${window.location.protocol}//${window.location.host}/add-session?baseURL=` + encodeURI(rootUrl.toString()));
250 return Promise.reject(new Error("Could not validate cluster"));
254 export const removeSession = (clusterId: string) =>
255 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
256 await dispatch(authActions.REMOVE_SESSION(clusterId));
257 services.authService.saveSessions(getState().auth.sessions);
260 export const toggleSession = (session: Session) =>
261 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
262 const s: Session = { ...session };
264 if (session.loggedIn) {
266 dispatch(authActions.UPDATE_SESSION(s));
268 const sessions = getState().auth.sessions;
269 const activeSession = getActiveSession(sessions);
272 await dispatch(validateSession(s, activeSession));
274 dispatch(snackbarActions.OPEN_SNACKBAR({
276 kind: SnackbarKind.ERROR
279 dispatch(authActions.UPDATE_SESSION(s));
284 services.authService.saveSessions(getState().auth.sessions);
287 export const initSessions = (authService: AuthService, config: Config, user: User) =>
288 (dispatch: Dispatch<any>) => {
289 const sessions = authService.buildSessions(config, user);
290 dispatch(authActions.SET_SESSIONS(sessions));
291 dispatch(validateSessions());
294 export const loadSiteManagerPanel = () =>
295 async (dispatch: Dispatch<any>) => {
297 dispatch(setBreadcrumbs([{ label: 'Site Manager' }]));
298 dispatch(validateSessions());