Merge branch 'origin/master' into 14478-log-in-into-clusters
[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 } 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 ...' }));
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 }));
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             }));
82         } catch (e) {
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));
88             }
89         }
90     };
91
92 export const loadSshKeysPanel = () =>
93     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
94         try {
95             dispatch(setBreadcrumbs([{ label: 'SSH Keys'}]));
96             const response = await services.authorizedKeysService.list();
97             dispatch(authActions.SET_SSH_KEYS(response.items));
98         } catch (e) {
99             return;
100         }
101     };
102