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 SET_EXTRA_TOKEN: ofType<{ extraToken: string }>(),
25 INIT_USER: ofType<{ user: User, token: string }>(),
26 USER_DETAILS_REQUEST: {},
27 USER_DETAILS_SUCCESS: ofType<User>(),
28 SET_SSH_KEYS: ofType<SshKeyResource[]>(),
29 ADD_SSH_KEY: ofType<SshKeyResource>(),
30 REMOVE_SSH_KEY: ofType<string>(),
31 SET_HOME_CLUSTER: ofType<string>(),
32 SET_SESSIONS: ofType<Session[]>(),
33 ADD_SESSION: ofType<Session>(),
34 REMOVE_SESSION: ofType<string>(),
35 UPDATE_SESSION: ofType<Session>(),
36 REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
39 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40 // Cancel any link account ops in progress unless the user has
41 // just logged in or there has been a successful link operation
42 const data = services.linkAccountService.getLinkOpStatus();
43 if (!matchTokenRoute(location.pathname) &&
44 (!matchFedTokenRoute(location.pathname)) && data === undefined) {
45 dispatch<any>(cancelLinking()).then(() => {
46 dispatch<any>(init(config));
49 dispatch<any>(init(config));
53 const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54 const remoteHosts = () => getState().auth.remoteHosts;
55 const token = services.authService.getApiToken();
56 let homeCluster = services.authService.getHomeCluster();
57 if (homeCluster && !config.remoteHosts[homeCluster]) {
58 homeCluster = undefined;
60 dispatch(authActions.SET_CONFIG({ config }));
61 Object.keys(remoteHosts()).forEach((remoteUuid: string) => {
62 const remoteHost = remoteHosts()[remoteUuid];
63 if (remoteUuid !== config.uuidPrefix) {
64 dispatch<any>(addRemoteConfig(remoteHost));
67 dispatch(authActions.SET_HOME_CLUSTER(config.loginCluster || homeCluster || config.uuidPrefix));
69 if (token && token !== "undefined") {
70 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
71 dispatch<any>(saveApiToken(token)).then(() => {
72 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
74 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
79 export const getConfig = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Config => {
80 const state = getState().auth;
81 return state.remoteHostsConfig[state.localCluster];
84 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<any> => {
85 const config = dispatch<any>(getConfig);
86 const svc = createServices(config, { progressFn: () => { }, errorFn: () => { } });
87 setAuthorizationHeader(svc, token);
88 return svc.authService.getUserDetails().then((user: User) => {
89 dispatch(authActions.INIT_USER({ user, token }));
90 // Upon user init, request an extra token that won't be expired on logout
91 // for other uses like the "get token" dialog, or S3 URL building.
92 dispatch<any>(getNewExtraToken());
94 dispatch(authActions.LOGOUT({ deleteLinkData: false }));
98 export const getNewExtraToken = () =>
99 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 const user = getState().auth.user;
101 if (user === undefined) { return; }
103 const aca = await services.apiClientAuthorizationService.create();
104 const newExtraToken = `v2/${aca.uuid}/${aca.apiToken}`;
105 dispatch(authActions.SET_EXTRA_TOKEN({ extraToken: newExtraToken }));
106 return newExtraToken;
112 export const login = (uuidPrefix: string, homeCluster: string, loginCluster: string,
113 remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
114 services.authService.login(uuidPrefix, homeCluster, loginCluster, remoteHosts);
115 dispatch(authActions.LOGIN());
118 export const logout = (deleteLinkData: boolean = false) =>
119 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) =>
120 dispatch(authActions.LOGOUT({ deleteLinkData }));
122 export type AuthAction = UnionOf<typeof authActions>;