14720: Fix tests
[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 { Config } from '~/common/config';
14 import { initSessions } from "~/store/auth/auth-action-session";
15
16 export const authActions = unionize({
17     SAVE_API_TOKEN: ofType<string>(),
18     LOGIN: {},
19     LOGOUT: {},
20     CONFIG: ofType<{ uuidPrefix: string, remoteHosts: { [key: string]: string } }>(),
21     INIT: ofType<{ user: User, token: string }>(),
22     USER_DETAILS_REQUEST: {},
23     USER_DETAILS_SUCCESS: ofType<User>(),
24     SET_SSH_KEYS: ofType<SshKeyResource[]>(),
25     ADD_SSH_KEY: ofType<SshKeyResource>(),
26     REMOVE_SSH_KEY: ofType<string>(),
27     SET_HOME_CLUSTER: ofType<string>(),
28     SET_SESSIONS: ofType<Session[]>(),
29     ADD_SESSION: ofType<Session>(),
30     REMOVE_SESSION: ofType<string>(),
31     UPDATE_SESSION: ofType<Session>()
32 });
33
34 function setAuthorizationHeader(services: ServiceRepository, token: string) {
35     services.apiClient.defaults.headers.common = {
36         Authorization: `OAuth2 ${token}`
37     };
38     services.webdavClient.defaults.headers = {
39         Authorization: `OAuth2 ${token}`
40     };
41 }
42
43 function removeAuthorizationHeader(client: AxiosInstance) {
44     delete client.defaults.headers.common.Authorization;
45 }
46
47 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48     const user = services.authService.getUser();
49     const token = services.authService.getApiToken();
50     if (token) {
51         setAuthorizationHeader(services, token);
52     }
53     dispatch(authActions.CONFIG({ uuidPrefix: config.uuidPrefix, remoteHosts: config.remoteHosts }));
54     if (token && user) {
55         dispatch(authActions.INIT({ user, token }));
56         dispatch<any>(initSessions(services.authService, config, user));
57     }
58 };
59
60 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
61     services.authService.saveApiToken(token);
62     setAuthorizationHeader(services, token);
63     dispatch(authActions.SAVE_API_TOKEN(token));
64 };
65
66 export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
67     services.authService.login(uuidPrefix, homeCluster);
68     dispatch(authActions.LOGIN());
69 };
70
71 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72     services.authService.removeApiToken();
73     services.authService.removeUser();
74     removeAuthorizationHeader(services.apiClient);
75     services.authService.logout();
76     dispatch(authActions.LOGOUT());
77 };
78
79 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
80     dispatch(authActions.USER_DETAILS_REQUEST());
81     return services.authService.getUserDetails().then(user => {
82         services.authService.saveUser(user);
83         dispatch(authActions.USER_DETAILS_SUCCESS(user));
84         return user;
85     });
86 };
87
88 export type AuthAction = UnionOf<typeof authActions>;