/** * Export Key Panel - Export SSH key to remote host */ import { ChevronRight, Info } from 'lucide-react'; import React, { useState } from 'react'; import { useKeychainBackend } from '../../application/state/useKeychainBackend'; import { useI18n } from '../../application/i18n/I18nProvider'; import { resolveHostAuth } from '../../domain/sshAuth'; import { cn } from '../../lib/utils'; import { Host, Identity, SSHKey } from '../../types'; import { Button } from '../ui/button'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '../ui/collapsible'; import { Input } from '../ui/input'; import { Label } from '../ui/label'; import { Textarea } from '../ui/textarea'; import { toast } from '../ui/toast'; import { getKeyIcon, getKeyTypeDisplay, isMacOS } from './utils'; interface ExportKeyPanelProps { keyItem: SSHKey; _hosts: Host[]; // Reserved for future inline host list/validation keys: SSHKey[]; identities: Identity[]; exportHost: Host | null; _setExportHost: (host: Host | null) => void; // Host selection handled by onShowHostSelector callback onShowHostSelector: () => void; onSaveHost?: (host: Host) => void; onClose: () => void; } const DEFAULT_EXPORT_SCRIPT = `DIR="$HOME/$1" FILE="$DIR/$2" if [ ! -d "$DIR" ]; then mkdir -p "$DIR" chmod 700 "$DIR" fi if [ ! -f "$FILE" ]; then touch "$FILE" chmod 600 "$FILE" fi echo $3 >> "$FILE"`; export const ExportKeyPanel: React.FC = ({ keyItem, _hosts, // Reserved for future inline host list/validation keys, identities, exportHost, _setExportHost, // Host selection handled by onShowHostSelector callback onShowHostSelector, onSaveHost, onClose, }) => { const { t } = useI18n(); const { execCommand } = useKeychainBackend(); const [exportLocation, setExportLocation] = useState('.ssh'); const [exportFilename, setExportFilename] = useState('authorized_keys'); const [exportAdvancedOpen, setExportAdvancedOpen] = useState(false); const [exportScript, setExportScript] = useState(DEFAULT_EXPORT_SCRIPT); const [isExporting, setIsExporting] = useState(false); const isMac = isMacOS(); const handleExport = async () => { if (!exportHost || !keyItem.publicKey) return; setIsExporting(true); try { const exportAuth = resolveHostAuth({ host: exportHost, keys, identities }); // Check for authentication method if (!exportAuth.password && !exportAuth.key?.privateKey) { throw new Error(t('keychain.export.missingCredentials')); } const hostPrivateKey = exportAuth.key?.privateKey; // Escape the public key for shell const escapedPublicKey = keyItem.publicKey.replace(/'/g, "'\\''"); // Build the command by replacing $1, $2, $3 const scriptWithVars = exportScript .replace(/\$1/g, exportLocation) .replace(/\$2/g, exportFilename) .replace(/\$3/g, `'${escapedPublicKey}'`); const command = scriptWithVars; // Execute via SSH const result = await execCommand({ hostname: exportHost.hostname, username: exportAuth.username, port: exportHost.port || 22, password: exportAuth.password, privateKey: hostPrivateKey, command, timeout: 30000, }); // Check result const exitCode = result?.code; const hasError = result?.stderr?.trim(); if (exitCode === 0 || (exitCode == null && !hasError)) { // Update host to use this key for authentication if (onSaveHost) { const updatedHost: Host = { ...exportHost, identityFileId: keyItem.id, authMethod: 'key', }; onSaveHost(updatedHost); } toast.success( t('keychain.export.successMessage', { host: exportHost.label }), t('keychain.export.successTitle'), ); onClose(); } else { const errorMsg = hasError || result?.stdout?.trim() || `Command exited with code ${exitCode}`; toast.error( t('keychain.export.failedMessage', { error: errorMsg }), t('keychain.export.failedTitle'), ); } } catch (err) { const message = err instanceof Error ? err.message : String(err); toast.error( t('keychain.export.failedGeneric', { message }), t('keychain.export.failedTitle'), ); } finally { setIsExporting(false); } }; return ( <> {/* Key info card */}
{getKeyIcon(keyItem)}

{keyItem.label}

{t('auth.keyType', { type: getKeyTypeDisplay(keyItem, isMac) })}

{/* Export to field */}
{/* Location field */}
setExportLocation(e.target.value)} placeholder=".ssh" />
{/* Filename field */}
setExportFilename(e.target.value)} placeholder="authorized_keys" />
{/* Info note */}

{t('keychain.export.note.supportsOnly')}{' '} UNIX{' '} {t('keychain.export.note.systems')}{' '} {t('keychain.export.note.use')}{' '} {t('keychain.export.advanced')}{' '} {t('keychain.export.note.customize')}

{/* Advanced collapsible */}