1 import { dialogActions } from "~/store/dialog/dialog-actions";
2 import { Dispatch } from "redux";
3 import { RootState } from "~/store/store";
4 import { ServiceRepository } from "~/services/services";
5 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
6 import { FormErrors, reset, startSubmit, stopSubmit } from "redux-form";
7 import { KeyType } from "~/models/ssh-key";
9 AuthorizedKeysServiceError,
10 getAuthorizedKeysServiceError
11 } from "~/services/authorized-keys-service/authorized-keys-service";
12 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
15 } from "~/store/auth/auth-action";
17 export const SSH_KEY_CREATE_FORM_NAME = 'sshKeyCreateFormName';
18 export const SSH_KEY_PUBLIC_KEY_DIALOG = 'sshKeyPublicKeyDialog';
19 export const SSH_KEY_REMOVE_DIALOG = 'sshKeyRemoveDialog';
20 export const SSH_KEY_ATTRIBUTES_DIALOG = 'sshKeyAttributesDialog';
22 export interface SshKeyCreateFormDialogData {
27 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
29 export const openPublicKeyDialog = (name: string, publicKey: string) =>
30 dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
32 export const openSshKeyAttributesDialog = (uuid: string) =>
33 (dispatch: Dispatch, getState: () => RootState) => {
34 const sshKey = getState().auth.sshKeys.find(it => it.uuid === uuid);
35 dispatch(dialogActions.OPEN_DIALOG({ id: SSH_KEY_ATTRIBUTES_DIALOG, data: { sshKey } }));
38 export const openSshKeyRemoveDialog = (uuid: string) =>
39 (dispatch: Dispatch, getState: () => RootState) => {
40 dispatch(dialogActions.OPEN_DIALOG({
41 id: SSH_KEY_REMOVE_DIALOG,
43 title: 'Remove public key',
44 text: 'Are you sure you want to remove this public key?',
45 confirmButtonLabel: 'Remove',
51 export const removeSshKey = (uuid: string) =>
52 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
53 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
54 await services.authorizedKeysService.delete(uuid);
55 dispatch(authActions.REMOVE_SSH_KEY(uuid));
56 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Public Key has been successfully removed.', hideDuration: 2000 }));
59 export const createSshKey = (data: SshKeyCreateFormDialogData) =>
60 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
61 const userUuid = getState().auth.user!.uuid;
62 const { name, publicKey } = data;
63 dispatch(startSubmit(SSH_KEY_CREATE_FORM_NAME));
65 const newSshKey = await services.authorizedKeysService.create({
69 authorizedUserUuid: userUuid
71 dispatch(authActions.ADD_SSH_KEY(newSshKey));
72 dispatch(dialogActions.CLOSE_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME }));
73 dispatch(reset(SSH_KEY_CREATE_FORM_NAME));
74 dispatch(snackbarActions.OPEN_SNACKBAR({
75 message: "Public key has been successfully created.",
79 const error = getAuthorizedKeysServiceError(e);
80 if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
81 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' } as FormErrors));
82 } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
83 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' } as FormErrors));
88 export const loadSshKeysPanel = () =>
89 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
91 dispatch(setBreadcrumbs([{ label: 'SSH Keys'}]));
92 const response = await services.authorizedKeysService.list();
93 dispatch(authActions.SET_SSH_KEYS(response.items));