15088: Moves link account operation check to initAuth
[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 { cancelLinking } from '~/store/link-account-panel/link-account-panel-actions';
16 import { matchTokenRoute } from '~/routes/routes';
17
18 export const authActions = unionize({
19     SAVE_API_TOKEN: ofType<string>(),
20     SAVE_USER: ofType<UserResource>(),
21     LOGIN: {},
22     LOGOUT: {},
23     CONFIG: ofType<{ config: Config }>(),
24     INIT: ofType<{ user: User, token: string }>(),
25     USER_DETAILS_REQUEST: {},
26     USER_DETAILS_SUCCESS: ofType<User>(),
27     SET_SSH_KEYS: ofType<SshKeyResource[]>(),
28     ADD_SSH_KEY: ofType<SshKeyResource>(),
29     REMOVE_SSH_KEY: ofType<string>(),
30     SET_HOME_CLUSTER: ofType<string>(),
31     SET_SESSIONS: ofType<Session[]>(),
32     ADD_SESSION: ofType<Session>(),
33     REMOVE_SESSION: ofType<string>(),
34     UPDATE_SESSION: ofType<Session>()
35 });
36
37 function setAuthorizationHeader(services: ServiceRepository, token: string) {
38     services.apiClient.defaults.headers.common = {
39         Authorization: `OAuth2 ${token}`
40     };
41     services.webdavClient.defaults.headers = {
42         Authorization: `OAuth2 ${token}`
43     };
44 }
45
46 function removeAuthorizationHeader(client: AxiosInstance) {
47     delete client.defaults.headers.common.Authorization;
48 }
49
50 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
51     // Cancel any link account ops in progess unless the user has
52     // just logged in or there has been a successful link operation
53     const data = services.linkAccountService.getLinkOpStatus();
54     if (!matchTokenRoute(location.pathname) && data === undefined) {
55         dispatch<any>(cancelLinking());
56     }
57
58     const user = services.authService.getUser();
59     const token = services.authService.getApiToken();
60     if (token) {
61         setAuthorizationHeader(services, token);
62     }
63     dispatch(authActions.CONFIG({ config }));
64     if (token && user) {
65         dispatch(authActions.INIT({ user, token }));
66         dispatch<any>(initSessions(services.authService, config, user));
67         dispatch<any>(getUserDetails()).then((user: User) => {
68             dispatch(authActions.INIT({ user, token }));
69         });
70     }
71 };
72
73 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74     services.authService.saveApiToken(token);
75     setAuthorizationHeader(services, token);
76     dispatch(authActions.SAVE_API_TOKEN(token));
77 };
78
79 export const saveUser = (user: UserResource) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
80     services.authService.saveUser(user);
81     dispatch(authActions.SAVE_USER(user));
82 };
83
84 export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
85     services.authService.login(uuidPrefix, homeCluster);
86     dispatch(authActions.LOGIN());
87 };
88
89 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90     if (deleteLinkData) {
91         services.linkAccountService.removeAccountToLink();
92     }
93     services.authService.removeApiToken();
94     services.authService.removeUser();
95     removeAuthorizationHeader(services.apiClient);
96     services.authService.logout();
97     dispatch(authActions.LOGOUT());
98 };
99
100 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
101     dispatch(authActions.USER_DETAILS_REQUEST());
102     return services.authService.getUserDetails().then(user => {
103         services.authService.saveUser(user);
104         dispatch(authActions.USER_DETAILS_SUCCESS(user));
105         return user;
106     });
107 };
108
109 export type AuthAction = UnionOf<typeof authActions>;