Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Co 1375 frontend changes for smime #696

Draft
wants to merge 18 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
"@commitlint/cli": "^19.1.0",
"@commitlint/config-conventional": "^19.1.0",
"@faker-js/faker": "^8.4.1",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@testing-library/dom": "^10.4.0",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.4",
"@types/react": "^18.3.2",
Expand Down Expand Up @@ -85,6 +85,7 @@
"@zextras/carbonio-design-system": "^8.0.0",
"@zextras/carbonio-shell-ui": "devel",
"@zextras/carbonio-ui-preview": "^3.0.0",
"asn1js": "^3.0.5",
"axios": "^1.6.7",
"babel-jest": "^29.4.3",
"core-js": "^3.36.0",
Expand All @@ -94,6 +95,8 @@
"jest-junit": "^15.0.0",
"lodash": "^4.17.21",
"moment": "^2.29.4",
"node-forge": "^1.3.1",
"pkijs": "^3.2.4",
"prop-types": "^15.8.1",
"react": "^18.3.1",
"react-colorful": "^5.6.1",
Expand Down
5 changes: 4 additions & 1 deletion src/store/actions/send-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getParticipantsFromMessage } from '../../helpers/messages';
import { MailMessage, SendMsgResult, SendMsgWithSmartLinksResponse } from '../../types';
import type { SaveDraftRequest, SaveDraftResponse, SendMsgParameters } from '../../types';
import { generateMailRequest } from '../editor-slice-utils';
import { getCertificate } from '../zustand/certificates/certificate';
import { createSoapSendMsgRequestFromEditor } from '../zustand/editor/editor-transformations';

export const sendMsg = createAsyncThunk<any, { msg: MailMessage }>(
Expand Down Expand Up @@ -69,14 +70,16 @@ export const sendMsgFromEditor = createAsyncThunk<SendMsgResult, SendMsgParamete
const msg = createSoapSendMsgRequestFromEditor(editor);

const identity = getIdentityDescriptor(editor.identityId);
const certificate = getCertificate({ accountId: identity?.fromAddress ?? '' });

let resp: SendMsgWithSmartLinksResponse;
try {
resp = await soapFetch<SaveDraftRequest, SaveDraftResponse>(
'SendMsg',
{
_jsns: 'urn:zimbraMail',
m: msg
m: msg,
...(editor.isSmimeSign ? { sign: true, ...certificate } : {})
},
identity?.ownerAccount ?? undefined
);
Expand Down
9 changes: 9 additions & 0 deletions src/store/zustand/certificates/certificate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* SPDX-FileCopyrightText: 2023 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Certificate, useCertificatesStore } from './store';

export const getCertificate = ({ accountId }: { accountId: string }): Certificate | null =>
useCertificatesStore.getState()?.certificates?.[accountId] ?? null;
35 changes: 35 additions & 0 deletions src/store/zustand/certificates/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { create } from 'zustand';

export type Certificate = {
privateKey: string;
certificate: string;
caCertificate: string;
};

type CertificatesState = {
certificates: Record<string, Certificate>;
addCertificate: (accountId: string, certificate: Certificate) => void;
removeCertificate: (accountId: string) => void;
getCertificate: (accountId: string) => Certificate | undefined;
};
export const useCertificatesStore = create<CertificatesState>((set, get) => ({
certificates: {},
addCertificate: (accountId: string, certificate: Certificate): void =>
set((state) => ({
certificates: {
...state.certificates,
[accountId]: certificate
}
})),
removeCertificate: (accountId: string): void =>
set((state) => {
const { [accountId]: _, ...rest } = state.certificates;
return { certificates: rest };
}),
getCertificate: (accountId: string): Certificate | undefined => get().certificates[accountId]
}));
26 changes: 26 additions & 0 deletions src/store/zustand/editor/hooks/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,29 @@ export const useEditorSignatureId = (
[editorId, debouncedSaveDraft, setter, value]
);
};

/**
* Returns reactive reference to the isUrgent value and to its setter
* @param id
*/
export const useEditorIsSmimeSign = (
id: MailsEditorV2['id']
): {
isSmimeSign: MailsEditorV2['isSmimeSign'];
setIsSmimeSign: (isSmimeSign: MailsEditorV2['isSmimeSign']) => void;
} => {
const { debouncedSaveDraft } = useSaveDraftFromEditor();
const value = useEditorsStore((state) => state.editors[id].isSmimeSign);
const setter = useEditorsStore((state) => state.setIsSmimeSign);

return useMemo(
() => ({
isSmimeSign: value,
setIsSmimeSign: (val: MailsEditorV2['isSmimeSign']): void => {
setter(id, val);
debouncedSaveDraft(id);
}
}),
[id, debouncedSaveDraft, setter, value]
);
};
9 changes: 9 additions & 0 deletions src/store/zustand/editor/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,14 @@ export const useEditorsStore = create<EditorsStateTypeV2>()((set) => ({
}
})
);
},
setIsSmimeSign: (id: MailsEditorV2['id'], value: MailsEditorV2['isSmimeSign']): void => {
set(
produce((state: EditorsStateTypeV2) => {
if (state?.editors?.[id]) {
state.editors[id].isSmimeSign = value;
}
})
);
}
}));
2 changes: 2 additions & 0 deletions src/types/editor/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export type MailsEditorV2 = {
size: number;
// the sum of the size of the attachments requiring smart link conversion
totalSmartLinksSize: number;
// flag for the S/MIME request
isSmimeSign?: boolean;
};

type IdentityType = {
Expand Down
1 change: 1 addition & 0 deletions src/types/state/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type EditorsStateTypeV2 = {
setMessagesStoreDispatch: (id: MailsEditorV2['id'], dispatch: AppDispatch) => void;
toggleSmartLink: (id: MailsEditorV2['id'], partName: string) => void;
setSignatureId: (id: MailsEditorV2['id'], signId: MailsEditorV2['signatureId']) => void;
setIsSmimeSign: (id: MailsEditorV2['id'], isSmimeSign: MailsEditorV2['isSmimeSign']) => void;
};

export type MsgStateType = {
Expand Down
Loading