15064: If initializing auth fails, go back to login screen.
[arvados-workbench2.git] / src / store / auth / auth-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ofType, unionize, UnionOf } from '~/common/unionize';
6 import { Dispatch } from "redux";
7 import { AxiosInstance } from "axios";
8 import { RootState } from "../store";
9 import { ServiceRepository } from "~/services/services";
10 import { SshKeyResource } from '~/models/ssh-key';
11 import { User } from "~/models/user";
12 import { Session } from "~/models/session";
13 import { getDiscoveryURL, Config } from '~/common/config';
14 import { initSessions } from "~/store/auth/auth-action-session";
15 import Axios from "axios";
16
17 export const authActions = unionize({
18     SAVE_API_TOKEN: ofType<string>(),
19     LOGIN: {},
20     LOGOUT: {},
21     CONFIG: ofType<{ config: Config }>(),
22     INIT: ofType<{ user: User, token: string }>(),
23     USER_DETAILS_REQUEST: {},
24     USER_DETAILS_SUCCESS: ofType<User>(),
25     SET_SSH_KEYS: ofType<SshKeyResource[]>(),
26     ADD_SSH_KEY: ofType<SshKeyResource>(),
27     REMOVE_SSH_KEY: ofType<string>(),
28     SET_HOME_CLUSTER: ofType<string>(),
29     SET_SESSIONS: ofType<Session[]>(),
30     ADD_SESSION: ofType<Session>(),
31     REMOVE_SESSION: ofType<string>(),
32     UPDATE_SESSION: ofType<Session>(),
33     REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
34 });
35
36 function setAuthorizationHeader(services: ServiceRepository, token: string) {
37     services.apiClient.defaults.headers.common = {
38         Authorization: `OAuth2 ${token}`
39     };
40     services.webdavClient.defaults.headers = {
41         Authorization: `OAuth2 ${token}`
42     };
43 }
44
45 function removeAuthorizationHeader(client: AxiosInstance) {
46     delete client.defaults.headers.common.Authorization;
47 }
48
49 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50     const user = services.authService.getUser();
51     const token = services.authService.getApiToken();
52     if (token) {
53         setAuthorizationHeader(services, token);
54     }
55     dispatch(authActions.CONFIG({ config }));
56     if (token && user) {
57         dispatch(authActions.INIT({ user, token }));
58         dispatch<any>(initSessions(services.authService, config, user));
59         dispatch<any>(getUserDetails()).then((user: User) => {
60             dispatch(authActions.INIT({ user, token }));
61         }).catch(() => {
62             logout()(dispatch, getState, services);
63         });
64     }
65     Object.keys(config.remoteHosts).map((k) => {
66         Axios.get<Config>(getDiscoveryURL(config.remoteHosts[k]))
67             .then(response => dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: response.data })));
68     });
69 };
70
71 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72     services.authService.saveApiToken(token);
73     setAuthorizationHeader(services, token);
74     dispatch(authActions.SAVE_API_TOKEN(token));
75 };
76
77 export const login = (uuidPrefix: string, homeCluster: string, remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78     services.authService.login(uuidPrefix, homeCluster, remoteHosts);
79     dispatch(authActions.LOGIN());
80 };
81
82 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
83     services.authService.removeApiToken();
84     services.authService.removeUser();
85     removeAuthorizationHeader(services.apiClient);
86     services.authService.logout();
87     dispatch(authActions.LOGOUT());
88 };
89
90 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
91     dispatch(authActions.USER_DETAILS_REQUEST());
92     return services.authService.getUserDetails().then(user => {
93         services.authService.saveUser(user);
94         dispatch(authActions.USER_DETAILS_SUCCESS(user));
95         return user;
96     });
97 };
98
99 export type AuthAction = UnionOf<typeof authActions>;