16029: Fixes failed login attempt by redirecting the user to the login page.
[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 { matchTokenRoute, matchFedTokenRoute } from '~/routes/routes';
14 import { createServices, setAuthorizationHeader } from "~/services/services";
15 import { cancelLinking } from '~/store/link-account-panel/link-account-panel-actions';
16 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
17 import { WORKBENCH_LOADING_SCREEN } from '~/store/workbench/workbench-actions';
18
19 export const authActions = unionize({
20     LOGIN: {},
21     LOGOUT: ofType<{ deleteLinkData: boolean }>(),
22     SET_CONFIG: ofType<{ config: Config }>(),
23     INIT_USER: 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     REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
35 });
36
37 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38     // Cancel any link account ops in progress unless the user has
39     // just logged in or there has been a successful link operation
40     const data = services.linkAccountService.getLinkOpStatus();
41     if (!matchTokenRoute(location.pathname) && (!matchFedTokenRoute(location.pathname)) && data === undefined) {
42         dispatch<any>(cancelLinking()).then(() => {
43             dispatch<any>(init(config));
44         });
45     }
46     else {
47         dispatch<any>(init(config));
48     }
49 };
50
51 const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
52     const token = services.authService.getApiToken();
53     let homeCluster = services.authService.getHomeCluster();
54     if (homeCluster && !config.remoteHosts[homeCluster]) {
55         homeCluster = undefined;
56     }
57     dispatch(authActions.SET_CONFIG({ config }));
58     dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
59
60     if (token && token !== "undefined") {
61         dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
62         dispatch<any>(saveApiToken(token)).then(() => {
63             dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
64         }).catch(() => {
65             dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
66         });
67     }
68 };
69
70 export const getConfig = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Config => {
71     const state = getState().auth;
72     return state.remoteHostsConfig[state.localCluster];
73 };
74
75 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
76     const config = dispatch<any>(getConfig);
77     const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
78     setAuthorizationHeader(svc, token);
79     return svc.authService.getUserDetails().then((user: User) => {
80         dispatch(authActions.INIT_USER({ user, token }));
81     }).catch(() => {
82         dispatch(authActions.LOGOUT({ deleteLinkData: false }));
83     });
84 };
85
86 export const login = (uuidPrefix: string, homeCluster: string, loginCluster: string,
87     remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
88         services.authService.login(uuidPrefix, homeCluster, loginCluster, remoteHosts);
89         dispatch(authActions.LOGIN());
90     };
91
92 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
93     dispatch(authActions.LOGOUT({ deleteLinkData }));
94 };
95
96 export type AuthAction = UnionOf<typeof authActions>;