1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { dialogActions } from "~/store/dialog/dialog-actions";
6 import { Dispatch } from "redux";
7 import { RootState } from "~/store/store";
8 import { ServiceRepository } from "~/services/services";
9 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
10 import { FormErrors, reset, startSubmit, stopSubmit } from "redux-form";
11 import { KeyType } from "~/models/ssh-key";
13 AuthorizedKeysServiceError,
14 getAuthorizedKeysServiceError
15 } from "~/services/authorized-keys-service/authorized-keys-service";
16 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
19 } from "~/store/auth/auth-action";
21 export const SSH_KEY_CREATE_FORM_NAME = 'sshKeyCreateFormName';
22 export const SSH_KEY_PUBLIC_KEY_DIALOG = 'sshKeyPublicKeyDialog';
23 export const SSH_KEY_REMOVE_DIALOG = 'sshKeyRemoveDialog';
24 export const SSH_KEY_ATTRIBUTES_DIALOG = 'sshKeyAttributesDialog';
26 export interface SshKeyCreateFormDialogData {
31 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
33 export const openPublicKeyDialog = (name: string, publicKey: string) =>
34 dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
36 export const openSshKeyAttributesDialog = (uuid: string) =>
37 (dispatch: Dispatch, getState: () => RootState) => {
38 const sshKey = getState().auth.sshKeys.find(it => it.uuid === uuid);
39 dispatch(dialogActions.OPEN_DIALOG({ id: SSH_KEY_ATTRIBUTES_DIALOG, data: { sshKey } }));
42 export const openSshKeyRemoveDialog = (uuid: string) =>
43 (dispatch: Dispatch, getState: () => RootState) => {
44 dispatch(dialogActions.OPEN_DIALOG({
45 id: SSH_KEY_REMOVE_DIALOG,
47 title: 'Remove public key',
48 text: 'Are you sure you want to remove this public key?',
49 confirmButtonLabel: 'Remove',
55 export const removeSshKey = (uuid: string) =>
56 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
57 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
58 await services.authorizedKeysService.delete(uuid);
59 dispatch(authActions.REMOVE_SSH_KEY(uuid));
60 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Public Key has been successfully removed.', hideDuration: 2000 }));
63 export const createSshKey = (data: SshKeyCreateFormDialogData) =>
64 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65 const userUuid = getState().auth.user!.uuid;
66 const { name, publicKey } = data;
67 dispatch(startSubmit(SSH_KEY_CREATE_FORM_NAME));
69 const newSshKey = await services.authorizedKeysService.create({
73 authorizedUserUuid: userUuid
75 dispatch(authActions.ADD_SSH_KEY(newSshKey));
76 dispatch(dialogActions.CLOSE_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME }));
77 dispatch(reset(SSH_KEY_CREATE_FORM_NAME));
78 dispatch(snackbarActions.OPEN_SNACKBAR({
79 message: "Public key has been successfully created.",
83 const error = getAuthorizedKeysServiceError(e);
84 if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
85 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' } as FormErrors));
86 } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
87 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' } as FormErrors));
92 export const loadSshKeysPanel = () =>
93 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
95 dispatch(setBreadcrumbs([{ label: 'SSH Keys'}]));
96 const response = await services.authorizedKeysService.list();
97 dispatch(authActions.SET_SSH_KEYS(response.items));