diff --git a/app/client/modules/repositories/components/repository-forms/sftp-repository-form.tsx b/app/client/modules/repositories/components/repository-forms/sftp-repository-form.tsx index 20301600..1bd18260 100644 --- a/app/client/modules/repositories/components/repository-forms/sftp-repository-form.tsx +++ b/app/client/modules/repositories/components/repository-forms/sftp-repository-form.tsx @@ -13,7 +13,7 @@ import { Textarea } from "../../../../components/ui/textarea"; import { Switch } from "../../../../components/ui/switch"; import type { RepositoryFormValues } from "../create-repository-form"; import { Button } from "~/client/components/ui/button"; -import { generatePrivateKeyPem } from "~/utils/ssh"; +import { generatePrivateKeyPem, generateSshKeyPairPem } from "~/utils/ssh"; type Props = { form: UseFormReturn; @@ -23,25 +23,43 @@ export const SftpRepositoryForm = ({ form }: Props) => { const skipHostKeyCheck = useWatch({ control: form.control, name: "skipHostKeyCheck" }); const [isGeneratingKey, setIsGeneratingKey] = useState(false); const [keyGenerationError, setKeyGenerationError] = useState(null); + const [generatedPublicKey, setGeneratedPublicKey] = useState(null); + const [copyPublicKeyMessage, setCopyPublicKeyMessage] = useState(null); const handleGenerateKey = async () => { setIsGeneratingKey(true); setKeyGenerationError(null); try { - const privateKey = await generatePrivateKeyPem(); - form.setValue("privateKey", privateKey, { + const { privateKeyPem, publicKeyPem } = await generateSshKeyPairPem(); + form.setValue("privateKey", privateKeyPem, { shouldDirty: true, shouldTouch: true, shouldValidate: true, }); + setGeneratedPublicKey(publicKeyPem); } catch { + setGeneratedPublicKey(null); setKeyGenerationError("Could not generate SSH key in this browser."); } finally { setIsGeneratingKey(false); } }; + const handleCopyPublicKey = async () => { + setCopyPublicKeyMessage(null); + if (!generatedPublicKey) { + setKeyGenerationError("Generate a key first to copy its public key."); + return; + } + try { + await navigator.clipboard.writeText(generatedPublicKey); + setCopyPublicKeyMessage("Public key copied to clipboard."); + } catch { + setKeyGenerationError("Could not copy public key to clipboard."); + } + }; + return ( <> { Paste the contents of your SSH private key. - +
+ + +
The key is generated privately in your browser. Don't forget to save it! {keyGenerationError && {keyGenerationError}} + {copyPublicKeyMessage && {copyPublicKeyMessage}} )} />