Files
Netcatty/components/keychain/utils.ts
T
陈大猫andnetcatty-bot 48f1a1cecf feat(ssh): restore FIDO2 security key support (#2823)
* feat(ssh): restore FIDO2 security key support (#2308)

Consolidate the restored feature and its review fixes before rebasing onto current main. Original PR head: 5d0a0c6c9a550364d3ec5c2331395cedfc9ea7ae.

* fix(ssh): verify and repair FIDO authentication with simulated hardware

Preserve complete OpenSSH SK signature payloads, classify combined PIN/presence prompts correctly, expand IdentityFile tokens, retain certificate selectors, and route shared-agent signing prompts to their originating window. Dismiss prompts when notification helpers exit. Add a software SK provider and opt-in real OpenSSH integration coverage for both algorithms, certificates, SFTP, forwarding, and concurrent windows.

* refactor(ui): refine security key forms and authentication motion

Use shared Select and Switch controls, concise localized copy and compact token-based dialogs. Add scoped enter/exit, field transition and presence animations with reduced-motion support. Preserve raw prompt details and visible PIN errors.

* fix(ssh): align security key prompts with hardware presence and verify Docker logins

* fix(ui): keep security key dialog above its remounted overlay

* fix: address Codex review on PR #2823

* feat(ssh): guide security key recovery and explicit authentication fallback

* test(ssh): exercise security key authentication on native Windows

* test(ssh): verify Windows fixture signatures and explicit fallback

* test(ssh): configure Windows OpenSSH toolchain and document coverage

---------

Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
2026-10-17 21:53:44 +08:00

118 lines
3.4 KiB
TypeScript

/**
* Keychain utility functions
*/
import { BadgeCheck, Key } from 'lucide-react';
import React from 'react';
import { detectFidoSshKeyType } from '../../domain/fidoSsh';
import { logger } from '../../lib/logger';
import { detectSshKeyType } from '../../domain/keyTypeDetect';
import { KeyType, SSHKey } from '../../types';
/**
* Get icon element for key source
*/
export const getKeyIcon = (key: SSHKey): React.ReactElement => {
if (key.certificate) return React.createElement(BadgeCheck, { size: 16 });
return React.createElement(Key, { size: 16 });
};
/**
* Get display text for key type
*/
export const getKeyTypeDisplay = (key: SSHKey, isMac: boolean): string => {
void isMac;
return key.type;
};
/**
* Resolve vault KeyType from imported key material.
*
* Always prefer material detection over the import form's seeded default
* (`openImport` seeds `type: "ED25519"`). Real OpenSSH sk private PEMs only
* expose sk-* algorithms after base64 decode — handled by detectFidoSshKeyType.
*/
export const resolveImportedKeyType = (args: {
privateKey: string;
publicKey?: string;
}): KeyType => {
return detectFidoSshKeyType(args) || detectSshKeyType(args.privateKey, args.publicKey).type;
};
/**
* Detect key type from private key content (file drop / paste helpers).
*/
export const detectKeyType = (privateKey: string): KeyType =>
resolveImportedKeyType({ privateKey });
/**
* Copy text to clipboard
*/
export const copyToClipboard = async (text: string): Promise<boolean> => {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
logger.error('Failed to copy to clipboard:', err);
return false;
}
};
/**
* Check if running on macOS
*/
export const isMacOS = (): boolean => {
return navigator.platform.toLowerCase().includes('mac') ||
navigator.userAgent.toLowerCase().includes('mac');
};
// Panel modes type
export type PanelMode =
| { type: 'closed' }
| { type: 'view'; key: SSHKey }
| { type: 'edit'; key: SSHKey }
| { type: 'generate'; keyType: 'standard' }
| { type: 'import' }
| { type: 'identity'; identity?: import('../../types').Identity }
| { type: 'export'; key: SSHKey };
interface IdentitySectionVisibilityOptions {
identityCount: number;
filteredIdentityCount: number;
filteredKeyCount: number;
search: string;
}
/** Show identities whenever any exist; while searching, keep the section if it matches or nothing matches. */
export const shouldShowIdentitySection = ({
identityCount,
filteredIdentityCount,
filteredKeyCount,
search,
}: IdentitySectionVisibilityOptions): boolean => {
if (identityCount === 0) return false;
if (!search.trim()) return true;
return filteredIdentityCount > 0 || filteredKeyCount === 0;
};
/**
* Show keys when any match (or exist while browsing). Hide the empty-key CTA when
* identities alone already fill the page - including identity-only vaults.
*/
export const shouldShowKeySection = ({
identityCount,
filteredKeyCount,
}: Pick<
IdentitySectionVisibilityOptions,
'identityCount' | 'filteredKeyCount' | 'search'
>): boolean => {
return filteredKeyCount > 0 || identityCount === 0;
};
export const shouldShowSearchNoResults = (
search: string,
filteredItemCount: number,
totalItemCount: number,
): boolean => Boolean(search.trim()) && totalItemCount > 0 && filteredItemCount === 0;