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 { Config } from '~/common/config';
14 import { initSessions } from "~/store/auth/auth-action-session";
16 export const authActions = unionize({
17 SAVE_API_TOKEN: ofType<string>(),
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>()
34 function setAuthorizationHeader(services: ServiceRepository, token: string) {
35 services.apiClient.defaults.headers.common = {
36 Authorization: `OAuth2 ${token}`
38 services.webdavClient.defaults.headers = {
39 Authorization: `OAuth2 ${token}`
43 function removeAuthorizationHeader(client: AxiosInstance) {
44 delete client.defaults.headers.common.Authorization;
47 export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48 const user = services.authService.getUser();
49 const token = services.authService.getApiToken();
51 setAuthorizationHeader(services, token);
53 dispatch(authActions.CONFIG({ config }));
55 dispatch(authActions.INIT({ user, token }));
56 dispatch<any>(initSessions(services.authService, config, user));
57 dispatch<any>(getUserDetails()).then((user: User) => {
58 dispatch(authActions.INIT({ user, token }));
63 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
64 services.authService.saveApiToken(token);
65 setAuthorizationHeader(services, token);
66 dispatch(authActions.SAVE_API_TOKEN(token));
69 export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70 services.authService.login(uuidPrefix, homeCluster);
71 dispatch(authActions.LOGIN());
74 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75 services.authService.removeApiToken();
76 services.authService.removeUser();
77 removeAuthorizationHeader(services.apiClient);
78 services.authService.logout();
79 dispatch(authActions.LOGOUT());
82 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
83 dispatch(authActions.USER_DETAILS_REQUEST());
84 return services.authService.getUserDetails().then(user => {
85 services.authService.saveUser(user);
86 dispatch(authActions.USER_DETAILS_SUCCESS(user));
91 export type AuthAction = UnionOf<typeof authActions>;