feat: SSH Authentication Keys v2 (#113)

* feat: add SSH authentication keys v2 for Git operations

Add comprehensive SSH key authentication support for Git operations including:
- SSH host key verification with user approval flow
- Encrypted SSH key storage with passphrase support
- SSH connection testing endpoint
- Real-time host key change warnings

Backend changes:
- New SSHHostKeyHandler for managing host key verification via IPC
- Add passphrase handler for locked SSH keys
- Add SSH key manager utilities (key writing, validation, cleanup)
- Add crypto utilities for secure key/secret encryption
- Add SSH validation utilities for key format checking
- Add SSH routes for host key response handling
- Enhance GitAuthService with SSH setup and environment methods
- Update GitService to handle SSH authentication for push/fetch/pull
- Update OpenCode server to support SSH keys with SSH config generation
- Add database migration for trusted_ssh_hosts table
- Add settings endpoint for SSH connection testing

Frontend changes:
- Add SSHHostKeyDialog for host key verification UI
- Add PassphraseModal for SSH key passphrase input
- Update GitCredentialDialog with SSH key type and testing
- Update GitSettings to manage SSH keys
- Add SSH API client and hooks (useSSH, usePassphraseHandler)

Shared changes:
- Add SSH schemas and types

Test coverage:
- Integration tests for SSH flow
- IPC handler tests for host key and passphrase
- SSH route tests
- SSH utility validation tests
- Updated existing tests for SSH-aware changes
This commit is contained in:
Chris Scott authored and GitHub committed 2026-02-08 00:06:44 -05:00
1 parent 3d5ee463d7
commit 414f39b809
54 files changed
+4051 -214

No files matched your search

@@ -1,9 +1,11 @@
import { useState, useEffect } from 'react'
import { Loader2 } from 'lucide-react'
import { Loader2, Key, Lock, AlertTriangle } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Textarea } from '@/components/ui/textarea'
import { Alert } from '@/components/ui/alert'
import { showToast } from '@/lib/toast'
import type { GitCredential } from '@/api/types/settings'
@@ -19,10 +21,16 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is
const [formData, setFormData] = useState<GitCredential>({
name: '',
host: '',
type: 'pat',
token: '',
username: '',
sshPrivateKey: '',
passphrase: '',
})
const [tokenEdited, setTokenEdited] = useState(false)
const [isTesting, setIsTesting] = useState(false)
const [showPassphraseInput, setShowPassphraseInput] = useState(false)
const [testPassphrase, setTestPassphrase] = useState('')
const maskToken = (token: string) => {
if (!token) return ''
@@ -33,13 +41,16 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is
useEffect(() => {
if (open) {
setTokenEdited(false)
setShowPassphraseInput(false)
setTestPassphrase('')
if (credential) {
setFormData({
...credential,
token: ''
sshPrivateKey: '',
token: credential.type === 'pat' ? '' : credential.token
})
} else {
setFormData({ name: '', host: 'https://github.com', token: '', username: '' })
setFormData({ name: '', host: '', type: 'pat', token: '', username: '', sshPrivateKey: '', passphrase: '' })
}
}
}, [open, credential])
@@ -47,35 +58,77 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is
const handleSubmit = async (event?: React.MouseEvent) => {
event?.preventDefault()
event?.stopPropagation()
const isEditing = !!credential
const tokenRequired = isEditing ? tokenEdited : true
if (!formData.name.trim() || !formData.host.trim() || (tokenRequired && !formData.token.trim())) {
showToast.error('Please fill in all required fields')
if (!formData.name.trim() || !formData.host.trim()) {
showToast.error('Name and host are required')
return
}
if (formData.type === 'pat') {
if (!formData.token?.trim()) {
showToast.error('Token is required for PAT type')
return
}
} else if (formData.type === 'ssh') {
if (!formData.sshPrivateKey?.trim()) {
showToast.error('SSH key is required for SSH type')
return
}
}
try {
const dataToSave: GitCredential = {
...formData,
token: (isEditing && !tokenEdited) ? credential.token : formData.token
hasPassphrase: formData.type === 'ssh' ? Boolean(formData.passphrase?.trim()) : false
}
if (formData.type === 'pat' && credential?.token && !tokenEdited) {
dataToSave.token = credential.token
}
await onSave(dataToSave)
setFormData({ name: '', host: '', token: '', username: '' })
setFormData({ name: '', host: '', type: 'pat', token: '', username: '', sshPrivateKey: '', passphrase: '' })
onOpenChange(false)
} catch {
showToast.error('Failed to save credential')
}
}
const handleTestConnection = async () => {
if (!formData.sshPrivateKey?.trim()) {
showToast.error('Please enter an SSH key first')
return
}
setIsTesting(true)
try {
const host = formData.host.replace(/^https?:\/\//, '').replace(/\/$/, '')
const settingsApi = (await import('@/api/settings')).settingsApi
const result = await settingsApi.testSSHConnection(
host,
formData.sshPrivateKey,
testPassphrase || undefined
)
if (result.success) {
showToast.success(result.message)
} else {
showToast.error(result.message)
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to test SSH connection'
showToast.error(message)
} finally {
setIsTesting(false)
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-md">
<DialogContent className="max-w-lg z-[150]" overlayClassName="z-[140]">
<DialogHeader>
<DialogTitle>{credential ? 'Edit Git Credential' : 'Add Git Credential'}</DialogTitle>
</DialogHeader>
<form onSubmit={(e) => { e.preventDefault(); handleSubmit(); }} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="cred-name">Name *</Label>
@@ -85,52 +138,166 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is
value={formData.name}
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
disabled={isSaving}
autoComplete="off"
/>
</div>
<div className="space-y-2">
<Label htmlFor="cred-host">Host URL *</Label>
<Label htmlFor="cred-type">Authentication Type</Label>
<div className="flex gap-2">
<Button
type="button"
variant={formData.type === 'pat' ? 'default' : 'outline'}
onClick={() => setFormData({ ...formData, type: 'pat', sshPrivateKey: '', passphrase: '' })}
disabled={isSaving}
className="flex-1"
>
<Key className="h-4 w-4 mr-2" />
PAT
</Button>
<Button
type="button"
variant={formData.type === 'ssh' ? 'default' : 'outline'}
onClick={() => setFormData({ ...formData, type: 'ssh', token: '' })}
disabled={isSaving}
className="flex-1"
>
<Lock className="h-4 w-4 mr-2" />
SSH Key
</Button>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="cred-host">Host *</Label>
<Input
id="cred-host"
placeholder="https://github.com/"
placeholder="github.com or git@github.com"
value={formData.host}
onChange={(e) => setFormData({ ...formData, host: e.target.value })}
disabled={isSaving}
autoComplete="off"
/>
</div>
<div className="space-y-2">
<Label htmlFor="cred-token">
Access Token {credential && !tokenEdited ? '(unchanged)' : '*'}
</Label>
<Input
id="cred-token"
type="password"
placeholder={credential ? maskToken(credential.token) : 'Personal access token'}
value={formData.token}
onChange={(e) => {
setTokenEdited(true)
setFormData({ ...formData, token: e.target.value })
}}
disabled={isSaving}
/>
{credential && !tokenEdited && (
<p className="text-xs text-muted-foreground">
Leave empty to keep existing token
</p>
)}
</div>
{formData.type === 'pat' ? (
<>
<div className="space-y-2">
<Label htmlFor="cred-token">
Access Token {credential?.token && !tokenEdited ? '(unchanged)' : '*'}
</Label>
<Input
id="cred-token"
type="password"
placeholder={credential?.token ? maskToken(credential.token) : 'Personal access token'}
value={formData.token || ''}
onChange={(e) => {
setTokenEdited(true)
setFormData({ ...formData, token: e.target.value })
}}
disabled={isSaving}
autoComplete="new-password"
/>
{credential?.token && !tokenEdited && (
<p className="text-xs text-muted-foreground">
Leave empty to keep existing token
</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="cred-username">Username (optional)</Label>
<Input
id="cred-username"
placeholder="Auto-detected if empty"
value={formData.username || ''}
onChange={(e) => setFormData({ ...formData, username: e.target.value })}
disabled={isSaving}
/>
</div>
<div className="space-y-2">
<Label htmlFor="cred-pat-username">Username (optional)</Label>
<Input
id="cred-pat-username"
placeholder="Auto-detected if empty"
value={formData.username || ''}
onChange={(e) => setFormData({ ...formData, username: e.target.value })}
disabled={isSaving}
autoComplete="off"
/>
</div>
</>
) : (
<>
<div className="space-y-2">
<Label htmlFor="cred-ssh-key">SSH Private Key *</Label>
<Textarea
id="cred-ssh-key"
placeholder="-----BEGIN OPENSSH PRIVATE KEY-----"
value={formData.sshPrivateKey || ''}
onChange={(e) => setFormData({ ...formData, sshPrivateKey: e.target.value })}
disabled={isSaving}
rows={8}
className="font-mono"
/>
<p className="text-xs text-muted-foreground">
Paste your private key content here
</p>
</div>
<div className="space-y-2">
<Button
type="button"
variant="outline"
onClick={() => setShowPassphraseInput(!showPassphraseInput)}
disabled={isSaving}
className="w-full"
>
{showPassphraseInput ? 'Remove' : 'Add'} Passphrase
</Button>
</div>
{showPassphraseInput && (
<div className="space-y-2">
<Label htmlFor="cred-passphrase">Passphrase</Label>
<Input
id="cred-passphrase"
type="password"
placeholder="Enter passphrase for SSH key"
value={formData.passphrase || ''}
onChange={(e) => setFormData({ ...formData, passphrase: e.target.value })}
disabled={isSaving}
autoComplete="new-password"
/>
<p className="text-xs text-muted-foreground">
This passphrase will be required for each git operation
</p>
</div>
)}
<div className="space-y-2">
<Label htmlFor="test-passphrase">Passphrase for Test (if protected)</Label>
<Input
id="test-passphrase"
type="password"
placeholder="Enter passphrase to test connection"
value={testPassphrase}
onChange={(e) => setTestPassphrase(e.target.value)}
disabled={isTesting || isSaving}
autoComplete="new-password"
/>
</div>
<Button
type="button"
variant="outline"
onClick={handleTestConnection}
disabled={isTesting || isSaving || !formData.sshPrivateKey?.trim()}
className="w-full"
>
{isTesting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Test Connection
</Button>
<Alert>
<AlertTriangle className="h-4 w-4" />
<p className="text-sm font-medium">Security Notice</p>
<p className="text-xs mt-1">
Your private key will be encrypted at rest. Never share it with anyone.
</p>
</Alert>
</>
)}
<div className="flex justify-end gap-2 pt-4">
<Button
@@ -143,7 +310,9 @@ export function GitCredentialDialog({ open, onOpenChange, onSave, credential, is
</Button>
<Button
type="submit"
disabled={isSaving || !formData.name.trim() || !formData.host.trim() || (!credential && !formData.token.trim())}
disabled={isSaving || !formData.name.trim() || !formData.host.trim() ||
(formData.type === 'pat' && !formData.token?.trim()) ||
(formData.type === 'ssh' && !formData.sshPrivateKey?.trim())}
>
{isSaving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
{credential ? 'Update' : 'Add'}
@@ -45,6 +45,18 @@ export function GitSettings() {
setIsCredentialDialogOpen(true)
}
const handleEditClick = (e: React.MouseEvent, index: number) => {
e.preventDefault()
e.stopPropagation()
openEditCredentialDialog(index)
}
const handleDeleteClick = (e: React.MouseEvent, index: number) => {
e.preventDefault()
e.stopPropagation()
removeCredential(index)
}
const saveCredential = async (credential: GitCredential) => {
let newCredentials: GitCredential[]
@@ -216,10 +228,11 @@ export function GitSettings() {
) : (
<div className="border border-border rounded-lg overflow-hidden">
<table className="w-full text-sm">
<thead className="bg-muted/50">
<thead className="bg-muted/50">
<tr>
<th className="px-3 py-2 text-left font-medium text-muted-foreground">Name</th>
<th className="px-3 py-2 text-left font-medium text-muted-foreground hidden sm:table-cell">Host</th>
<th className="px-3 py-2 text-left font-medium text-muted-foreground hidden sm:table-cell">Type</th>
<th className="px-3 py-2 text-right font-medium text-muted-foreground">Actions</th>
</tr>
</thead>
@@ -235,32 +248,37 @@ export function GitSettings() {
<td className="px-3 py-2 text-muted-foreground hidden sm:table-cell">
{cred.host}
</td>
<td className="px-3 py-2">
<div className="flex items-center justify-end gap-1">
<td className="px-3 py-2 text-muted-foreground hidden sm:table-cell">
<span className="text-xs px-1.5 py-0.5 rounded bg-muted">
{cred.type === 'ssh' ? 'SSH' : 'PAT'}
</span>
</td>
<td className="px-3 py-2">
<div className="flex items-center justify-end gap-1">
<Button
type="button"
variant="ghost"
size="sm"
className="h-7 w-7 p-0"
onClick={(e) => handleEditClick(e, index)}
disabled={isSaving}
title="Edit"
>
<Pencil className="h-3.5 w-3.5" />
</Button>
<Button
type="button"
variant="ghost"
size="sm"
className="h-7 w-7 p-0"
onClick={() => openEditCredentialDialog(index)}
className="h-7 w-7 p-0 text-destructive hover:text-destructive"
onClick={(e) => handleDeleteClick(e, index)}
disabled={isSaving}
title="Edit"
title="Delete"
>
<Pencil className="h-3.5 w-3.5" />
<Trash2 className="h-3.5 w-3.5" />
</Button>
<Button
type="button"
variant="ghost"
size="sm"
className="h-7 w-7 p-0 text-destructive hover:text-destructive"
onClick={() => removeCredential(index)}
disabled={isSaving}
title="Delete"
>
<Trash2 className="h-3.5 w-3.5" />
</Button>
</div>
</td>
</div>
</td>
</tr>
))}
</tbody>
@@ -41,7 +41,10 @@ export function SettingsDialog() {
useEffect(() => {
if (!isOpen) return
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') close()
if (e.key === 'Escape') {
e.stopPropagation()
close()
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
@@ -116,14 +119,14 @@ export function SettingsDialog() {
<div className="flex-1 overflow-y-auto">
<div className="px-6 pb-6">
<TabsContent value="account" className="mt-0"><AccountSettings /></TabsContent>
<TabsContent value="general" className="mt-0"><GeneralSettings /></TabsContent>
<TabsContent value="notifications" className="mt-0"><NotificationSettings /></TabsContent>
<TabsContent value="voice" className="mt-0"><VoiceSettings /></TabsContent>
<TabsContent value="git" className="mt-0"><GitSettings /></TabsContent>
<TabsContent value="shortcuts" className="mt-0"><KeyboardShortcuts /></TabsContent>
<TabsContent value="opencode" className="mt-0"><OpenCodeConfigManager /></TabsContent>
<TabsContent value="providers" className="mt-0"><ProviderSettings /></TabsContent>
<TabsContent key="account" value="account" className="mt-0"><AccountSettings /></TabsContent>
<TabsContent key="general" value="general" className="mt-0"><GeneralSettings /></TabsContent>
<TabsContent key="notifications" value="notifications" className="mt-0"><NotificationSettings /></TabsContent>
<TabsContent key="voice" value="voice" className="mt-0"><VoiceSettings /></TabsContent>
<TabsContent key="git" value="git" className="mt-0"><GitSettings /></TabsContent>
<TabsContent key="shortcuts" value="shortcuts" className="mt-0"><KeyboardShortcuts /></TabsContent>
<TabsContent key="opencode" value="opencode" className="mt-0"><OpenCodeConfigManager /></TabsContent>
<TabsContent key="providers" value="providers" className="mt-0"><ProviderSettings /></TabsContent>
</div>
</div>
</Tabs>
@@ -156,41 +159,41 @@ export function SettingsDialog() {
</Button>
</div>
<div className="flex-1 min-h-0 overflow-y-auto p-4 pb-32">
{mobileView === 'menu' && (
<div className="space-y-3">
{menuItems.map((item) => (
<button
key={item.id}
onClick={() => {
setMobileView(item.id as SettingsView)
setActiveTab(item.id as SettingsView)
}}
className="w-full bg-gradient-to-br from-card to-card-hover border border-border rounded-xl p-4 hover:border-border transition-all duration-200 text-left"
>
<div className="flex items-center gap-4">
<div className="p-3 bg-accent rounded-lg">
<item.icon className="w-6 h-6 text-blue-600 dark:text-blue-400" />
</div>
<div className="flex-1 min-w-0">
<h3 className="font-semibold text-foreground mb-1">{item.label}</h3>
<p className="text-sm text-muted-foreground">{item.description}</p>
</div>
</div>
</button>
))}
</div>
)}
<div className="flex-1 min-h-0 overflow-y-auto p-4 pb-32">
{mobileView === 'menu' && (
<div className="space-y-3">
{menuItems.map((item) => (
<button
key={item.id}
onClick={() => {
setMobileView(item.id as SettingsView)
setActiveTab(item.id as SettingsView)
}}
className="w-full bg-gradient-to-br from-card to-card-hover border border-border rounded-xl p-4 hover:border-border transition-all duration-200 text-left"
>
<div className="flex items-center gap-4">
<div className="p-3 bg-accent rounded-lg">
<item.icon className="w-6 h-6 text-blue-600 dark:text-blue-400" />
</div>
<div className="flex-1 min-w-0">
<h3 className="font-semibold text-foreground mb-1">{item.label}</h3>
<p className="text-sm text-muted-foreground">{item.description}</p>
</div>
</div>
</button>
))}
</div>
)}
{mobileView === 'account' && <AccountSettings />}
{mobileView === 'general' && <GeneralSettings />}
{mobileView === 'notifications' && <NotificationSettings />}
{mobileView === 'voice' && <VoiceSettings />}
{mobileView === 'git' && <GitSettings />}
{mobileView === 'shortcuts' && <KeyboardShortcuts />}
{mobileView === 'opencode' && <OpenCodeConfigManager />}
{mobileView === 'providers' && <ProviderSettings />}
</div>
{mobileView === 'account' && <div key="account"><AccountSettings /></div>}
{mobileView === 'general' && <div key="general"><GeneralSettings /></div>}
{mobileView === 'notifications' && <div key="notifications"><NotificationSettings /></div>}
{mobileView === 'voice' && <div key="voice"><VoiceSettings /></div>}
{mobileView === 'git' && <div key="git"><GitSettings /></div>}
{mobileView === 'shortcuts' && <div key="shortcuts"><KeyboardShortcuts /></div>}
{mobileView === 'opencode' && <div key="opencode"><OpenCodeConfigManager /></div>}
{mobileView === 'providers' && <div key="providers"><ProviderSettings /></div>}
</div>
</div>
</DialogContent>
</Dialog>