15803: Refactor internal user and API token management
[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 { RootState } from "../store";
8 import { ServiceRepository } from "~/services/services";
9 import { SshKeyResource } from '~/models/ssh-key';
10 import { User } from "~/models/user";
11 import { Session } from "~/models/session";
12 import { Config } from '~/common/config';
13 import { cancelLinking } from '~/store/link-account-panel/link-account-panel-actions';
14 import { matchTokenRoute, matchFedTokenRoute } from '~/routes/routes';
15 import { createServices, setAuthorizationHeader, removeAuthorizationHeader } from "~/services/services";
16
17 export const authActions = unionize({
18     LOGIN: {},
19     LOGOUT: {},
20     CONFIG: ofType<{ config: Config }>(),
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     REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
33 });
34
35 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36     // Cancel any link account ops in progress unless the user has
37     // just logged in or there has been a successful link operation
38     const data = services.linkAccountService.getLinkOpStatus();
39     if (!matchTokenRoute(location.pathname) && (!matchFedTokenRoute(location.pathname)) && data === undefined) {
40         dispatch<any>(cancelLinking()).then(() => {
41             dispatch<any>(init(config));
42         });
43     }
44     else {
45         dispatch<any>(init(config));
46     }
47 };
48
49 const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50     const token = services.authService.getApiToken();
51     let homeCluster = services.authService.getHomeCluster();
52     if (homeCluster && !config.remoteHosts[homeCluster]) {
53         homeCluster = undefined;
54     }
55     dispatch(authActions.CONFIG({ config }));
56     dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
57
58     if (token && token !== "undefined") {
59         dispatch<any>(saveApiToken(token));
60     }
61 };
62
63 export const getConfig = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Config => {
64     const state = getState().auth;
65     return state.remoteHostsConfig[state.localCluster];
66 };
67
68 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
69     const config = dispatch<any>(getConfig);
70     const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
71     setAuthorizationHeader(svc, token);
72     return svc.authService.getUserDetails().then((user: User) => {
73         dispatch(authActions.INIT({ user, token }));
74     });
75 };
76
77 export const login = (uuidPrefix: string, homeCluster: string, loginCluster: string,
78     remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
79         services.authService.login(uuidPrefix, homeCluster, loginCluster, remoteHosts);
80         dispatch(authActions.LOGIN());
81     };
82
83 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84     if (deleteLinkData) {
85         services.linkAccountService.removeAccountToLink();
86     }
87     services.authService.removeApiToken();
88     services.authService.removeUser();
89     removeAuthorizationHeader(services);
90     services.authService.logout();
91     dispatch(authActions.LOGOUT());
92 };
93
94 export type AuthAction = UnionOf<typeof authActions>;