Files
Netcatty/components/KeychainManager.tsx
T
bincxz fed2132197 Improves SFTP reconnect logic and platform UI clarity
Adds auto-reconnect for SFTP pane sessions, preserving file state and user experience during connection loss. Refactors range selection for greater accuracy and UI alignment. Enhances biometric key and platform labeling to dynamically reflect macOS or Windows, improving clarity for end users. Refines sidebar and pane layouts for consistency and usability.
2025-12-10 01:21:13 +08:00

1285 lines
62 KiB
TypeScript

import React, { useMemo, useState, useCallback } from 'react';
import { SSHKey, KeyType, KeySource, KeyCategory, Identity, IdentityAuthMethod } from '../types';
import {
Key,
Plus,
Trash2,
Shield,
Search,
LayoutGrid,
List as ListIcon,
ChevronDown,
Fingerprint,
FileKey,
BadgeCheck,
MoreHorizontal,
ChevronRight,
Eye,
EyeOff,
Copy,
Download,
Upload,
User,
UserPlus,
} from 'lucide-react';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { Card, CardDescription, CardTitle } from './ui/card';
import { Label } from './ui/label';
import { Textarea } from './ui/textarea';
import { cn } from '../lib/utils';
import { Popover, PopoverContent, PopoverTrigger } from './ui/popover';
// Filter tab types
type FilterTab = 'key' | 'certificate' | 'biometric' | 'fido2';
// Panel modes
type PanelMode =
| { type: 'closed' }
| { type: 'view'; key: SSHKey }
| { type: 'edit'; key: SSHKey }
| { type: 'generate'; keyType: 'standard' | 'biometric' }
| { type: 'import' }
| { type: 'identity'; identity?: Identity };
interface KeychainManagerProps {
keys: SSHKey[];
identities?: Identity[];
onSave: (key: SSHKey) => void;
onUpdate: (key: SSHKey) => void;
onDelete: (id: string) => void;
onSaveIdentity?: (identity: Identity) => void;
onDeleteIdentity?: (id: string) => void;
}
// Helper to generate mock key pair (in real app, use crypto APIs)
const generateMockKeyPair = (type: KeyType, label: string): { privateKey: string; publicKey: string } => {
const typeMap = {
'ED25519': 'ed25519',
'ECDSA': 'ecdsa-sha2-nistp256',
'RSA': 'rsa',
};
const randomId = crypto.randomUUID().replace(/-/g, '').substring(0, 32);
const privateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACB${randomId}AAAEC${randomId}
-----END OPENSSH PRIVATE KEY-----`;
const publicKey = `ssh-${typeMap[type]} AAAAC3NzaC1lZDI1NTE5AAAAI${randomId.substring(0, 20)} ${label}@netcatty`;
return { privateKey, publicKey };
};
// WebAuthn helper for Windows Hello
const createBiometricCredential = async (label: string): Promise<{
credentialId: string;
publicKey: string;
rpId: string;
} | null> => {
try {
// Check if WebAuthn is supported
if (!window.PublicKeyCredential) {
throw new Error('WebAuthn is not supported in this environment');
}
// Check if we're in a secure context (HTTPS or localhost)
if (!window.isSecureContext) {
throw new Error('WebAuthn requires a secure context (HTTPS). This feature is not available in the current environment.');
}
// Check if platform authenticator is available (Windows Hello, Touch ID, etc.)
const available = await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
if (!available) {
const isMacOS = navigator.platform.toLowerCase().includes('mac') || navigator.userAgent.toLowerCase().includes('mac');
throw new Error(`No platform authenticator available. Please ensure ${isMacOS ? 'Touch ID' : 'Windows Hello'} is set up in your system settings.`);
}
// For Electron apps, we need to handle the rpId carefully
// The rpId must match the origin's effective domain
let rpId: string;
const hostname = window.location.hostname;
// In Electron file:// protocol or localhost dev server
if (!hostname || hostname === '' || hostname === 'localhost' || hostname === '127.0.0.1') {
rpId = 'localhost';
} else {
rpId = hostname;
}
const userId = new TextEncoder().encode(crypto.randomUUID());
const credential = await navigator.credentials.create({
publicKey: {
challenge: crypto.getRandomValues(new Uint8Array(32)),
rp: {
name: 'Netcatty SSH Manager',
id: rpId,
},
user: {
id: userId,
name: label,
displayName: label,
},
pubKeyCredParams: [
{ alg: -7, type: 'public-key' }, // ES256 (ECDSA P-256)
],
authenticatorSelection: {
authenticatorAttachment: 'platform',
residentKey: 'discouraged',
userVerification: 'preferred',
},
timeout: 180000, // 3 minutes
attestation: 'none',
},
}) as PublicKeyCredential;
if (!credential) {
return null;
}
const response = credential.response as AuthenticatorAttestationResponse;
// Convert credential ID to base64
const credentialId = btoa(String.fromCharCode(...new Uint8Array(credential.rawId)));
// Extract public key from attestation (simplified - in production, parse CBOR properly)
const publicKeyBytes = new Uint8Array(response.getPublicKey?.() || []);
const publicKeyBase64 = btoa(String.fromCharCode(...publicKeyBytes));
// Format as OpenSSH sk-ecdsa key
const publicKey = `sk-ecdsa-sha2-nistp256@openssh.com AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20${publicKeyBase64.substring(0, 100)} ${label}@netcatty`;
return {
credentialId,
publicKey,
rpId,
};
} catch (error) {
console.error('WebAuthn credential creation failed:', error);
throw error;
}
};
const KeychainManager: React.FC<KeychainManagerProps> = ({
keys,
identities = [],
onSave,
onUpdate,
onDelete,
onSaveIdentity,
onDeleteIdentity,
}) => {
const [activeFilter, setActiveFilter] = useState<FilterTab>('key');
const [search, setSearch] = useState('');
const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid');
const [panel, setPanel] = useState<PanelMode>({ type: 'closed' });
// Detect if running on macOS
const isMac = useMemo(() => {
return navigator.platform.toLowerCase().includes('mac') ||
navigator.userAgent.toLowerCase().includes('mac');
}, []);
// Biometric authentication label based on platform
const biometricLabel = isMac ? 'TOUCH ID' : 'WINDOWS HELLO';
// Draft state for forms
const [draftKey, setDraftKey] = useState<Partial<SSHKey>>({});
const [draftIdentity, setDraftIdentity] = useState<Partial<Identity>>({});
const [showPassphrase, setShowPassphrase] = useState(false);
const [isGenerating, setIsGenerating] = useState(false);
const [error, setError] = useState<string | null>(null);
// Filter keys based on active tab and search
const filteredKeys = useMemo(() => {
let result = keys;
// Filter by tab
switch (activeFilter) {
case 'key':
result = result.filter(k => k.source === 'generated' || k.source === 'imported');
break;
case 'certificate':
result = result.filter(k => k.category === 'certificate' || k.certificate);
break;
case 'biometric':
result = result.filter(k => k.source === 'biometric');
break;
case 'fido2':
result = result.filter(k => k.source === 'fido2');
break;
}
// Filter by search
if (search.trim()) {
const s = search.toLowerCase();
result = result.filter(k =>
k.label.toLowerCase().includes(s) ||
k.type.toLowerCase().includes(s) ||
k.publicKey?.toLowerCase().includes(s)
);
}
return result;
}, [keys, activeFilter, search]);
// Filter identities based on search
const filteredIdentities = useMemo(() => {
if (!search.trim()) return identities;
const s = search.toLowerCase();
return identities.filter(i =>
i.label.toLowerCase().includes(s) ||
i.username.toLowerCase().includes(s)
);
}, [identities, search]);
// Close panel
const closePanel = useCallback(() => {
setPanel({ type: 'closed' });
setDraftKey({});
setDraftIdentity({});
setError(null);
setShowPassphrase(false);
}, []);
// Open panel for viewing key
const openKeyView = useCallback((key: SSHKey) => {
setPanel({ type: 'view', key });
setDraftKey({ ...key });
setError(null);
}, []);
// Open panel for new identity
const openNewIdentity = useCallback(() => {
setPanel({ type: 'identity' });
setDraftIdentity({
id: '',
label: '',
username: '',
authMethod: 'password',
created: Date.now(),
});
setError(null);
}, []);
// Open generate panel
const openGenerate = useCallback((keyType: 'standard' | 'biometric') => {
setPanel({ type: 'generate', keyType });
setDraftKey({
id: '',
label: '',
type: keyType === 'biometric' ? 'ECDSA' : 'ED25519',
privateKey: '',
publicKey: '',
source: keyType === 'biometric' ? 'biometric' : 'generated',
category: 'key',
created: Date.now(),
});
setError(null);
}, []);
// Open import panel
const openImport = useCallback(() => {
setPanel({ type: 'import' });
setDraftKey({
id: '',
label: '',
type: 'ED25519',
privateKey: '',
publicKey: '',
source: 'imported',
category: 'key',
created: Date.now(),
});
setError(null);
}, []);
// Handle standard key generation
const handleGenerateStandard = useCallback(async () => {
if (!draftKey.label?.trim()) {
setError('Please enter a label for the key');
return;
}
setIsGenerating(true);
setError(null);
try {
// Simulate key generation delay
await new Promise(resolve => setTimeout(resolve, 500));
const { privateKey, publicKey } = generateMockKeyPair(
draftKey.type as KeyType || 'ED25519',
draftKey.label
);
const newKey: SSHKey = {
id: crypto.randomUUID(),
label: draftKey.label.trim(),
type: draftKey.type as KeyType || 'ED25519',
privateKey,
publicKey,
passphrase: draftKey.passphrase,
savePassphrase: draftKey.savePassphrase,
source: 'generated',
category: 'key',
created: Date.now(),
};
onSave(newKey);
closePanel();
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to generate key');
} finally {
setIsGenerating(false);
}
}, [draftKey, onSave, closePanel]);
// Handle biometric key generation (Windows Hello)
const handleGenerateBiometric = useCallback(async () => {
if (!draftKey.label?.trim()) {
setError('Please enter a label for the key');
return;
}
setIsGenerating(true);
setError(null);
try {
const result = await createBiometricCredential(draftKey.label.trim());
if (!result) {
throw new Error('Credential creation was cancelled');
}
const newKey: SSHKey = {
id: crypto.randomUUID(),
label: draftKey.label.trim(),
type: 'ECDSA',
privateKey: '', // Biometric keys don't have exportable private keys
publicKey: result.publicKey,
credentialId: result.credentialId,
rpId: result.rpId,
source: 'biometric',
category: 'key',
created: Date.now(),
};
onSave(newKey);
closePanel();
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create biometric credential');
} finally {
setIsGenerating(false);
}
}, [draftKey, onSave, closePanel]);
// Handle key import
const handleImport = useCallback(() => {
if (!draftKey.label?.trim() || !draftKey.privateKey?.trim()) {
setError('Label and private key are required');
return;
}
// Detect key type from private key content
let detectedType: KeyType = 'ED25519';
const pk = draftKey.privateKey.toLowerCase();
if (pk.includes('rsa')) detectedType = 'RSA';
else if (pk.includes('ecdsa') || pk.includes('ec ')) detectedType = 'ECDSA';
else if (pk.includes('ed25519')) detectedType = 'ED25519';
const newKey: SSHKey = {
id: crypto.randomUUID(),
label: draftKey.label.trim(),
type: draftKey.type as KeyType || detectedType,
privateKey: draftKey.privateKey.trim(),
publicKey: draftKey.publicKey?.trim() || undefined,
certificate: draftKey.certificate?.trim() || undefined,
passphrase: draftKey.passphrase,
savePassphrase: draftKey.savePassphrase,
source: 'imported',
category: draftKey.certificate ? 'certificate' : 'key',
created: Date.now(),
};
onSave(newKey);
closePanel();
}, [draftKey, onSave, closePanel]);
// Handle save identity
const handleSaveIdentity = useCallback(() => {
if (!draftIdentity.label?.trim() || !draftIdentity.username?.trim()) {
setError('Label and username are required');
return;
}
if (!onSaveIdentity) return;
const newIdentity: Identity = {
id: draftIdentity.id || crypto.randomUUID(),
label: draftIdentity.label.trim(),
username: draftIdentity.username.trim(),
authMethod: draftIdentity.authMethod || 'password',
password: draftIdentity.password,
keyId: draftIdentity.keyId,
created: draftIdentity.created || Date.now(),
};
onSaveIdentity(newIdentity);
closePanel();
}, [draftIdentity, onSaveIdentity, closePanel]);
// Handle delete
const handleDelete = useCallback((id: string) => {
onDelete(id);
if (panel.type === 'view' && panel.key.id === id) {
closePanel();
}
}, [onDelete, panel, closePanel]);
// Handle delete identity
const handleDeleteIdentity = useCallback((id: string) => {
onDeleteIdentity?.(id);
if (panel.type === 'identity' && panel.identity?.id === id) {
closePanel();
}
}, [onDeleteIdentity, panel, closePanel]);
// Copy to clipboard
const copyToClipboard = useCallback((text: string) => {
navigator.clipboard.writeText(text);
}, []);
// Get icon for key source
const getKeyIcon = (key: SSHKey) => {
if (key.source === 'biometric') return <Fingerprint size={16} />;
if (key.source === 'fido2') return <Shield size={16} />;
if (key.certificate) return <BadgeCheck size={16} />;
return <Key size={16} />;
};
// Get key type display
const getKeyTypeDisplay = (key: SSHKey) => {
if (key.source === 'biometric') return isMac ? 'Touch ID' : 'Windows Hello';
if (key.source === 'fido2') return 'FIDO2';
return key.type;
};
// File input ref for import
const fileInputRef = React.useRef<HTMLInputElement>(null);
// Handle file import
const handleFileImport = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target?.result as string;
if (content) {
// Try to detect key type from content
let detectedType: KeyType = 'ED25519';
const lc = content.toLowerCase();
if (lc.includes('rsa')) detectedType = 'RSA';
else if (lc.includes('ecdsa') || lc.includes('ec private')) detectedType = 'ECDSA';
else if (lc.includes('ed25519')) detectedType = 'ED25519';
// Extract label from filename (remove extension)
const label = file.name.replace(/\.(pem|key|pub|ppk)$/i, '');
setDraftKey(prev => ({
...prev,
privateKey: content,
label: prev.label || label,
type: detectedType,
}));
}
};
reader.readAsText(file);
// Reset input so same file can be selected again
event.target.value = '';
}, []);
// Handle drag and drop
const handleDrop = useCallback((event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
event.stopPropagation();
const file = event.dataTransfer.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target?.result as string;
if (content) {
let detectedType: KeyType = 'ED25519';
const lc = content.toLowerCase();
if (lc.includes('rsa')) detectedType = 'RSA';
else if (lc.includes('ecdsa') || lc.includes('ec private')) detectedType = 'ECDSA';
else if (lc.includes('ed25519')) detectedType = 'ED25519';
const label = file.name.replace(/\.(pem|key|pub|ppk)$/i, '');
setDraftKey(prev => ({
...prev,
privateKey: content,
label: prev.label || label,
type: detectedType,
}));
}
};
reader.readAsText(file);
}, []);
const handleDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
event.stopPropagation();
}, []);
return (
<div className="h-full flex relative">
{/* Hidden file input */}
<input
ref={fileInputRef}
type="file"
accept=".pem,.key,.pub,.ppk,*"
className="hidden"
onChange={handleFileImport}
/>
{/* Main Content */}
<div className={cn(
"flex-1 overflow-y-auto transition-all duration-200",
panel.type !== 'closed' && "mr-[380px]"
)}>
{/* Toolbar */}
<div className="flex flex-wrap items-center gap-3 bg-secondary/60 border-b border-border/70 px-3 py-1.5">
{/* Filter Tabs */}
<div className="flex items-center gap-1">
<Popover>
<PopoverTrigger asChild>
<Button
size="sm"
variant="secondary"
className={cn(
"h-8 px-3 gap-2",
activeFilter === 'key' && "bg-primary/15 text-primary"
)}
>
<Key size={14} />
KEY
<ChevronDown size={12} />
</Button>
</PopoverTrigger>
<PopoverContent className="w-44 p-1">
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={() => openGenerate('standard')}
>
<Plus size={14} /> Generate Key
</Button>
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={openImport}
>
<Upload size={14} /> Import Key
</Button>
{onSaveIdentity && (
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={openNewIdentity}
>
<UserPlus size={14} /> New Identity
</Button>
)}
</PopoverContent>
</Popover>
<Popover>
<PopoverTrigger asChild>
<Button
size="sm"
variant={activeFilter === 'certificate' ? "secondary" : "ghost"}
className={cn(
"h-8 px-3 gap-2",
activeFilter === 'certificate' && "bg-primary/15 text-primary"
)}
onClick={() => setActiveFilter('certificate')}
>
<BadgeCheck size={14} />
CERTIFICATE
<span className="text-[10px] px-1.5 rounded-full bg-muted text-muted-foreground">
{keys.filter(k => k.certificate).length}
</span>
</Button>
</PopoverTrigger>
<PopoverContent className="w-44 p-1">
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={openImport}
>
<Upload size={14} /> Import Certificate
</Button>
</PopoverContent>
</Popover>
<Button
size="sm"
variant={activeFilter === 'biometric' ? "secondary" : "ghost"}
className={cn(
"h-8 px-3 gap-2",
activeFilter === 'biometric' && "bg-primary/15 text-primary"
)}
onClick={() => setActiveFilter('biometric')}
>
<Fingerprint size={14} />
{biometricLabel}
</Button>
<Button
size="sm"
variant={activeFilter === 'fido2' ? "secondary" : "ghost"}
className={cn(
"h-8 px-3 gap-2",
activeFilter === 'fido2' && "bg-primary/15 text-primary"
)}
onClick={() => setActiveFilter('fido2')}
>
<Shield size={14} />
FIDO2
</Button>
</div>
{/* Search and View Mode - hide search when panel is open */}
<div className="ml-auto flex items-center gap-2 min-w-0 flex-shrink">
{panel.type === 'closed' && (
<div className="relative flex-shrink min-w-[100px]">
<Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" />
<Input
value={search}
onChange={e => setSearch(e.target.value)}
placeholder="Search..."
className="h-9 pl-8 w-full"
/>
</div>
)}
<Popover>
<PopoverTrigger asChild>
<Button variant="ghost" size="icon" className="h-9 w-9 flex-shrink-0">
{viewMode === 'grid' ? <LayoutGrid size={16} /> : <ListIcon size={16} />}
<ChevronDown size={10} className="ml-0.5" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-32 p-1 z-50" align="end">
<Button
variant={viewMode === 'grid' ? 'secondary' : 'ghost'}
className="w-full justify-start gap-2 h-9"
onClick={() => setViewMode('grid')}
>
<LayoutGrid size={14} /> Grid
</Button>
<Button
variant={viewMode === 'list' ? 'secondary' : 'ghost'}
className="w-full justify-start gap-2 h-9"
onClick={() => setViewMode('list')}
>
<ListIcon size={14} /> List
</Button>
</PopoverContent>
</Popover>
</div>
</div>
{/* Keys Section */}
<div className="space-y-3 p-3">
<div className="flex items-center justify-between">
<h2 className="text-base font-semibold text-muted-foreground">Keys</h2>
<span className="text-xs text-muted-foreground">{filteredKeys.length} items</span>
</div>
{filteredKeys.length === 0 ? (
<div className="flex flex-col items-center justify-center h-64 text-muted-foreground">
<div className="h-16 w-16 rounded-2xl bg-secondary/80 flex items-center justify-center mb-4">
<Shield size={32} className="opacity-60" />
</div>
<h3 className="text-lg font-semibold text-foreground mb-2">
{activeFilter === 'biometric' ? `Set up ${isMac ? 'Touch ID' : 'Windows Hello'}` :
activeFilter === 'fido2' ? 'Add a security key' :
'Set up your keys'}
</h3>
<p className="text-sm text-center max-w-sm mb-4">
{activeFilter === 'biometric'
? `Create biometric SSH keys secured by ${isMac ? 'Touch ID' : 'Windows Hello'} for passwordless authentication.`
: activeFilter === 'fido2'
? 'Connect a hardware security key (YubiKey, etc.) for enhanced security.'
: 'Import or generate SSH keys for secure authentication.'}
</p>
{activeFilter === 'biometric' && (
<Button onClick={() => openGenerate('biometric')}>
<Fingerprint size={14} className="mr-2" />
Create Biometric Key
</Button>
)}
{(activeFilter === 'key' || activeFilter === 'certificate') && (
<div className="flex gap-2">
<Button variant="secondary" onClick={openImport}>
<Upload size={14} className="mr-2" />
Import
</Button>
<Button onClick={() => openGenerate('standard')}>
<Plus size={14} className="mr-2" />
Generate
</Button>
</div>
)}
</div>
) : (
<div className={viewMode === 'grid'
? "grid gap-2.5 grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"
: "space-y-2"
}>
{filteredKeys.map((key) => (
<Card
key={key.id}
className={cn(
"group relative overflow-hidden bg-secondary/60 border transition-all cursor-pointer",
"h-[72px] px-3 py-2",
viewMode === 'list' && "w-full",
"border-border/60 shadow-sm hover:shadow-[0_0_0_2px_var(--ring)]",
panel.type === 'view' && panel.key.id === key.id && "ring-2 ring-primary"
)}
onClick={() => openKeyView(key)}
>
<div className="flex items-center gap-3 h-full">
<div className={cn(
"h-9 w-9 rounded-md flex items-center justify-center",
key.source === 'biometric'
? "bg-blue-500/15 text-blue-500"
: "bg-primary/15 text-primary"
)}>
{getKeyIcon(key)}
</div>
<div className="min-w-0 flex-1">
<CardTitle className="text-sm font-semibold truncate">{key.label}</CardTitle>
<CardDescription className="text-[11px] font-mono text-muted-foreground truncate">
Type {getKeyTypeDisplay(key)}
</CardDescription>
</div>
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<Button
size="icon"
variant="ghost"
className="h-8 w-8 text-destructive hover:text-destructive"
onClick={(e) => {
e.stopPropagation();
handleDelete(key.id);
}}
>
<Trash2 size={14} />
</Button>
</div>
</div>
</Card>
))}
</div>
)}
</div>
{/* Identities Section */}
{activeFilter === 'key' && filteredIdentities.length > 0 && (
<div className="space-y-3 px-3 pb-3">
<div className="flex items-center justify-between">
<h2 className="text-base font-semibold text-muted-foreground">Identities</h2>
<span className="text-xs text-muted-foreground">{filteredIdentities.length} items</span>
</div>
<div className={viewMode === 'grid'
? "grid gap-2.5 grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"
: "space-y-2"
}>
{filteredIdentities.map((identity) => (
<Card
key={identity.id}
className={cn(
"group relative overflow-hidden bg-secondary/60 border transition-all cursor-pointer",
"h-[72px] px-3 py-2",
viewMode === 'list' && "w-full",
"border-border/60 shadow-sm hover:shadow-[0_0_0_2px_var(--ring)]",
panel.type === 'identity' && panel.identity?.id === identity.id && "ring-2 ring-primary"
)}
onClick={() => {
setPanel({ type: 'identity', identity });
setDraftIdentity({ ...identity });
}}
>
<div className="flex items-center gap-3 h-full">
<div className="h-9 w-9 rounded-md bg-green-500/15 text-green-500 flex items-center justify-center">
<User size={16} />
</div>
<div className="min-w-0 flex-1">
<CardTitle className="text-sm font-semibold truncate">{identity.label || 'Add a label...'}</CardTitle>
<CardDescription className="text-[11px] font-mono text-muted-foreground truncate">
{identity.authMethod === 'password' ? 'Password' :
identity.authMethod === 'key' ? 'Key' :
identity.authMethod === 'certificate' ? 'Certificate' :
'FIDO2'}
</CardDescription>
</div>
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<Button
size="icon"
variant="ghost"
className="h-8 w-8 text-destructive hover:text-destructive"
onClick={(e) => {
e.stopPropagation();
handleDeleteIdentity(identity.id);
}}
>
<Trash2 size={14} />
</Button>
</div>
</div>
</Card>
))}
</div>
</div>
)}
</div>
{/* Slide-out Panel */}
{panel.type !== 'closed' && (
<div className="absolute right-0 top-0 bottom-0 w-[380px] border-l border-border/60 bg-secondary/95 backdrop-blur z-30 overflow-y-auto">
{/* Panel Header */}
<div className="p-4 flex items-center justify-between border-b border-border/60">
<div>
<p className="text-sm font-semibold">
{panel.type === 'generate' && panel.keyType === 'biometric' && 'Generate Biometric Key'}
{panel.type === 'generate' && panel.keyType === 'standard' && 'Generate Key'}
{panel.type === 'import' && 'New Key'}
{panel.type === 'view' && (panel.key.source === 'biometric' ? 'Biometric Key' : 'Key Details')}
{panel.type === 'edit' && 'Edit Key'}
{panel.type === 'identity' && (panel.identity ? 'Edit Identity' : 'New Identity')}
</p>
<p className="text-xs text-muted-foreground">Personal vault</p>
</div>
<div className="flex items-center gap-1">
{(panel.type === 'view' || panel.type === 'identity') && (
<Button variant="ghost" size="icon" className="h-8 w-8">
<MoreHorizontal size={16} />
</Button>
)}
<Button variant="ghost" size="icon" className="h-8 w-8" onClick={closePanel}>
<ChevronRight size={16} />
</Button>
</div>
</div>
{/* Panel Content */}
<div className="p-4 space-y-4">
{/* Error Display */}
{error && (
<div className="p-3 bg-destructive/10 border border-destructive/30 rounded-lg text-sm text-destructive">
{error}
</div>
)}
{/* Generate Biometric Key */}
{panel.type === 'generate' && panel.keyType === 'biometric' && (
<>
{/* Keyboard illustration */}
<div className="bg-card border border-border/80 rounded-lg p-3 flex items-center justify-center overflow-hidden">
<div className="text-center w-full">
<div className="flex justify-center items-center gap-0.5 mb-1.5">
{['9', '0', ')', '-', '+', '='].map((k, i) => (
<div key={i} className="w-6 h-6 bg-secondary border border-border/60 rounded text-[10px] flex items-center justify-center flex-shrink-0">
{k}
</div>
))}
<div className="w-12 h-6 bg-secondary border border-border/60 rounded text-[9px] flex items-center justify-center flex-shrink-0">
back
</div>
<div className="w-8 h-8 bg-blue-500/20 border border-blue-500/40 rounded-lg flex items-center justify-center ml-1.5 flex-shrink-0">
<Fingerprint size={16} className="text-blue-500" />
</div>
</div>
<div className="flex justify-center gap-0.5">
{['I', 'O', 'P', '[', ']', '{', '}'].map((k, i) => (
<div key={i} className="w-6 h-6 bg-secondary border border-border/60 rounded text-[10px] flex items-center justify-center flex-shrink-0">
{k}
</div>
))}
</div>
</div>
</div>
<p className="text-sm text-muted-foreground text-center">
Biometric Key based on Secure Enclave Process built-in into your {isMac ? 'mac' : 'system'}. This key is not possible to copy or steal.
</p>
<div className="space-y-2">
<Label>Label</Label>
<Input
value={draftKey.label || ''}
onChange={e => setDraftKey({ ...draftKey, label: e.target.value })}
placeholder={isMac ? 'Touch ID' : 'Windows Hello'}
/>
</div>
<div className="space-y-1">
<Label className="text-muted-foreground">Type</Label>
<p className="text-sm">ECDSA</p>
</div>
<div className="space-y-1">
<Label className="text-muted-foreground">Key Size</Label>
<p className="text-sm">256</p>
</div>
<Button
className="w-full h-11"
onClick={handleGenerateBiometric}
disabled={isGenerating || !draftKey.label?.trim()}
>
{isGenerating ? (
<div className="h-4 w-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />
) : (
'Generate'
)}
</Button>
</>
)}
{/* Generate Standard Key */}
{panel.type === 'generate' && panel.keyType === 'standard' && (
<>
<div className="space-y-2">
<Label>Label</Label>
<Input
value={draftKey.label || ''}
onChange={e => setDraftKey({ ...draftKey, label: e.target.value })}
placeholder="My SSH Key"
/>
</div>
<div className="space-y-2">
<Label>Key type</Label>
<div className="flex gap-2">
{(['ED25519', 'ECDSA', 'RSA'] as KeyType[]).map((t) => (
<Button
key={t}
variant={draftKey.type === t ? 'secondary' : 'ghost'}
className={cn(
"flex-1 h-10",
draftKey.type === t && "bg-primary/15 text-primary"
)}
onClick={() => setDraftKey({ ...draftKey, type: t })}
>
{t}
</Button>
))}
</div>
</div>
<div className="space-y-2">
<Label>Passphrase</Label>
<div className="relative">
<Input
type={showPassphrase ? 'text' : 'password'}
value={draftKey.passphrase || ''}
onChange={e => setDraftKey({ ...draftKey, passphrase: e.target.value })}
placeholder="Optional passphrase"
className="pr-10"
/>
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-1 top-1/2 -translate-y-1/2 h-8 w-8"
onClick={() => setShowPassphrase(!showPassphrase)}
>
{showPassphrase ? <EyeOff size={14} /> : <Eye size={14} />}
</Button>
</div>
</div>
<div className="flex items-center gap-2">
<input
type="checkbox"
id="savePassphrase"
checked={draftKey.savePassphrase || false}
onChange={e => setDraftKey({ ...draftKey, savePassphrase: e.target.checked })}
className="h-4 w-4 rounded border-border"
/>
<Label htmlFor="savePassphrase" className="text-sm font-normal cursor-pointer">
Save passphrase
</Label>
</div>
<Button
className="w-full h-11"
onClick={handleGenerateStandard}
disabled={isGenerating || !draftKey.label?.trim()}
>
{isGenerating ? (
<div className="h-4 w-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />
) : (
'Generate & save'
)}
</Button>
</>
)}
{/* Import Key */}
{panel.type === 'import' && (
<>
<div className="space-y-2">
<Label>Label</Label>
<Input
value={draftKey.label || ''}
onChange={e => setDraftKey({ ...draftKey, label: e.target.value })}
placeholder="Key label"
/>
</div>
<div className="space-y-2">
<Label>Private key *</Label>
<Textarea
value={draftKey.privateKey || ''}
onChange={e => setDraftKey({ ...draftKey, privateKey: e.target.value })}
placeholder="-----BEGIN OPENSSH PRIVATE KEY-----"
className="min-h-[120px] font-mono text-xs"
/>
</div>
<div className="space-y-2">
<Label>Public key</Label>
<Textarea
value={draftKey.publicKey || ''}
onChange={e => setDraftKey({ ...draftKey, publicKey: e.target.value })}
placeholder="ssh-ed25519 AAAAC3... user@host"
className="min-h-[80px] font-mono text-xs"
/>
</div>
<div className="space-y-2">
<Label className="flex items-center gap-2">
Certificate
<span className="text-[10px] px-2 py-0.5 rounded-full bg-muted text-muted-foreground">
Optional
</span>
</Label>
<Textarea
value={draftKey.certificate || ''}
onChange={e => setDraftKey({ ...draftKey, certificate: e.target.value })}
placeholder="Paste certificate..."
className="min-h-[80px] font-mono text-xs"
/>
</div>
<div
className="border border-dashed border-border/80 rounded-xl p-4 text-center space-y-2 bg-background/60 transition-colors hover:border-primary/50"
onDrop={handleDrop}
onDragOver={handleDragOver}
>
<div className="flex items-center justify-center gap-2 text-muted-foreground">
<Upload size={16} />
<span className="text-sm">Drag and drop a private key file to import</span>
</div>
<Button
variant="secondary"
className="w-full"
onClick={() => fileInputRef.current?.click()}
>
Import from key file
</Button>
</div>
<Button
className="w-full h-11"
onClick={handleImport}
disabled={!draftKey.label?.trim() || !draftKey.privateKey?.trim()}
>
Save Key
</Button>
</>
)}
{/* View Key */}
{panel.type === 'view' && (
<>
<div className="space-y-2">
<Label className="text-muted-foreground">Label</Label>
<p className="text-sm">{panel.key.label}</p>
</div>
{panel.key.publicKey && (
<div className="space-y-2">
<Label className="text-muted-foreground">Public Key</Label>
<div className="relative">
<div className="p-3 bg-card border border-border/80 rounded-lg font-mono text-xs break-all max-h-32 overflow-y-auto">
{panel.key.publicKey}
</div>
<Button
size="icon"
variant="ghost"
className="absolute top-2 right-2 h-7 w-7"
onClick={() => copyToClipboard(panel.key.publicKey || '')}
>
<Copy size={12} />
</Button>
</div>
</div>
)}
<div className="space-y-1">
<Label className="text-muted-foreground">Type</Label>
<p className="text-sm">{panel.key.type}</p>
</div>
{panel.key.source === 'biometric' && (
<div className="space-y-1">
<Label className="text-muted-foreground">Key Size</Label>
<p className="text-sm">256</p>
</div>
)}
<Button className="w-full h-11">
<Download size={14} className="mr-2" />
Export to host
</Button>
</>
)}
{/* Identity Panel */}
{panel.type === 'identity' && (
<>
<div className="flex items-center gap-3 mb-4">
<div className="h-10 w-10 rounded-lg bg-green-500/15 text-green-500 flex items-center justify-center">
<User size={20} />
</div>
<Input
value={draftIdentity.label || ''}
onChange={e => setDraftIdentity({ ...draftIdentity, label: e.target.value })}
placeholder="Label"
className="flex-1"
/>
</div>
<div className="space-y-2">
<Label>Username *</Label>
<div className="relative">
<User size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground" />
<Input
value={draftIdentity.username || ''}
onChange={e => setDraftIdentity({ ...draftIdentity, username: e.target.value })}
placeholder="Username"
className="pl-9"
/>
</div>
</div>
<div className="space-y-2">
<Label>Password</Label>
<div className="relative">
<Input
type={showPassphrase ? 'text' : 'password'}
value={draftIdentity.password || ''}
onChange={e => setDraftIdentity({ ...draftIdentity, password: e.target.value })}
placeholder="Password"
className="pr-10"
/>
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-1 top-1/2 -translate-y-1/2 h-8 w-8"
onClick={() => setShowPassphrase(!showPassphrase)}
>
{showPassphrase ? <EyeOff size={14} /> : <Eye size={14} />}
</Button>
</div>
</div>
<div className="space-y-2">
<Label>Authentication Method</Label>
<Popover>
<PopoverTrigger asChild>
<Button variant="secondary" className="w-full justify-between h-10">
<span className="flex items-center gap-2">
{draftIdentity.authMethod === 'key' && <><Key size={14} /> Key</>}
{draftIdentity.authMethod === 'certificate' && <><BadgeCheck size={14} /> Certificate</>}
{draftIdentity.authMethod === 'fido2' && <><Shield size={14} /> FIDO2</>}
{(!draftIdentity.authMethod || draftIdentity.authMethod === 'password') && 'None (Password only)'}
</span>
<ChevronDown size={14} />
</Button>
</PopoverTrigger>
<PopoverContent className="w-56 p-1">
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={() => setDraftIdentity({ ...draftIdentity, authMethod: 'key', keyId: undefined })}
>
<Key size={14} /> Key
</Button>
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={() => setDraftIdentity({ ...draftIdentity, authMethod: 'certificate', keyId: undefined })}
>
<BadgeCheck size={14} /> Certificate
</Button>
<Button
variant="ghost"
className="w-full justify-start gap-2"
onClick={() => setDraftIdentity({ ...draftIdentity, authMethod: 'fido2', keyId: undefined })}
>
<Shield size={14} /> FIDO2
</Button>
</PopoverContent>
</Popover>
</div>
{(draftIdentity.authMethod === 'key' || draftIdentity.authMethod === 'certificate') && (
<div className="space-y-2">
<Label>Select {draftIdentity.authMethod === 'key' ? 'Key' : 'Certificate'}</Label>
<select
value={draftIdentity.keyId || ''}
onChange={e => setDraftIdentity({ ...draftIdentity, keyId: e.target.value || undefined })}
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
>
<option value="">Select...</option>
{keys
.filter(k => draftIdentity.authMethod === 'certificate' ? k.certificate : !k.certificate)
.map(k => (
<option key={k.id} value={k.id}>{k.label}</option>
))
}
</select>
</div>
)}
<Button
className="w-full h-11"
onClick={handleSaveIdentity}
disabled={!draftIdentity.label?.trim() || !draftIdentity.username?.trim()}
>
{panel.identity ? 'Update Identity' : 'Save Identity'}
</Button>
</>
)}
</div>
</div>
)}
</div>
);
};
export default KeychainManager;