14813: wb2 uses cluster config
[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_URL, ARVADOS_API_PATH } from "~/common/config";
13 import { Session, SessionStatus } from "~/models/session";
14 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
15 import { AuthService, UserDetailsResponse } from "~/services/auth-service/auth-service";
16 import * as jsSHA from "jssha";
17
18 const getRemoteHostBaseUrl = async (remoteHost: string): Promise<string | null> => {
19     let url = remoteHost;
20     if (url.indexOf('://') < 0) {
21         url = 'https://' + url;
22     }
23     const origin = new URL(url).origin;
24     let baseUrl: string | null = null;
25
26     try {
27         const resp = await Axios.get<ClusterConfigJSON>(`${origin}/${CLUSTER_CONFIG_URL}`);
28         baseUrl = `${resp.data.Services.Controller.ExternalURL}/${ARVADOS_API_PATH}`;
29     } catch (err) {
30         try {
31             const resp = await Axios.get<any>(`${origin}/status.json`);
32             baseUrl = resp.data.apiBaseURL;
33         } catch (err) {
34         }
35     }
36
37     if (baseUrl && baseUrl[baseUrl.length - 1] === '/') {
38         baseUrl = baseUrl.substr(0, baseUrl.length - 1);
39     }
40
41     return baseUrl;
42 };
43
44 const getUserDetails = async (baseUrl: string, token: string): Promise<UserDetailsResponse> => {
45     const resp = await Axios.get<UserDetailsResponse>(`${baseUrl}/users/current`, {
46         headers: {
47             Authorization: `OAuth2 ${token}`
48         }
49     });
50     return resp.data;
51 };
52
53 const getTokenUuid = async (baseUrl: string, token: string): Promise<string> => {
54     if (token.startsWith("v2/")) {
55         const uuid = token.split("/")[1];
56         return Promise.resolve(uuid);
57     }
58
59     const resp = await Axios.get(`${baseUrl}api_client_authorizations`, {
60         headers: {
61             Authorization: `OAuth2 ${token}`
62         },
63         data: {
64             filters: JSON.stringify([['api_token', '=', token]])
65         }
66     });
67
68     return resp.data.items[0].uuid;
69 };
70
71 export const getSaltedToken = (clusterId: string, tokenUuid: string, token: string) => {
72     const shaObj = new jsSHA("SHA-1", "TEXT");
73     let secret = token;
74     if (token.startsWith("v2/")) {
75         secret = token.split("/")[2];
76     }
77     shaObj.setHMACKey(secret, "TEXT");
78     shaObj.update(clusterId);
79     const hmac = shaObj.getHMAC("HEX");
80     return `v2/${tokenUuid}/${hmac}`;
81 };
82
83 const clusterLogin = async (clusterId: string, baseUrl: string, activeSession: Session): Promise<{ user: User, token: string }> => {
84     const tokenUuid = await getTokenUuid(activeSession.baseUrl, activeSession.token);
85     const saltedToken = getSaltedToken(clusterId, tokenUuid, activeSession.token);
86     const user = await getUserDetails(baseUrl, saltedToken);
87     return {
88         user: {
89             firstName: user.first_name,
90             lastName: user.last_name,
91             uuid: user.uuid,
92             ownerUuid: user.owner_uuid,
93             email: user.email,
94             isAdmin: user.is_admin,
95             isActive: user.is_active,
96             username: user.username,
97             prefs: user.prefs
98         },
99         token: saltedToken
100     };
101 };
102
103 export const getActiveSession = (sessions: Session[]): Session | undefined => sessions.find(s => s.active);
104
105 export const validateCluster = async (remoteHost: string, clusterId: string, activeSession: Session): Promise<{ user: User; token: string, baseUrl: string }> => {
106     const baseUrl = await getRemoteHostBaseUrl(remoteHost);
107     if (!baseUrl) {
108         return Promise.reject(`Could not find base url for ${remoteHost}`);
109     }
110     const { user, token } = await clusterLogin(clusterId, baseUrl, activeSession);
111     return { baseUrl, user, token };
112 };
113
114 export const validateSession = (session: Session, activeSession: Session) =>
115     async (dispatch: Dispatch): Promise<Session> => {
116         dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED }));
117         session.loggedIn = false;
118         try {
119             const { baseUrl, user, token } = await validateCluster(session.remoteHost, session.clusterId, activeSession);
120             session.baseUrl = baseUrl;
121             session.token = token;
122             session.email = user.email;
123             session.username = getUserFullname(user);
124             session.loggedIn = true;
125         } catch {
126             session.loggedIn = false;
127         } finally {
128             session.status = SessionStatus.VALIDATED;
129             dispatch(authActions.UPDATE_SESSION(session));
130         }
131         return session;
132     };
133
134 export const validateSessions = () =>
135     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
136         const sessions = getState().auth.sessions;
137         const activeSession = getActiveSession(sessions);
138         if (activeSession) {
139             dispatch(progressIndicatorActions.START_WORKING("sessionsValidation"));
140             for (const session of sessions) {
141                 if (session.status === SessionStatus.INVALIDATED) {
142                     await dispatch(validateSession(session, activeSession));
143                 }
144             }
145             services.authService.saveSessions(sessions);
146             dispatch(progressIndicatorActions.STOP_WORKING("sessionsValidation"));
147         }
148     };
149
150 export const addSession = (remoteHost: string) =>
151     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
152         const sessions = getState().auth.sessions;
153         const activeSession = getActiveSession(sessions);
154         if (activeSession) {
155             const clusterId = remoteHost.match(/^(\w+)\./)![1];
156             if (sessions.find(s => s.clusterId === clusterId)) {
157                 return Promise.reject("Cluster already exists");
158             }
159             try {
160                 const { baseUrl, user, token } = await validateCluster(remoteHost, clusterId, activeSession);
161                 const session = {
162                     loggedIn: true,
163                     status: SessionStatus.VALIDATED,
164                     active: false,
165                     email: user.email,
166                     username: getUserFullname(user),
167                     remoteHost,
168                     baseUrl,
169                     clusterId,
170                     token
171                 };
172
173                 dispatch(authActions.ADD_SESSION(session));
174                 services.authService.saveSessions(getState().auth.sessions);
175
176                 return session;
177             } catch (e) {
178             }
179         }
180         return Promise.reject("Could not validate cluster");
181     };
182
183 export const toggleSession = (session: Session) =>
184     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
185         let s = { ...session };
186
187         if (session.loggedIn) {
188             s.loggedIn = false;
189         } else {
190             const sessions = getState().auth.sessions;
191             const activeSession = getActiveSession(sessions);
192             if (activeSession) {
193                 s = await dispatch<any>(validateSession(s, activeSession)) as Session;
194             }
195         }
196
197         dispatch(authActions.UPDATE_SESSION(s));
198         services.authService.saveSessions(getState().auth.sessions);
199     };
200
201 export const initSessions = (authService: AuthService, config: Config, user: User) =>
202     (dispatch: Dispatch<any>) => {
203         const sessions = authService.buildSessions(config, user);
204         authService.saveSessions(sessions);
205         dispatch(authActions.SET_SESSIONS(sessions));
206     };
207
208 export const loadSiteManagerPanel = () =>
209     async (dispatch: Dispatch<any>) => {
210         try {
211             dispatch(setBreadcrumbs([{ label: 'Site Manager' }]));
212             dispatch(validateSessions());
213         } catch (e) {
214             return;
215         }
216     };