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';
18 import { addRemoteConfig } from './auth-action-session';
20 export const authActions = unionize({
22 LOGOUT: ofType<{ deleteLinkData: boolean }>(),
23 SET_CONFIG: ofType<{ config: Config }>(),
24 INIT_USER: 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 REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
38 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39 // Cancel any link account ops in progress unless the user has
40 // just logged in or there has been a successful link operation
41 const data = services.linkAccountService.getLinkOpStatus();
42 if (!matchTokenRoute(location.pathname) &&
43 (!matchFedTokenRoute(location.pathname)) && data === undefined) {
44 dispatch<any>(cancelLinking()).then(() => {
45 dispatch<any>(init(config));
48 dispatch<any>(init(config));
52 const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
53 const remoteHosts = () => getState().auth.remoteHosts;
54 const token = services.authService.getApiToken();
55 let homeCluster = services.authService.getHomeCluster();
56 if (homeCluster && !config.remoteHosts[homeCluster]) {
57 homeCluster = undefined;
59 dispatch(authActions.SET_CONFIG({ config }));
60 Object.keys(remoteHosts()).forEach((remoteUuid: string) => {
61 const remoteHost = remoteHosts()[remoteUuid];
62 if (remoteUuid !== config.uuidPrefix) {
63 dispatch<any>(addRemoteConfig(remoteHost));
66 dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
68 if (token && token !== "undefined") {
69 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
70 dispatch<any>(saveApiToken(token)).then(() => {
71 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
73 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
78 export const getConfig = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Config => {
79 const state = getState().auth;
80 return state.remoteHostsConfig[state.localCluster];
83 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
84 const config = dispatch<any>(getConfig);
85 const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
86 setAuthorizationHeader(svc, token);
87 return svc.authService.getUserDetails().then((user: User) => {
88 dispatch(authActions.INIT_USER({ user, token }));
90 dispatch(authActions.LOGOUT({ deleteLinkData: false }));
94 export const login = (uuidPrefix: string, homeCluster: string, loginCluster: string,
95 remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
96 services.authService.login(uuidPrefix, homeCluster, loginCluster, remoteHosts);
97 dispatch(authActions.LOGIN());
100 export const logout = (deleteLinkData: boolean = false) =>
101 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) =>
102 dispatch(authActions.LOGOUT({ deleteLinkData }));
104 export type AuthAction = UnionOf<typeof authActions>;