1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
19 export const authActions = unionize({
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 }>(),
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));
47 dispatch<any>(init(config));
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;
57 dispatch(authActions.SET_CONFIG({ config }));
58 dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
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));
65 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
70 export const getConfig = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Config => {
71 const state = getState().auth;
72 return state.remoteHostsConfig[state.localCluster];
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 }));
84 export const login = (uuidPrefix: string, homeCluster: string, loginCluster: string,
85 remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
86 services.authService.login(uuidPrefix, homeCluster, loginCluster, remoteHosts);
87 dispatch(authActions.LOGIN());
90 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
91 dispatch(authActions.LOGOUT({ deleteLinkData }));
94 export type AuthAction = UnionOf<typeof authActions>;