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';
17 export const authActions = unionize({
19 LOGOUT: ofType<{ deleteLinkData: boolean }>(),
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 }>(),
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));
45 dispatch<any>(init(config));
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;
55 dispatch(authActions.CONFIG({ config }));
56 dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
58 if (token && token !== "undefined") {
59 dispatch<any>(saveApiToken(token));
63 export const getConfig = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Config => {
64 const state = getState().auth;
65 return state.remoteHostsConfig[state.localCluster];
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 }));
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());
83 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84 dispatch(authActions.LOGOUT({ deleteLinkData }));
87 export type AuthAction = UnionOf<typeof authActions>;