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, UserResource } 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 { cancelLinking } from '~/store/link-account-panel/link-account-panel-actions';
16 import { matchTokenRoute, matchFedTokenRoute } from '~/routes/routes';
17 import Axios from "axios";
18 import { AxiosError } from "axios";
20 export const authActions = unionize({
21 SAVE_API_TOKEN: ofType<string>(),
22 SAVE_USER: ofType<UserResource>(),
25 CONFIG: ofType<{ config: Config }>(),
26 INIT: ofType<{ user: User, token: string }>(),
27 USER_DETAILS_REQUEST: {},
28 USER_DETAILS_SUCCESS: ofType<User>(),
29 SET_SSH_KEYS: ofType<SshKeyResource[]>(),
30 ADD_SSH_KEY: ofType<SshKeyResource>(),
31 REMOVE_SSH_KEY: ofType<string>(),
32 SET_HOME_CLUSTER: ofType<string>(),
33 SET_SESSIONS: ofType<Session[]>(),
34 ADD_SESSION: ofType<Session>(),
35 REMOVE_SESSION: ofType<string>(),
36 UPDATE_SESSION: ofType<Session>(),
37 REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(),
40 export function setAuthorizationHeader(services: ServiceRepository, token: string) {
41 services.apiClient.defaults.headers.common = {
42 Authorization: `OAuth2 ${token}`
44 services.webdavClient.defaults.headers = {
45 Authorization: `OAuth2 ${token}`
49 function removeAuthorizationHeader(client: AxiosInstance) {
50 delete client.defaults.headers.common.Authorization;
53 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54 // Cancel any link account ops in progess unless the user has
55 // just logged in or there has been a successful link operation
56 const data = services.linkAccountService.getLinkOpStatus();
57 if (!matchTokenRoute(location.pathname) && (!matchFedTokenRoute(location.pathname)) && data === undefined) {
58 dispatch<any>(cancelLinking()).then(() => {
59 dispatch<any>(init(config));
63 dispatch<any>(init(config));
68 const init = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
69 const user = services.authService.getUser();
70 const token = services.authService.getApiToken();
71 const homeCluster = services.authService.getHomeCluster();
73 setAuthorizationHeader(services, token);
75 dispatch(authActions.CONFIG({ config }));
76 dispatch(authActions.SET_HOME_CLUSTER(homeCluster || config.uuidPrefix));
78 dispatch(authActions.INIT({ user, token }));
79 dispatch<any>(initSessions(services.authService, config, user));
80 dispatch<any>(getUserDetails()).then((user: User) => {
81 dispatch(authActions.INIT({ user, token }));
82 }).catch((err: AxiosError) => {
85 if (err.response.status === 401) {
86 logout()(dispatch, getState, services);
91 Object.keys(config.remoteHosts).map((k) => {
92 Axios.get<Config>(getDiscoveryURL(config.remoteHosts[k]))
93 .then(response => dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: response.data })));
97 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
98 services.authService.saveApiToken(token);
99 setAuthorizationHeader(services, token);
100 dispatch(authActions.SAVE_API_TOKEN(token));
103 export const saveUser = (user: UserResource) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
104 services.authService.saveUser(user);
105 dispatch(authActions.SAVE_USER(user));
108 export const login = (uuidPrefix: string, homeCluster: string, remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
109 services.authService.login(uuidPrefix, homeCluster, remoteHosts);
110 dispatch(authActions.LOGIN());
113 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
114 if (deleteLinkData) {
115 services.linkAccountService.removeAccountToLink();
117 services.authService.removeApiToken();
118 services.authService.removeUser();
119 removeAuthorizationHeader(services.apiClient);
120 services.authService.logout();
121 dispatch(authActions.LOGOUT());
124 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
125 dispatch(authActions.USER_DETAILS_REQUEST());
126 return services.authService.getUserDetails().then(user => {
127 services.authService.saveUser(user);
128 dispatch(authActions.USER_DETAILS_SUCCESS(user));
133 export type AuthAction = UnionOf<typeof authActions>;