109 行
3.1 KiB
TypeScript
109 行
3.1 KiB
TypeScript
import { useState } from "react";
|
||
import { invoke } from "@tauri-apps/api/core";
|
||
import type { AuthenticationType, ConnectionProfile } from "../../types/profile";
|
||
|
||
interface CredentialEnrollmentResult {
|
||
profileId: string;
|
||
credentialReference: string;
|
||
authenticationType: AuthenticationType;
|
||
}
|
||
|
||
interface Props {
|
||
profile: ConnectionProfile;
|
||
onChanged: () => Promise<void> | void;
|
||
}
|
||
|
||
export function CredentialEnrollmentCard({ profile, onChanged }: Props) {
|
||
const [secret, setSecret] = useState("");
|
||
const [username, setUsername] = useState(profile.remoteUsername ?? "");
|
||
const [busy, setBusy] = useState(false);
|
||
const [message, setMessage] = useState<string | null>(null);
|
||
|
||
async function enroll() {
|
||
setBusy(true);
|
||
setMessage(null);
|
||
try {
|
||
const result = await invoke<CredentialEnrollmentResult>(
|
||
"enroll_profile_credential",
|
||
{
|
||
request: {
|
||
profileId: profile.id,
|
||
authenticationType: profile.authenticationType,
|
||
secret,
|
||
username: username.trim() || undefined,
|
||
},
|
||
},
|
||
);
|
||
setSecret("");
|
||
setMessage(`已登记凭据引用 ${result.credentialReference}`);
|
||
await onChanged();
|
||
} catch (error) {
|
||
setMessage(
|
||
error instanceof Error ? error.message : "凭据登记失败(详情未包含密钥)。",
|
||
);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function clearCredential() {
|
||
setBusy(true);
|
||
setMessage(null);
|
||
try {
|
||
await invoke("clear_profile_credential", { profileId: profile.id });
|
||
setMessage("已清除凭据引用");
|
||
await onChanged();
|
||
} catch (error) {
|
||
setMessage(
|
||
error instanceof Error ? error.message : "清除凭据失败。",
|
||
);
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<section className="credential-card" aria-label={`凭据登记 ${profile.displayName}`}>
|
||
<h4>安全凭据登记</h4>
|
||
<p>
|
||
密钥只通过一次性 IPC 写入 Windows Credential Manager,不会进入 React 持久状态。
|
||
</p>
|
||
<label>
|
||
用户名(可选,AstrBot 可用)
|
||
<input
|
||
value={username}
|
||
onChange={(event) => setUsername(event.target.value)}
|
||
disabled={busy}
|
||
/>
|
||
</label>
|
||
<label>
|
||
API Key / Token(一次性)
|
||
<input
|
||
type="password"
|
||
value={secret}
|
||
onChange={(event) => setSecret(event.target.value)}
|
||
disabled={busy}
|
||
autoComplete="off"
|
||
/>
|
||
</label>
|
||
<div className="button-row">
|
||
<button type="button" disabled={busy || !secret.trim()} onClick={() => void enroll()}>
|
||
登记凭据
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={busy || !profile.credentialReference}
|
||
onClick={() => void clearCredential()}
|
||
>
|
||
清除凭据
|
||
</button>
|
||
</div>
|
||
{profile.credentialReference ? (
|
||
<p>当前引用:{profile.credentialReference}</p>
|
||
) : (
|
||
<p>尚未登记凭据</p>
|
||
)}
|
||
{message ? <p role="status">{message}</p> : null}
|
||
</section>
|
||
);
|
||
}
|