Add site manager and initial validation
[arvados-workbench2.git] / src / store / auth / auth-action-ssh.ts
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";
8 import {
9     AuthorizedKeysServiceError,
10     getAuthorizedKeysServiceError
11 } from "~/services/authorized-keys-service/authorized-keys-service";
12 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
13 import {
14     authActions,
15 } from "~/store/auth/auth-action";
16
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';
21
22 export interface SshKeyCreateFormDialogData {
23     publicKey: string;
24     name: string;
25 }
26
27 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
28
29 export const openPublicKeyDialog = (name: string, publicKey: string) =>
30     dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
31
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 } }));
36     };
37
38 export const openSshKeyRemoveDialog = (uuid: string) =>
39     (dispatch: Dispatch, getState: () => RootState) => {
40         dispatch(dialogActions.OPEN_DIALOG({
41             id: SSH_KEY_REMOVE_DIALOG,
42             data: {
43                 title: 'Remove public key',
44                 text: 'Are you sure you want to remove this public key?',
45                 confirmButtonLabel: 'Remove',
46                 uuid
47             }
48         }));
49     };
50
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 }));
57     };
58
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));
64         try {
65             const newSshKey = await services.authorizedKeysService.create({
66                 name,
67                 publicKey,
68                 keyType: KeyType.SSH,
69                 authorizedUserUuid: userUuid
70             });
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.",
76                 hideDuration: 2000
77             }));
78         } catch (e) {
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));
84             }
85         }
86     };
87
88 export const loadSshKeysPanel = () =>
89     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
90         try {
91             dispatch(setBreadcrumbs([{ label: 'SSH Keys'}]));
92             const response = await services.authorizedKeysService.list();
93             dispatch(authActions.SET_SSH_KEYS(response.items));
94         } catch (e) {
95             return;
96         }
97     };
98