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 { 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 } from '~/routes/routes';
18 export const authActions = unionize({
19 SAVE_API_TOKEN: ofType<string>(),
20 SAVE_USER: ofType<UserResource>(),
23 CONFIG: ofType<{ config: Config }>(),
24 INIT: 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>()
37 function setAuthorizationHeader(services: ServiceRepository, token: string) {
38 services.apiClient.defaults.headers.common = {
39 Authorization: `OAuth2 ${token}`
41 services.webdavClient.defaults.headers = {
42 Authorization: `OAuth2 ${token}`
46 function removeAuthorizationHeader(client: AxiosInstance) {
47 delete client.defaults.headers.common.Authorization;
50 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
51 // Cancel any link account ops in progess unless the user has
52 // just logged in or there has been a successful link operation
53 const data = services.linkAccountService.getLinkOpStatus();
54 if (!matchTokenRoute(location.pathname) && data === undefined) {
55 dispatch<any>(cancelLinking());
58 const user = services.authService.getUser();
59 const token = services.authService.getApiToken();
61 setAuthorizationHeader(services, token);
63 dispatch(authActions.CONFIG({ config }));
65 dispatch(authActions.INIT({ user, token }));
66 dispatch<any>(initSessions(services.authService, config, user));
67 dispatch<any>(getUserDetails()).then((user: User) => {
68 dispatch(authActions.INIT({ user, token }));
73 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74 services.authService.saveApiToken(token);
75 setAuthorizationHeader(services, token);
76 dispatch(authActions.SAVE_API_TOKEN(token));
79 export const saveUser = (user: UserResource) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
80 services.authService.saveUser(user);
81 dispatch(authActions.SAVE_USER(user));
84 export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
85 services.authService.login(uuidPrefix, homeCluster);
86 dispatch(authActions.LOGIN());
89 export const logout = (deleteLinkData: boolean = false) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
91 services.linkAccountService.removeAccountToLink();
93 services.authService.removeApiToken();
94 services.authService.removeUser();
95 removeAuthorizationHeader(services.apiClient);
96 services.authService.logout();
97 dispatch(authActions.LOGOUT());
100 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
101 dispatch(authActions.USER_DETAILS_REQUEST());
102 return services.authService.getUserDetails().then(user => {
103 services.authService.saveUser(user);
104 dispatch(authActions.USER_DETAILS_SUCCESS(user));
109 export type AuthAction = UnionOf<typeof authActions>;