扩展认证逻辑支持 API Token 和动态权限解析,更新配置结构及 Swagger 文档
This commit is contained in:
@@ -21,10 +21,10 @@ func NewConfigHandler() *ConfigHandler {
|
||||
// @Tags Admin
|
||||
// @Security AdminAuth
|
||||
// @Produce json
|
||||
// @Success 200 {object} config.Config
|
||||
// @Success 200 {object} model.Response{data=config.Config}
|
||||
// @Router /admin/config [get]
|
||||
func (h *ConfigHandler) GetConfig(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, config.GlobalConfig)
|
||||
c.JSON(http.StatusOK, model.SuccessResponse(config.GlobalConfig))
|
||||
}
|
||||
|
||||
// UpdateConfig 更新配置
|
||||
@@ -35,7 +35,7 @@ func (h *ConfigHandler) GetConfig(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param config body config.Config true "新配置内容"
|
||||
// @Success 200 {object} model.Response
|
||||
// @Success 200 {object} model.Response{data=config.Config}
|
||||
// @Failure 400 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /admin/config [put]
|
||||
@@ -66,5 +66,5 @@ func (h *ConfigHandler) UpdateConfig(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.SuccessResponse("Config updated successfully and hot-reloaded"))
|
||||
c.JSON(http.StatusOK, model.SuccessResponse(config.GlobalConfig))
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ type CreateTokenResponse struct {
|
||||
|
||||
// ListTokens 获取 API Token 列表
|
||||
// @Summary 获取 API Token 列表
|
||||
// @Description 获取系统中所有 API Token 的详详信息(不包含哈希)
|
||||
// @Description 获取系统中所有 API Token 的详细信息(不包含哈希)
|
||||
// @Tags Admin
|
||||
// @Security AdminAuth
|
||||
// @Produce json
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func AdminAuth() gin.HandlerFunc {
|
||||
tokenService := service.NewTokenService()
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
@@ -27,29 +28,41 @@ func AdminAuth() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := auth.ParseToken(parts[1])
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Invalid or expired token"))
|
||||
c.Abort()
|
||||
tokenStr := parts[1]
|
||||
|
||||
// 1. 尝试解析为管理员 JWT
|
||||
claims, err := auth.ParseToken(tokenStr)
|
||||
if err == nil {
|
||||
c.Set("admin_id", claims.AdminID)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("admin_id", claims.AdminID)
|
||||
c.Next()
|
||||
// 2. 尝试解析为 API Token (如果配置允许)
|
||||
if config.GlobalConfig.APIToken.Enabled && config.GlobalConfig.APIToken.AllowAdminAPI {
|
||||
token, err := tokenService.ValidateToken(tokenStr, model.ScopeAdmin)
|
||||
if err == nil {
|
||||
c.Set("token_id", token.ID)
|
||||
c.Set("token_scope", token.Scope)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Invalid or expired token"))
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
func APITokenAuth(requiredScope string) gin.HandlerFunc {
|
||||
func APITokenAuth(requiredScope string, optional bool) gin.HandlerFunc {
|
||||
tokenService := service.NewTokenService()
|
||||
return func(c *gin.Context) {
|
||||
if !config.GlobalConfig.APIToken.Enabled {
|
||||
c.JSON(http.StatusForbidden, model.ErrorResponse(model.CodeForbidden, "API Token is disabled"))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
if optional {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Authorization header required"))
|
||||
c.Abort()
|
||||
return
|
||||
@@ -57,13 +70,31 @@ func APITokenAuth(requiredScope string) gin.HandlerFunc {
|
||||
|
||||
parts := strings.SplitN(authHeader, " ", 2)
|
||||
if !(len(parts) == 2 && parts[0] == "Bearer") {
|
||||
if optional {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Invalid authorization format"))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if !config.GlobalConfig.APIToken.Enabled {
|
||||
if optional {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusForbidden, model.ErrorResponse(model.CodeForbidden, "API Token is disabled"))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
token, err := tokenService.ValidateToken(parts[1], requiredScope)
|
||||
if err != nil {
|
||||
if optional {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, err.Error()))
|
||||
c.Abort()
|
||||
return
|
||||
|
||||
@@ -16,11 +16,23 @@ func NewConfigHandler() *ConfigHandler {
|
||||
|
||||
// PublicConfig 公开配置结构
|
||||
type PublicConfig struct {
|
||||
Site config.SiteConfig `json:"site"`
|
||||
Upload config.UploadConfig `json:"upload"`
|
||||
APIToken struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
} `json:"api_token"`
|
||||
Site config.SiteConfig `json:"site"`
|
||||
Security PublicSecurityConfig `json:"security"`
|
||||
Upload config.UploadConfig `json:"upload"`
|
||||
APIToken PublicAPITokenConfig `json:"api_token"`
|
||||
Storage PublicStorageConfig `json:"storage"`
|
||||
}
|
||||
|
||||
type PublicSecurityConfig struct {
|
||||
PickupCodeLength int `json:"pickup_code_length"`
|
||||
}
|
||||
|
||||
type PublicAPITokenConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type PublicStorageConfig struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// GetPublicConfig 获取非敏感配置
|
||||
@@ -32,10 +44,18 @@ type PublicConfig struct {
|
||||
// @Router /api/config [get]
|
||||
func (h *ConfigHandler) GetPublicConfig(c *gin.Context) {
|
||||
pub := PublicConfig{
|
||||
Site: config.GlobalConfig.Site,
|
||||
Site: config.GlobalConfig.Site,
|
||||
Security: PublicSecurityConfig{
|
||||
PickupCodeLength: config.GlobalConfig.Security.PickupCodeLength,
|
||||
},
|
||||
Upload: config.GlobalConfig.Upload,
|
||||
APIToken: PublicAPITokenConfig{
|
||||
Enabled: config.GlobalConfig.APIToken.Enabled,
|
||||
},
|
||||
Storage: PublicStorageConfig{
|
||||
Type: config.GlobalConfig.Storage.Type,
|
||||
},
|
||||
}
|
||||
pub.APIToken.Enabled = config.GlobalConfig.APIToken.Enabled
|
||||
|
||||
c.JSON(http.StatusOK, model.SuccessResponse(pub))
|
||||
}
|
||||
|
||||
@@ -29,8 +29,9 @@ type PickupResponse struct {
|
||||
|
||||
// DownloadBatch 批量下载文件 (ZIP)
|
||||
// @Summary 批量下载文件
|
||||
// @Description 根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载
|
||||
// @Description 根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载。可选提供带 pickup scope 的 API Token。
|
||||
// @Tags Public
|
||||
// @Security APITokenAuth
|
||||
// @Param pickup_code path string true "取件码"
|
||||
// @Produce application/zip
|
||||
// @Success 200 {file} file
|
||||
@@ -82,8 +83,9 @@ func NewPickupHandler() *PickupHandler {
|
||||
|
||||
// Pickup 获取批次信息
|
||||
// @Summary 获取批次信息
|
||||
// @Description 根据取件码获取文件批次详详情和文件列表
|
||||
// @Description 根据取件码获取文件批次详细信息和文件列表。可选提供带 pickup scope 的 API Token。
|
||||
// @Tags Public
|
||||
// @Security APITokenAuth
|
||||
// @Produce json
|
||||
// @Param pickup_code path string true "取件码"
|
||||
// @Success 200 {object} model.Response{data=PickupResponse}
|
||||
@@ -122,8 +124,9 @@ func (h *PickupHandler) Pickup(c *gin.Context) {
|
||||
|
||||
// DownloadFile 下载单个文件
|
||||
// @Summary 下载单个文件
|
||||
// @Description 根据文件 ID 下载单个文件
|
||||
// @Description 根据文件 ID 下载单个文件。可选提供带 pickup scope 的 API Token。
|
||||
// @Tags Public
|
||||
// @Security APITokenAuth
|
||||
// @Param file_id path string true "文件 ID (UUID)"
|
||||
// @Produce application/octet-stream
|
||||
// @Success 200 {file} file
|
||||
|
||||
@@ -29,10 +29,11 @@ type UploadResponse struct {
|
||||
|
||||
// Upload 上传文件并生成取件码
|
||||
// @Summary 上传文件
|
||||
// @Description 上传一个或多个文件并创建一个提取批次
|
||||
// @Description 上传一个或多个文件并创建一个提取批次。如果配置了 require_token,则必须提供带 upload scope 的 API Token。
|
||||
// @Tags Public
|
||||
// @Accept multipart/form-data
|
||||
// @Produce json
|
||||
// @Security APITokenAuth
|
||||
// @Param files formData file true "文件列表"
|
||||
// @Param remark formData string false "备注"
|
||||
// @Param expire_type formData string false "过期类型 (time/download/permanent)"
|
||||
@@ -105,10 +106,11 @@ type UploadTextRequest struct {
|
||||
|
||||
// UploadText 发送长文本并生成取件码
|
||||
// @Summary 发送长文本
|
||||
// @Description 中转一段长文本内容并创建一个提取批次
|
||||
// @Description 中转一段长文本内容并创建一个提取批次。如果配置了 require_token,则必须提供带 upload scope 的 API Token。
|
||||
// @Tags Public
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security APITokenAuth
|
||||
// @Param request body UploadTextRequest true "文本内容及配置"
|
||||
// @Success 200 {object} model.Response{data=UploadResponse}
|
||||
// @Failure 400 {object} model.Response
|
||||
|
||||
@@ -8,61 +8,62 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Site SiteConfig `yaml:"site"`
|
||||
Security SecurityConfig `yaml:"security"`
|
||||
Upload UploadConfig `yaml:"upload"`
|
||||
Storage StorageConfig `yaml:"storage"`
|
||||
APIToken APITokenConfig `yaml:"api_token"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
Site SiteConfig `yaml:"site" json:"site"` // 站点设置
|
||||
Security SecurityConfig `yaml:"security" json:"security"` // 安全设置
|
||||
Upload UploadConfig `yaml:"upload" json:"upload"` // 上传设置
|
||||
Storage StorageConfig `yaml:"storage" json:"storage"` // 存储设置
|
||||
APIToken APITokenConfig `yaml:"api_token" json:"api_token"` // API Token 设置
|
||||
Database DatabaseConfig `yaml:"database" json:"database"` // 数据库设置
|
||||
}
|
||||
|
||||
type SiteConfig struct {
|
||||
Name string `yaml:"name"`
|
||||
Description string `yaml:"description"`
|
||||
Name string `yaml:"name" json:"name"` // 站点名称
|
||||
Description string `yaml:"description" json:"description"` // 站点描述
|
||||
}
|
||||
|
||||
type SecurityConfig struct {
|
||||
AdminPasswordHash string `yaml:"admin_password_hash"`
|
||||
PickupCodeLength int `yaml:"pickup_code_length"`
|
||||
PickupFailLimit int `yaml:"pickup_fail_limit"`
|
||||
JWTSecret string `yaml:"jwt_secret"`
|
||||
AdminPasswordHash string `yaml:"admin_password_hash" json:"admin_password_hash"` // 管理员密码哈希 (bcrypt)
|
||||
PickupCodeLength int `yaml:"pickup_code_length" json:"pickup_code_length"` // 取件码长度
|
||||
PickupFailLimit int `yaml:"pickup_fail_limit" json:"pickup_fail_limit"` // 取件失败尝试限制
|
||||
JWTSecret string `yaml:"jwt_secret" json:"jwt_secret"` // JWT 签名密钥
|
||||
}
|
||||
|
||||
type UploadConfig struct {
|
||||
MaxFileSizeMB int64 `yaml:"max_file_size_mb"`
|
||||
MaxBatchFiles int `yaml:"max_batch_files"`
|
||||
MaxRetentionDays int `yaml:"max_retention_days"`
|
||||
MaxFileSizeMB int64 `yaml:"max_file_size_mb" json:"max_file_size_mb"` // 单个文件最大大小 (MB)
|
||||
MaxBatchFiles int `yaml:"max_batch_files" json:"max_batch_files"` // 每个批次最大文件数
|
||||
MaxRetentionDays int `yaml:"max_retention_days" json:"max_retention_days"` // 最大保留天数
|
||||
RequireToken bool `yaml:"require_token" json:"require_token"` // 是否强制要求上传 Token
|
||||
}
|
||||
|
||||
type StorageConfig struct {
|
||||
Type string `yaml:"type"`
|
||||
Type string `yaml:"type" json:"type"` // 存储类型: local, webdav, s3
|
||||
Local struct {
|
||||
Path string `yaml:"path"`
|
||||
} `yaml:"local"`
|
||||
Path string `yaml:"path" json:"path"` // 本地存储路径
|
||||
} `yaml:"local" json:"local"`
|
||||
WebDAV struct {
|
||||
URL string `yaml:"url"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
Root string `yaml:"root"`
|
||||
} `yaml:"webdav"`
|
||||
URL string `yaml:"url" json:"url"` // WebDAV 地址
|
||||
Username string `yaml:"username" json:"username"` // WebDAV 用户名
|
||||
Password string `yaml:"password" json:"password"` // WebDAV 密码
|
||||
Root string `yaml:"root" json:"root"` // WebDAV 根目录
|
||||
} `yaml:"webdav" json:"webdav"`
|
||||
S3 struct {
|
||||
Endpoint string `yaml:"endpoint"`
|
||||
Region string `yaml:"region"`
|
||||
AccessKey string `yaml:"access_key"`
|
||||
SecretKey string `yaml:"secret_key"`
|
||||
Bucket string `yaml:"bucket"`
|
||||
UseSSL bool `yaml:"use_ssl"`
|
||||
} `yaml:"s3"`
|
||||
Endpoint string `yaml:"endpoint" json:"endpoint"` // S3 端点
|
||||
Region string `yaml:"region" json:"region"` // S3 区域
|
||||
AccessKey string `yaml:"access_key" json:"access_key"` // S3 Access Key
|
||||
SecretKey string `yaml:"secret_key" json:"secret_key"` // S3 Secret Key
|
||||
Bucket string `yaml:"bucket" json:"bucket"` // S3 Bucket
|
||||
UseSSL bool `yaml:"use_ssl" json:"use_ssl"` // 是否使用 SSL
|
||||
} `yaml:"s3" json:"s3"`
|
||||
}
|
||||
|
||||
type APITokenConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
AllowAdminAPI bool `yaml:"allow_admin_api"`
|
||||
MaxTokens int `yaml:"max_tokens"`
|
||||
Enabled bool `yaml:"enabled" json:"enabled"` // 是否启用 API Token
|
||||
AllowAdminAPI bool `yaml:"allow_admin_api" json:"allow_admin_api"` // 是否允许 API Token 访问管理接口
|
||||
MaxTokens int `yaml:"max_tokens" json:"max_tokens"` // 最大 Token 数量
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Path string `yaml:"path"`
|
||||
Path string `yaml:"path" json:"path"` // 数据库文件路径
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -4,6 +4,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ScopeUpload = "upload" // 上传权限
|
||||
ScopePickup = "pickup" // 取件/下载权限
|
||||
ScopeAdmin = "admin" // 管理权限
|
||||
)
|
||||
|
||||
type APIToken struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
Reference in New Issue
Block a user