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 { reset, stopSubmit, startSubmit, FormErrors } from 'redux-form';
8 import { AxiosInstance } from "axios";
9 import { RootState } from "../store";
10 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
11 import { dialogActions } from '~/store/dialog/dialog-actions';
12 import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
13 import { ServiceRepository } from "~/services/services";
14 import { getAuthorizedKeysServiceError, AuthorizedKeysServiceError } from '~/services/authorized-keys-service/authorized-keys-service';
15 import { KeyType, SshKeyResource } from '~/models/ssh-key';
16 import { User } from "~/models/user";
17 import * as Routes from '~/routes/routes';
19 export const authActions = unionize({
20 SAVE_API_TOKEN: ofType<string>(),
23 INIT: ofType<{ user: User, token: string }>(),
24 USER_DETAILS_REQUEST: {},
25 USER_DETAILS_SUCCESS: ofType<User>(),
26 SET_SSH_KEYS: ofType<SshKeyResource[]>(),
27 ADD_SSH_KEY: ofType<SshKeyResource>(),
28 REMOVE_SSH_KEY: ofType<string>()
31 export const SSH_KEY_CREATE_FORM_NAME = 'sshKeyCreateFormName';
32 export const SSH_KEY_PUBLIC_KEY_DIALOG = 'sshKeyPublicKeyDialog';
33 export const SSH_KEY_REMOVE_DIALOG = 'sshKeyRemoveDialog';
34 export const SSH_KEY_ATTRIBUTES_DIALOG = 'sshKeyAttributesDialog';
36 export interface SshKeyCreateFormDialogData {
41 function setAuthorizationHeader(services: ServiceRepository, token: string) {
42 services.apiClient.defaults.headers.common = {
43 Authorization: `OAuth2 ${token}`
45 services.webdavClient.defaults.headers = {
46 Authorization: `OAuth2 ${token}`
50 function removeAuthorizationHeader(client: AxiosInstance) {
51 delete client.defaults.headers.common.Authorization;
54 export const initAuth = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 const user = services.authService.getUser();
56 const token = services.authService.getApiToken();
58 setAuthorizationHeader(services, token);
61 dispatch(authActions.INIT({ user, token }));
65 export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
66 services.authService.saveApiToken(token);
67 setAuthorizationHeader(services, token);
68 dispatch(authActions.SAVE_API_TOKEN(token));
71 export const login = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72 services.authService.login();
73 dispatch(authActions.LOGIN());
76 export const logout = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
77 services.authService.removeApiToken();
78 services.authService.removeUser();
79 removeAuthorizationHeader(services.apiClient);
80 services.authService.logout();
81 dispatch(authActions.LOGOUT());
84 export const getUserDetails = () => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<User> => {
85 dispatch(authActions.USER_DETAILS_REQUEST());
86 return services.authService.getUserDetails().then(user => {
87 services.authService.saveUser(user);
88 dispatch(authActions.USER_DETAILS_SUCCESS(user));
93 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
95 export const openPublicKeyDialog = (name: string, publicKey: string) =>
96 dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
98 export const openSshKeyAttributesDialog = (uuid: string) =>
99 (dispatch: Dispatch, getState: () => RootState) => {
100 const sshKey = getState().auth.sshKeys.find(it => it.uuid === uuid);
101 dispatch(dialogActions.OPEN_DIALOG({ id: SSH_KEY_ATTRIBUTES_DIALOG, data: { sshKey } }));
104 export const openSshKeyRemoveDialog = (uuid: string) =>
105 (dispatch: Dispatch, getState: () => RootState) => {
106 dispatch(dialogActions.OPEN_DIALOG({
107 id: SSH_KEY_REMOVE_DIALOG,
109 title: 'Remove public key',
110 text: 'Are you sure you want to remove this public key?',
111 confirmButtonLabel: 'Remove',
117 export const removeSshKey = (uuid: string) =>
118 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
119 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
120 await services.authorizedKeysService.delete(uuid);
121 dispatch(authActions.REMOVE_SSH_KEY(uuid));
122 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Public Key has been successfully removed.', hideDuration: 2000 }));
125 export const createSshKey = (data: SshKeyCreateFormDialogData) =>
126 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
127 const userUuid = getState().auth.user!.uuid;
128 const { name, publicKey } = data;
129 dispatch(startSubmit(SSH_KEY_CREATE_FORM_NAME));
131 const newSshKey = await services.authorizedKeysService.create({
134 keyType: KeyType.SSH,
135 authorizedUserUuid: userUuid
137 dispatch(authActions.ADD_SSH_KEY(newSshKey));
138 dispatch(dialogActions.CLOSE_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME }));
139 dispatch(reset(SSH_KEY_CREATE_FORM_NAME));
140 dispatch(snackbarActions.OPEN_SNACKBAR({
141 message: "Public key has been successfully created.",
145 const error = getAuthorizedKeysServiceError(e);
146 if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
147 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' } as FormErrors));
148 } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
149 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' } as FormErrors));
154 export const loadSshKeysPanel = () =>
155 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
157 const userUuid = getState().auth.user!.uuid;
158 const { router } = getState();
159 const pathname = router.location ? router.location.pathname : '';
160 dispatch(setBreadcrumbs([{ label: 'SSH Keys' }]));
161 const response = await services.authorizedKeysService.list();
162 const userSshKeys = response.items.find(it => it.ownerUuid === userUuid);
163 return Routes.matchSshKeysAdminRoute(pathname) ? dispatch(authActions.SET_SSH_KEYS(response.items)) : dispatch(authActions.SET_SSH_KEYS([userSshKeys!]));
169 export type AuthAction = UnionOf<typeof authActions>;