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
@@ -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>