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 { AxiosInstance } from "axios";
8 import { RootState } from "../store";
9 import { ServiceRepository } from "~/services/services";
10 import { SshKeyResource } from '~/models/ssh-key';
11 import { User } from "~/models/user";
12 import { Session } from "~/models/session";
13 import { getDiscoveryURL, Config } from '~/common/config';
14 import { initSessions } from "~/store/auth/auth-action-session";
15 import Axios from "axios";
17 export const authActions = unionize({
18 SAVE_API_TOKEN: ofType<string>(),
21 CONFIG: ofType<{ config: Config }>(),
22 INIT: ofType<{ user: User, token: string }>(),
23 USER_DETAILS_REQUEST: {},
24 USER_DETAILS_SUCCESS: ofType<User>(),
25 SET_SSH_KEYS: ofType<SshKeyResource[]>(),
26 ADD_SSH_KEY: ofType<SshKeyResource>(),
27 REMOVE_SSH_KEY: ofType<string>(),
28 SET_HOME_CLUSTER: ofType<string>(),
29 SET_SESSIONS: ofType<Session[]>(),
30 ADD_SESSION: ofType<Session>(),
31 REMOVE_SESSION: ofType<string>(),
32 UPDATE_SESSION: ofType<Session>(),
33 REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
36 function setAuthorizationHeader(services: ServiceRepository, token: string) {
37 services.apiClient.defaults.headers.common = {
38 Authorization: `OAuth2 ${token}`
40 services.webdavClient.defaults.headers = {
41 Authorization: `OAuth2 ${token}`
45 function removeAuthorizationHeader(client: AxiosInstance) {
46 delete client.defaults.headers.common.Authorization;
49 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50 const user = services.authService.getUser();
51 const token = services.authService.getApiToken();
53 setAuthorizationHeader(services, token);
55 dispatch(authActions.CONFIG({ config }));
57 dispatch(authActions.INIT({ user, token }));
58 dispatch<any>(initSessions(services.authService, config, user));
59 dispatch<any>(getUserDetails()).then((user: User) => {
60 dispatch(authActions.INIT({ user, token }));
63 Object.keys(config.remoteHosts).map((k) => {
64 Axios.get<Config>(getDiscoveryURL(config.remoteHosts[k]))
65 .then(response => dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: response.data })));
69 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70 services.authService.saveApiToken(token);
71 setAuthorizationHeader(services, token);
72 dispatch(authActions.SAVE_API_TOKEN(token));
75 export const login = (uuidPrefix: string, homeCluster: string, remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76 services.authService.login(uuidPrefix, homeCluster, remoteHosts);
77 dispatch(authActions.LOGIN());
80 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
81 services.authService.removeApiToken();
82 services.authService.removeUser();
83 removeAuthorizationHeader(services.apiClient);
84 services.authService.logout();
85 dispatch(authActions.LOGOUT());
88 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
89 dispatch(authActions.USER_DETAILS_REQUEST());
90 return services.authService.getUserDetails().then(user => {
91 services.authService.saveUser(user);
92 dispatch(authActions.USER_DETAILS_SUCCESS(user));
97 export type AuthAction = UnionOf<typeof authActions>;