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 { getUserUuid } from "~/common/getuser";
9 import { ServiceRepository } from "~/services/services";
10 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
11 import { FormErrors, reset, startSubmit, stopSubmit } from "redux-form";
12 import { KeyType } from "~/models/ssh-key";
14 AuthorizedKeysServiceError,
15 getAuthorizedKeysServiceError
16 } from "~/services/authorized-keys-service/authorized-keys-service";
17 import { setBreadcrumbs } from "~/store/breadcrumbs/breadcrumbs-actions";
18 import { authActions } from "~/store/auth/auth-action";
20 export const SSH_KEY_CREATE_FORM_NAME = 'sshKeyCreateFormName';
21 export const SSH_KEY_PUBLIC_KEY_DIALOG = 'sshKeyPublicKeyDialog';
22 export const SSH_KEY_REMOVE_DIALOG = 'sshKeyRemoveDialog';
23 export const SSH_KEY_ATTRIBUTES_DIALOG = 'sshKeyAttributesDialog';
25 export interface SshKeyCreateFormDialogData {
30 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
32 export const openPublicKeyDialog = (name: string, publicKey: string) =>
33 dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
35 export const openSshKeyAttributesDialog = (uuid: string) =>
36 (dispatch: Dispatch, getState: () => RootState) => {
37 const sshKey = getState().auth.sshKeys.find(it => it.uuid === uuid);
38 dispatch(dialogActions.OPEN_DIALOG({ id: SSH_KEY_ATTRIBUTES_DIALOG, data: { sshKey } }));
41 export const openSshKeyRemoveDialog = (uuid: string) =>
42 (dispatch: Dispatch, getState: () => RootState) => {
43 dispatch(dialogActions.OPEN_DIALOG({
44 id: SSH_KEY_REMOVE_DIALOG,
46 title: 'Remove public key',
47 text: 'Are you sure you want to remove this public key?',
48 confirmButtonLabel: 'Remove',
54 export const removeSshKey = (uuid: string) =>
55 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
57 await services.authorizedKeysService.delete(uuid);
58 dispatch(authActions.REMOVE_SSH_KEY(uuid));
59 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Public Key has been successfully removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
62 export const createSshKey = (data: SshKeyCreateFormDialogData) =>
63 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
64 const userUuid = getUserUuid(getState());
65 if (!userUuid) { return; }
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.",
81 kind: SnackbarKind.SUCCESS
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));
93 export const loadSshKeysPanel = () =>
94 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
96 dispatch(setBreadcrumbs([{ label: 'SSH Keys' }]));
97 const response = await services.authorizedKeysService.list();
98 dispatch(authActions.SET_SSH_KEYS(response.items));