扩展认证逻辑支持 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
|
||||
|
||||
Reference in New Issue
Block a user