扩展认证逻辑支持 API Token 和动态权限解析,更新配置结构及 Swagger 文档

This commit is contained in:
2026-01-14 16:31:58 +08:00
parent fe656fb298
commit 2ea2c93bb4
13 changed files with 634 additions and 207 deletions

View File

@@ -31,7 +31,7 @@ const docTemplate = `{
"AdminAuth": []
}
],
"description": "获取系统中所有 API Token 的详信息(不包含哈希)",
"description": "获取系统中所有 API Token 的详信息(不包含哈希)",
"produces": [
"application/json"
],
@@ -445,7 +445,19 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/config.Config"
"allOf": [
{
"$ref": "#/definitions/model.Response"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/config.Config"
}
}
}
]
}
}
}
@@ -482,7 +494,19 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.Response"
"allOf": [
{
"$ref": "#/definitions/model.Response"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/config.Config"
}
}
}
]
}
},
"400": {
@@ -554,7 +578,12 @@ const docTemplate = `{
},
"/api/batches": {
"post": {
"description": "上传一个或多个文件并创建一个提取批次",
"security": [
{
"APITokenAuth": []
}
],
"description": "上传一个或多个文件并创建一个提取批次。如果配置了 require_token则必须提供带 upload scope 的 API Token。",
"consumes": [
"multipart/form-data"
],
@@ -634,7 +663,12 @@ const docTemplate = `{
},
"/api/batches/text": {
"post": {
"description": "中转一段长文本内容并创建一个提取批次",
"security": [
{
"APITokenAuth": []
}
],
"description": "中转一段长文本内容并创建一个提取批次。如果配置了 require_token则必须提供带 upload scope 的 API Token。",
"consumes": [
"application/json"
],
@@ -692,7 +726,12 @@ const docTemplate = `{
},
"/api/batches/{pickup_code}": {
"get": {
"description": "根据取件码获取文件批次详详情和文件列表",
"security": [
{
"APITokenAuth": []
}
],
"description": "根据取件码获取文件批次详细信息和文件列表。可选提供带 pickup scope 的 API Token。",
"produces": [
"application/json"
],
@@ -739,7 +778,12 @@ const docTemplate = `{
},
"/api/batches/{pickup_code}/download": {
"get": {
"description": "根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载",
"security": [
{
"APITokenAuth": []
}
],
"description": "根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载。可选提供带 pickup scope 的 API Token。",
"produces": [
"application/zip"
],
@@ -806,7 +850,12 @@ const docTemplate = `{
},
"/api/files/{file_id}/download": {
"get": {
"description": "根据文件 ID 下载单个文件",
"security": [
{
"APITokenAuth": []
}
],
"description": "根据文件 ID 下载单个文件。可选提供带 pickup scope 的 API Token。",
"produces": [
"application/octet-stream"
],
@@ -940,13 +989,16 @@ const docTemplate = `{
"config.APITokenConfig": {
"type": "object",
"properties": {
"allowAdminAPI": {
"allow_admin_api": {
"description": "是否允许 API Token 访问管理接口",
"type": "boolean"
},
"enabled": {
"description": "是否启用 API Token",
"type": "boolean"
},
"maxTokens": {
"max_tokens": {
"description": "最大 Token 数量",
"type": "integer"
}
}
@@ -954,23 +1006,53 @@ const docTemplate = `{
"config.Config": {
"type": "object",
"properties": {
"apitoken": {
"$ref": "#/definitions/config.APITokenConfig"
"api_token": {
"description": "API Token 设置",
"allOf": [
{
"$ref": "#/definitions/config.APITokenConfig"
}
]
},
"database": {
"$ref": "#/definitions/config.DatabaseConfig"
"description": "数据库设置",
"allOf": [
{
"$ref": "#/definitions/config.DatabaseConfig"
}
]
},
"security": {
"$ref": "#/definitions/config.SecurityConfig"
"description": "安全设置",
"allOf": [
{
"$ref": "#/definitions/config.SecurityConfig"
}
]
},
"site": {
"$ref": "#/definitions/config.SiteConfig"
"description": "站点设置",
"allOf": [
{
"$ref": "#/definitions/config.SiteConfig"
}
]
},
"storage": {
"$ref": "#/definitions/config.StorageConfig"
"description": "存储设置",
"allOf": [
{
"$ref": "#/definitions/config.StorageConfig"
}
]
},
"upload": {
"$ref": "#/definitions/config.UploadConfig"
"description": "上传设置",
"allOf": [
{
"$ref": "#/definitions/config.UploadConfig"
}
]
}
}
},
@@ -978,6 +1060,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"path": {
"description": "数据库文件路径",
"type": "string"
}
}
@@ -985,16 +1068,20 @@ const docTemplate = `{
"config.SecurityConfig": {
"type": "object",
"properties": {
"adminPasswordHash": {
"admin_password_hash": {
"description": "管理员密码哈希 (bcrypt)",
"type": "string"
},
"jwtsecret": {
"jwt_secret": {
"description": "JWT 签名密钥",
"type": "string"
},
"pickupCodeLength": {
"pickup_code_length": {
"description": "取件码长度",
"type": "integer"
},
"pickupFailLimit": {
"pickup_fail_limit": {
"description": "取件失败尝试限制",
"type": "integer"
}
}
@@ -1003,9 +1090,11 @@ const docTemplate = `{
"type": "object",
"properties": {
"description": {
"description": "站点描述",
"type": "string"
},
"name": {
"description": "站点名称",
"type": "string"
}
}
@@ -1017,6 +1106,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"path": {
"description": "本地存储路径",
"type": "string"
}
}
@@ -1024,42 +1114,53 @@ const docTemplate = `{
"s3": {
"type": "object",
"properties": {
"accessKey": {
"access_key": {
"description": "S3 Access Key",
"type": "string"
},
"bucket": {
"description": "S3 Bucket",
"type": "string"
},
"endpoint": {
"description": "S3 端点",
"type": "string"
},
"region": {
"description": "S3 区域",
"type": "string"
},
"secretKey": {
"secret_key": {
"description": "S3 Secret Key",
"type": "string"
},
"useSSL": {
"use_ssl": {
"description": "是否使用 SSL",
"type": "boolean"
}
}
},
"type": {
"description": "存储类型: local, webdav, s3",
"type": "string"
},
"webDAV": {
"webdav": {
"type": "object",
"properties": {
"password": {
"description": "WebDAV 密码",
"type": "string"
},
"root": {
"description": "WebDAV 根目录",
"type": "string"
},
"url": {
"description": "WebDAV 地址",
"type": "string"
},
"username": {
"description": "WebDAV 用户名",
"type": "string"
}
}
@@ -1069,14 +1170,21 @@ const docTemplate = `{
"config.UploadConfig": {
"type": "object",
"properties": {
"maxBatchFiles": {
"max_batch_files": {
"description": "每个批次最大文件数",
"type": "integer"
},
"maxFileSizeMB": {
"max_file_size_mb": {
"description": "单个文件最大大小 (MB)",
"type": "integer"
},
"maxRetentionDays": {
"max_retention_days": {
"description": "最大保留天数",
"type": "integer"
},
"require_token": {
"description": "是否强制要求上传 Token",
"type": "boolean"
}
}
},
@@ -1228,25 +1336,50 @@ const docTemplate = `{
}
}
},
"public.PublicAPITokenConfig": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
}
},
"public.PublicConfig": {
"type": "object",
"properties": {
"api_token": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
}
"$ref": "#/definitions/public.PublicAPITokenConfig"
},
"security": {
"$ref": "#/definitions/public.PublicSecurityConfig"
},
"site": {
"$ref": "#/definitions/config.SiteConfig"
},
"storage": {
"$ref": "#/definitions/public.PublicStorageConfig"
},
"upload": {
"$ref": "#/definitions/config.UploadConfig"
}
}
},
"public.PublicSecurityConfig": {
"type": "object",
"properties": {
"pickup_code_length": {
"type": "integer"
}
}
},
"public.PublicStorageConfig": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"public.UploadResponse": {
"type": "object",
"properties": {
@@ -1291,8 +1424,14 @@ const docTemplate = `{
}
},
"securityDefinitions": {
"APITokenAuth": {
"description": "Type \"Bearer \u003cAPI-Token\u003e\" to authenticate. Required scope depends on the endpoint.",
"type": "apiKey",
"name": "Authorization",
"in": "header"
},
"AdminAuth": {
"description": "Type \"Bearer \u003cyour-jwt-token\u003e\" to authenticate.",
"description": "Type \"Bearer \u003cJWT-Token\u003e\" or \"Bearer \u003cAPI-Token\u003e\" to authenticate. API Token must have 'admin' scope.",
"type": "apiKey",
"name": "Authorization",
"in": "header"