From a400ff703b68dc2e3e43245b7d2fae0a6e2de7b2 Mon Sep 17 00:00:00 2001 From: David Edler Date: Wed, 10 May 2023 11:47:48 +0200 Subject: [PATCH] feat(cert) ask for certificate password on creation #331 --- .../certificates/CertificateGenerate.tsx | 23 ++++- src/pages/certificates/PasswordModal.tsx | 83 +++++++++++++++++++ src/util/certificate.tsx | 4 +- 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/pages/certificates/PasswordModal.tsx diff --git a/src/pages/certificates/CertificateGenerate.tsx b/src/pages/certificates/CertificateGenerate.tsx index 1120e9a5a1..cf4bb9712b 100644 --- a/src/pages/certificates/CertificateGenerate.tsx +++ b/src/pages/certificates/CertificateGenerate.tsx @@ -5,6 +5,7 @@ import BrowserImport from "pages/certificates/BrowserImport"; import { Navigate } from "react-router-dom"; import { useAuth } from "context/auth"; import Loader from "components/Loader"; +import PasswordModal from "pages/certificates/PasswordModal"; interface Certs { crt: string; @@ -13,6 +14,7 @@ interface Certs { const CertificateGenerate: FC = () => { const [isGenerating, setGenerating] = useState(false); + const [isModalOpen, setModalOpen] = useState(false); const [certs, setCerts] = useState(null); const { isAuthenticated, isAuthLoading } = useAuth(); @@ -24,11 +26,20 @@ const CertificateGenerate: FC = () => { return ; } - const createCert = () => { + const closeModal = () => { + setModalOpen(false); + }; + + const openModal = () => { + setModalOpen(true); + }; + + const createCert = (password: string) => { + closeModal(); setGenerating(true); // using timeout to avoid compute heavy generation in the main ui thread setTimeout(() => { - const certs = generateCert(); + const certs = generateCert(password); setCerts(certs); setGenerating(false); }, 10); @@ -76,8 +87,14 @@ const CertificateGenerate: FC = () => { + {isModalOpen && ( + + )} + + + } + > +

Protect your certificate by adding a password.

+ + + + ); +}; + +export default PasswordModal; diff --git a/src/util/certificate.tsx b/src/util/certificate.tsx index 02bd88e88b..337f0308e7 100644 --- a/src/util/certificate.tsx +++ b/src/util/certificate.tsx @@ -25,7 +25,7 @@ const details = [ }, ]; -export const generateCert = () => { +export const generateCert = (password: string) => { const validDays = 1000; const keys = forge.pki.rsa.generateKeyPair(2048); @@ -44,7 +44,7 @@ export const generateCert = () => { const crt = forge.pki.certificateToPem(cert); - const asn1 = forge.pkcs12.toPkcs12Asn1(keys.privateKey, [cert], "", { + const asn1 = forge.pkcs12.toPkcs12Asn1(keys.privateKey, [cert], password, { generateLocalKeyId: true, friendlyName: "LXD-UI", });