2026-04-22 16:01:59 +08:00
|
|
|
|
package middleware
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
2026-04-22 16:01:59 +08:00
|
|
|
|
"with_you/internal/pkg/response"
|
|
|
|
|
|
"with_you/internal/service"
|
2026-03-09 21:28:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Auth 认证中间件
|
refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion.
- Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects.
- Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management.
- Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering.
- Improve database initialization by moving it from `internal/model` to `internal/database`.
- Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool.
- Enhance error handling and security by implementing fail-fast checks for encryption key length during startup.
- Clean up unused code, including the `avatar` package and several unused DTOs.
2026-06-15 03:41:59 +08:00
|
|
|
|
func Auth(jwtService service.JWTService) gin.HandlerFunc {
|
2026-03-09 21:28:58 +08:00
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
|
authHeader := c.GetHeader("Authorization")
|
|
|
|
|
|
|
|
|
|
|
|
if authHeader == "" {
|
|
|
|
|
|
response.Unauthorized(c, "authorization header is required")
|
|
|
|
|
|
c.Abort()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 提取Token
|
2026-03-30 04:49:35 +08:00
|
|
|
|
prefix, token, found := strings.Cut(authHeader, " ")
|
|
|
|
|
|
if !found || prefix != "Bearer" {
|
2026-03-09 21:28:58 +08:00
|
|
|
|
response.Unauthorized(c, "invalid authorization header format")
|
|
|
|
|
|
c.Abort()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证Token
|
|
|
|
|
|
claims, err := jwtService.ParseToken(token)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
response.Unauthorized(c, "invalid token")
|
|
|
|
|
|
c.Abort()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将用户信息存入上下文
|
|
|
|
|
|
c.Set("user_id", claims.UserID)
|
|
|
|
|
|
c.Set("username", claims.Username)
|
|
|
|
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// OptionalAuth 可选认证中间件
|
refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion.
- Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects.
- Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management.
- Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering.
- Improve database initialization by moving it from `internal/model` to `internal/database`.
- Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool.
- Enhance error handling and security by implementing fail-fast checks for encryption key length during startup.
- Clean up unused code, including the `avatar` package and several unused DTOs.
2026-06-15 03:41:59 +08:00
|
|
|
|
func OptionalAuth(jwtService service.JWTService) gin.HandlerFunc {
|
2026-03-09 21:28:58 +08:00
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
|
authHeader := c.GetHeader("Authorization")
|
|
|
|
|
|
if authHeader == "" {
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 提取Token
|
2026-03-30 04:49:35 +08:00
|
|
|
|
prefix, token, found := strings.Cut(authHeader, " ")
|
|
|
|
|
|
if !found || prefix != "Bearer" {
|
2026-03-09 21:28:58 +08:00
|
|
|
|
c.Next()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证Token
|
|
|
|
|
|
claims, err := jwtService.ParseToken(token)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将用户信息存入上下文
|
|
|
|
|
|
c.Set("user_id", claims.UserID)
|
|
|
|
|
|
c.Set("username", claims.Username)
|
|
|
|
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|