|
| 1 | +import { AlertTriangle, Download, KeySquare, Loader2, Sparkles, X } from 'lucide-react'; |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import { api, type SshKey } from '../lib/api'; |
| 4 | +import { ed25519Supported, generateEd25519KeyPair, type GeneratedKeyPair } from '../lib/sshkeygen'; |
| 5 | +import { Alert } from './Alert'; |
| 6 | +import { CodeBlock } from './CodeBlock'; |
| 7 | + |
| 8 | +function download(filename: string, contents: string) { |
| 9 | + const blob = new Blob([contents], { type: 'application/octet-stream' }); |
| 10 | + const url = URL.createObjectURL(blob); |
| 11 | + const a = document.createElement('a'); |
| 12 | + a.href = url; |
| 13 | + a.download = filename; |
| 14 | + document.body.appendChild(a); |
| 15 | + a.click(); |
| 16 | + a.remove(); |
| 17 | + URL.revokeObjectURL(url); |
| 18 | +} |
| 19 | + |
| 20 | +export function GenerateKey({ onAdded }: { onAdded: (key: SshKey) => void }) { |
| 21 | + const [label, setLabel] = useState(''); |
| 22 | + const [busy, setBusy] = useState(false); |
| 23 | + const [error, setError] = useState<string | null>(null); |
| 24 | + const [supported, setSupported] = useState<boolean | null>(null); |
| 25 | + const [result, setResult] = useState<{ pair: GeneratedKeyPair; stored: SshKey } | null>(null); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + let active = true; |
| 29 | + void ed25519Supported().then((ok) => active && setSupported(ok)); |
| 30 | + return () => { |
| 31 | + active = false; |
| 32 | + }; |
| 33 | + }, []); |
| 34 | + |
| 35 | + const generate = async () => { |
| 36 | + setError(null); |
| 37 | + setBusy(true); |
| 38 | + try { |
| 39 | + const pair = await generateEd25519KeyPair(label.trim()); |
| 40 | + // Only the public half is uploaded; the private key stays in this tab. |
| 41 | + const stored = await api.addKey(label.trim(), pair.publicKey); |
| 42 | + onAdded(stored); |
| 43 | + setResult({ pair, stored }); |
| 44 | + setLabel(''); |
| 45 | + } catch (err) { |
| 46 | + setError(err instanceof Error ? err.message : 'Key generation failed.'); |
| 47 | + } finally { |
| 48 | + setBusy(false); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + if (supported === false) { |
| 53 | + return ( |
| 54 | + <div className="card p-6"> |
| 55 | + <h2 className="font-semibold text-white">Generate a key pair</h2> |
| 56 | + <div className="mt-3"> |
| 57 | + <Alert |
| 58 | + kind="error" |
| 59 | + message="This browser can't generate Ed25519 keys. Use ssh-keygen locally and paste the public key above." |
| 60 | + /> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <> |
| 68 | + <div className="card p-6"> |
| 69 | + <div className="flex items-center gap-2"> |
| 70 | + <Sparkles className="h-4 w-4 text-accent-400" /> |
| 71 | + <h2 className="font-semibold text-white">Generate a key pair in your browser</h2> |
| 72 | + </div> |
| 73 | + <p className="mt-1.5 text-sm text-slate-400"> |
| 74 | + Creates an Ed25519 key with Web Crypto. The private key is shown only once and never sent to |
| 75 | + the server — the public key is added to your handle automatically. |
| 76 | + </p> |
| 77 | + <div className="mt-4 flex flex-col gap-3 sm:flex-row"> |
| 78 | + <input |
| 79 | + value={label} |
| 80 | + onChange={(e) => setLabel(e.target.value)} |
| 81 | + placeholder="Label (optional), e.g. Work laptop" |
| 82 | + className="input sm:flex-1" |
| 83 | + /> |
| 84 | + <button onClick={generate} disabled={busy || supported === null} className="btn-primary shrink-0"> |
| 85 | + {busy ? <Loader2 className="h-4 w-4 animate-spin" /> : <KeySquare className="h-4 w-4" />} |
| 86 | + Generate |
| 87 | + </button> |
| 88 | + </div> |
| 89 | + {error && ( |
| 90 | + <div className="mt-3"> |
| 91 | + <Alert kind="error" message={error} /> |
| 92 | + </div> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + |
| 96 | + {result && ( |
| 97 | + <PrivateKeyModal |
| 98 | + pair={result.pair} |
| 99 | + fingerprint={result.stored.fingerprint} |
| 100 | + onClose={() => setResult(null)} |
| 101 | + onDownload={download} |
| 102 | + /> |
| 103 | + )} |
| 104 | + </> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +function PrivateKeyModal({ |
| 109 | + pair, |
| 110 | + fingerprint, |
| 111 | + onClose, |
| 112 | + onDownload, |
| 113 | +}: { |
| 114 | + pair: GeneratedKeyPair; |
| 115 | + fingerprint: string; |
| 116 | + onClose: () => void; |
| 117 | + onDownload: (filename: string, contents: string) => void; |
| 118 | +}) { |
| 119 | + useEffect(() => { |
| 120 | + const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose(); |
| 121 | + window.addEventListener('keydown', onKey); |
| 122 | + document.body.style.overflow = 'hidden'; |
| 123 | + return () => { |
| 124 | + window.removeEventListener('keydown', onKey); |
| 125 | + document.body.style.overflow = ''; |
| 126 | + }; |
| 127 | + }, [onClose]); |
| 128 | + |
| 129 | + return ( |
| 130 | + <div |
| 131 | + className="fixed inset-0 z-50 grid place-items-center bg-ink-950/80 p-4 backdrop-blur-sm" |
| 132 | + role="dialog" |
| 133 | + aria-modal="true" |
| 134 | + onClick={onClose} |
| 135 | + > |
| 136 | + <div |
| 137 | + className="card w-full max-w-2xl animate-fade-up p-6 shadow-glow" |
| 138 | + onClick={(e) => e.stopPropagation()} |
| 139 | + > |
| 140 | + <div className="flex items-start justify-between"> |
| 141 | + <div> |
| 142 | + <h2 className="text-lg font-bold text-white">Save your private key</h2> |
| 143 | + <p className="mt-1 font-mono text-xs text-slate-500">{fingerprint}</p> |
| 144 | + </div> |
| 145 | + <button onClick={onClose} aria-label="Close" className="rounded-lg p-1.5 text-slate-400 hover:bg-ink-800 hover:text-white"> |
| 146 | + <X className="h-5 w-5" /> |
| 147 | + </button> |
| 148 | + </div> |
| 149 | + |
| 150 | + <div className="mt-4 flex items-start gap-2.5 rounded-xl border border-amber-500/30 bg-amber-500/10 px-4 py-3 text-sm text-amber-200"> |
| 151 | + <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0" /> |
| 152 | + <span> |
| 153 | + This private key is shown <strong>once</strong> and is never stored by SSHID. Download or copy |
| 154 | + it now and keep it secret. The public key has already been added to your handle. |
| 155 | + </span> |
| 156 | + </div> |
| 157 | + |
| 158 | + <div className="mt-4 space-y-3"> |
| 159 | + <CodeBlock label="id_ed25519 (private key)" code={pair.privateKey}> |
| 160 | + {pair.privateKey} |
| 161 | + </CodeBlock> |
| 162 | + |
| 163 | + <div className="flex flex-wrap gap-3"> |
| 164 | + <button onClick={() => onDownload(pair.filename, pair.privateKey)} className="btn-primary"> |
| 165 | + <Download className="h-4 w-4" /> |
| 166 | + Download private key |
| 167 | + </button> |
| 168 | + <button |
| 169 | + onClick={() => onDownload(`${pair.filename}.pub`, pair.publicKey + '\n')} |
| 170 | + className="btn-ghost" |
| 171 | + > |
| 172 | + <Download className="h-4 w-4" /> |
| 173 | + Download public key |
| 174 | + </button> |
| 175 | + </div> |
| 176 | + |
| 177 | + <p className="text-xs text-slate-500"> |
| 178 | + Install it with{' '} |
| 179 | + <code className="font-mono text-slate-400"> |
| 180 | + mv {pair.filename} ~/.ssh/ && chmod 600 ~/.ssh/{pair.filename} |
| 181 | + </code> |
| 182 | + </p> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + ); |
| 187 | +} |
0 commit comments