mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-11 18:10:53 +08:00
Merge pull request #109 from MisonL/feature/newapi-token-validation
fix: validate NEWAPI token input across UI and API
This commit is contained in:
@@ -1367,6 +1367,28 @@ function closeNewapiServiceModal() {
|
||||
elements.newapiServiceEditModal.classList.remove('active');
|
||||
}
|
||||
|
||||
function validateNewapiApiKeyInput(apiKey, { required = false } = {}) {
|
||||
const normalizedApiKey = String(apiKey || '').trim();
|
||||
if (!normalizedApiKey) {
|
||||
if (required) {
|
||||
return '新增服务时 Root Token / API Key 不能为空';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
for (const char of normalizedApiKey) {
|
||||
const code = char.charCodeAt(0);
|
||||
if (code > 127) {
|
||||
return 'Root Token / API Key 只能包含 ASCII 字符,请粘贴实际令牌,不要填写中文说明';
|
||||
}
|
||||
if (code < 32 || code === 127) {
|
||||
return 'Root Token / API Key 包含非法控制字符';
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
async function editNewapiService(id) {
|
||||
try {
|
||||
const service = await api.get(`/newapi-services/${id}`);
|
||||
@@ -1392,8 +1414,9 @@ async function handleSaveNewapiService(e) {
|
||||
toast.error('名称和 API URL 不能为空');
|
||||
return;
|
||||
}
|
||||
if (!id && !apiKey) {
|
||||
toast.error('新增服务时 Root Token / API Key 不能为空');
|
||||
const apiKeyValidationError = validateNewapiApiKeyInput(apiKey, { required: !id });
|
||||
if (apiKeyValidationError) {
|
||||
toast.error(apiKeyValidationError);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user