UI Improvements
[arvados-workbench2.git] / src / store / auth / auth-action-ssh.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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, SnackbarKind} from "~/store/snackbar/snackbar-actions";
10 import { FormErrors, reset, startSubmit, stopSubmit } from "redux-form";
11 import { KeyType } from "~/models/ssh-key";
12 import {
13     AuthorizedKeysServiceError,
14     getAuthorizedKeysServiceError
15 } from "~/services/authorized-keys-service/authorized-keys-service";
16 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
17 import {
18     authActions,
19 } from "~/store/auth/auth-action";
20
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';
25
26 export interface SshKeyCreateFormDialogData {
27     publicKey: string;
28     name: string;
29 }
30
31 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
32
33 export const openPublicKeyDialog = (name: string, publicKey: string) =>
34     dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
35
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 } }));
40     };
41
42 export const openSshKeyRemoveDialog = (uuid: string) =>
43     (dispatch: Dispatch, getState: () => RootState) => {
44         dispatch(dialogActions.OPEN_DIALOG({
45             id: SSH_KEY_REMOVE_DIALOG,
46             data: {
47                 title: 'Remove public key',
48                 text: 'Are you sure you want to remove this public key?',
49                 confirmButtonLabel: 'Remove',
50                 uuid
51             }
52         }));
53     };
54
55 export const removeSshKey = (uuid: string) =>
56     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
57         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
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, kind: SnackbarKind.SUCCESS }));
61     };
62
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));
68         try {
69             const newSshKey = await services.authorizedKeysService.create({
70                 name,
71                 publicKey,
72                 keyType: KeyType.SSH,
73                 authorizedUserUuid: userUuid
74             });
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.",
80                 hideDuration: 2000,
81                 kind: SnackbarKind.SUCCESS
82             }));
83         } catch (e) {
84             const error = getAuthorizedKeysServiceError(e);
85             if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
86                 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' } as FormErrors));
87             } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
88                 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' } as FormErrors));
89             }
90         }
91     };
92
93 export const loadSshKeysPanel = () =>
94     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
95         try {
96             dispatch(setBreadcrumbs([{ label: 'SSH Keys'}]));
97             const response = await services.authorizedKeysService.list();
98             dispatch(authActions.SET_SSH_KEYS(response.items));
99         } catch (e) {
100             return;
101         }
102     };
103