feat(config): add secure API key generator in visual editor

This commit is contained in:
Supra4E8C
2026-02-16 21:49:45 +08:00
parent 8a4eb267f0
commit d09ea6aeab
4 changed files with 27 additions and 0 deletions

View File

@@ -106,6 +106,13 @@ function ApiKeysCardEditor({
const [inputValue, setInputValue] = useState('');
const [formError, setFormError] = useState('');
function generateSecureApiKey(): string {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const array = new Uint8Array(17);
crypto.getRandomValues(array);
return 'sk-' + Array.from(array, (b) => charset[b % charset.length]).join('');
}
const openAddModal = () => {
setEditingIndex(null);
setInputValue('');
@@ -162,6 +169,11 @@ function ApiKeysCardEditor({
);
};
const handleGenerate = () => {
setInputValue(generateSecureApiKey());
setFormError('');
};
return (
<div className="form-group" style={{ marginBottom: 0 }}>
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 12 }}>
@@ -233,6 +245,18 @@ function ApiKeysCardEditor({
disabled={disabled}
error={formError || undefined}
hint={t('config_management.visual.api_keys.input_hint')}
style={{ paddingRight: 148 }}
rightElement={
<Button
type="button"
variant="secondary"
size="sm"
onClick={handleGenerate}
disabled={disabled}
>
{t('config_management.visual.api_keys.generate')}
</Button>
}
/>
</Modal>
</div>

View File

@@ -1011,6 +1011,7 @@
"api_keys": {
"label": "API Keys List (api-keys)",
"add": "Add API Key",
"generate": "Generate",
"empty": "No API keys",
"hint": "Each entry represents an API key (consistent with 'API Key Management' page style)",
"edit_title": "Edit API Key",

View File

@@ -1016,6 +1016,7 @@
"api_keys": {
"label": "Список API-ключей (api-keys)",
"add": "Добавить API-ключ",
"generate": "Сгенерировать",
"empty": "API-ключи отсутствуют",
"hint": "Каждая запись — это API-ключ (в том же стиле, что и на странице управления API-ключами)",
"edit_title": "Редактирование API-ключа",

View File

@@ -1011,6 +1011,7 @@
"api_keys": {
"label": "API 密钥列表 (api-keys)",
"add": "添加 API 密钥",
"generate": "生成",
"empty": "暂无 API 密钥",
"hint": "每个条目代表一个 API 密钥(与 「API 密钥管理」 页面样式一致)",
"edit_title": "编辑 API 密钥",