15088: Handles browser navigation during link account ops
[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, UserResource } 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 import { UserRepositoryCreate } from '~/views-components/dialog-create/dialog-user-create';
16
17 export const authActions = unionize({
18     SAVE_API_TOKEN: ofType<string>(),
19     SAVE_USER: ofType<UserResource>(),
20     LOGIN: {},
21     LOGOUT: {},
22     CONFIG: ofType<{ config: Config }>(),
23     INIT: ofType<{ user: User, token: string }>(),
24     USER_DETAILS_REQUEST: {},
25     USER_DETAILS_SUCCESS: ofType<User>(),
26     SET_SSH_KEYS: ofType<SshKeyResource[]>(),
27     ADD_SSH_KEY: ofType<SshKeyResource>(),
28     REMOVE_SSH_KEY: ofType<string>(),
29     SET_HOME_CLUSTER: ofType<string>(),
30     SET_SESSIONS: ofType<Session[]>(),
31     ADD_SESSION: ofType<Session>(),
32     REMOVE_SESSION: ofType<string>(),
33     UPDATE_SESSION: ofType<Session>()
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         });
62     }
63 };
64
65 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
66     services.authService.saveApiToken(token);
67     setAuthorizationHeader(services, token);
68     dispatch(authActions.SAVE_API_TOKEN(token));
69 };
70
71 export const saveUser = (user: UserResource) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72     services.authService.saveUser(user);
73     dispatch(authActions.SAVE_USER(user));
74 };
75
76 export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
77     services.authService.login(uuidPrefix, homeCluster);
78     dispatch(authActions.LOGIN());
79 };
80
81 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
82     if (deleteLinkData) {
83         services.linkAccountService.removeAccountToLink();
84     }
85     services.authService.removeApiToken();
86     services.authService.removeUser();
87     removeAuthorizationHeader(services.apiClient);
88     services.authService.logout();
89     dispatch(authActions.LOGOUT());
90 };
91
92 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
93     dispatch(authActions.USER_DETAILS_REQUEST());
94     return services.authService.getUserDetails().then(user => {
95         services.authService.saveUser(user);
96         dispatch(authActions.USER_DETAILS_SUCCESS(user));
97         return user;
98     });
99 };
100
101 export type AuthAction = UnionOf<typeof authActions>;