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

+68 -3
View File
@@ -1,8 +1,13 @@
export interface GitCredential {
name: string
host: string
token: string
type: 'pat' | 'ssh'
token?: string
sshPrivateKey?: string
sshPrivateKeyEncrypted?: string
hasPassphrase?: boolean
username?: string
passphrase?: string
}
export function isGitHubHttpsUrl(repoUrl: string): boolean {
@@ -31,6 +36,29 @@ export function getDefaultUsername(host: string): string {
}
}
export function isSSHUrl(url: string): boolean {
return url.startsWith('git@') || url.startsWith('ssh://')
}
export function extractHostFromSSHUrl(url: string): string | null {
if (url.startsWith('git@')) {
const match = url.match(/^git@([^:]+):/)
const host = match?.[1]
return host || null
}
if (url.startsWith('ssh://')) {
try {
const parsed = new URL(url)
const hostname = parsed.hostname ?? ''
const port = parsed.port ?? ''
return port ? `${hostname}:${port}` : parsed.hostname || null
} catch {
return null
}
}
return null
}
export function normalizeHost(host: string): string {
if (!host.endsWith('/')) {
return `${host}/`
@@ -70,7 +98,7 @@ export function createGitEnv(credentials: GitCredential[]): Record<string, strin
}
export function createGitHubGitEnv(gitToken: string): Record<string, string> {
return createGitEnv([{ name: 'GitHub', host: 'https://github.com/', token: gitToken }])
return createGitEnv([{ name: 'GitHub', host: 'https://github.com/', token: gitToken, type: 'pat' }])
}
export function findGitHubCredential(credentials: GitCredential[]): GitCredential | null {
@@ -97,6 +125,43 @@ export function getCredentialForHost(credentials: GitCredential[], host: string)
})
}
export function getSSHCredentialsForHost(credentials: GitCredential[], host: string): GitCredential[] {
return credentials.filter(cred => {
if (cred.type !== 'ssh') return false
const credHost = cred.host.toLowerCase()
const targetHost = host.toLowerCase()
if (credHost === targetHost) {
return true
}
try {
const parsedCredHost = new URL(credHost)
const credHostname = parsedCredHost.hostname.toLowerCase()
const credPort = parsedCredHost.port || (parsedCredHost.protocol.includes('ssh') ? 22 : null)
const normalizedCredHost = credPort ? `${credHostname}:${credPort}` : credHostname
const parsedTargetHost = new URL(`ssh://dummy@${targetHost}`)
const targetHostname = parsedTargetHost.hostname.toLowerCase()
const targetPort = parsedTargetHost.port || 22
const normalizedTargetHost = targetPort !== '' ? `${targetHostname}:${targetPort}` : targetHostname
return normalizedCredHost === normalizedTargetHost
} catch {
if (credHost.includes(':')) {
const [credHostname, credPort] = credHost.split(':')
if (targetHost.includes(':')) {
const [targetHostname, targetPort] = targetHost.split(':')
return credHostname === targetHostname && credPort === targetPort
}
return credHostname === targetHost
}
return credHost === targetHost
}
})
}
export interface GitHubUserInfo {
name: string | null
email: string
@@ -117,7 +182,7 @@ export async function resolveGitIdentity(
}
const githubCred = findGitHubCredential(credentials)
if (githubCred) {
if (githubCred && githubCred.token) {
const githubUser = await fetchGitHubUserInfo(githubCred.token)
if (githubUser) {
return {