2025-12-28 18:38:14 +08:00
|
|
|
package jwt
|
2025-12-05 13:27:04 +08:00
|
|
|
|
|
|
|
|
import (
|
2025-12-23 20:48:41 +08:00
|
|
|
"context"
|
2025-12-09 15:27:33 +08:00
|
|
|
"errors"
|
2025-12-23 20:48:41 +08:00
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2025-12-05 13:27:04 +08:00
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
2025-12-23 20:48:41 +08:00
|
|
|
"time"
|
2025-12-05 13:27:04 +08:00
|
|
|
|
2025-12-07 17:36:44 +08:00
|
|
|
"feedsystem_video_go/internal/account"
|
2025-12-05 13:27:04 +08:00
|
|
|
"feedsystem_video_go/internal/auth"
|
2025-12-28 18:38:14 +08:00
|
|
|
rediscache "feedsystem_video_go/internal/middleware/redis"
|
2025-12-23 20:48:41 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-12-05 13:27:04 +08:00
|
|
|
)
|
|
|
|
|
|
2025-12-07 17:36:44 +08:00
|
|
|
// JWTAuth check jwt token and ensure it matches the currently stored token.
|
2025-12-23 20:48:41 +08:00
|
|
|
func JWTAuth(accountRepo *account.AccountRepository, cache *rediscache.Client) gin.HandlerFunc {
|
2025-12-05 13:27:04 +08:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
authHeader := c.GetHeader("Authorization")
|
|
|
|
|
if authHeader == "" {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing authorization header"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parts := strings.SplitN(authHeader, " ", 2)
|
|
|
|
|
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization header"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokenString := parts[1]
|
|
|
|
|
|
|
|
|
|
claims, err := auth.ParseToken(tokenString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-23 20:48:41 +08:00
|
|
|
check(c, claims, tokenString, accountRepo, cache)
|
2025-12-05 13:27:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-09 15:27:33 +08:00
|
|
|
|
2025-12-23 20:48:41 +08:00
|
|
|
func SoftJWTAuth(accountRepo *account.AccountRepository, cache *rediscache.Client) gin.HandlerFunc {
|
2025-12-16 18:50:10 +08:00
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
authHeader := c.GetHeader("Authorization")
|
|
|
|
|
if authHeader == "" {
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parts := strings.SplitN(authHeader, " ", 2)
|
|
|
|
|
if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid authorization header"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokenString := parts[1]
|
|
|
|
|
|
|
|
|
|
claims, err := auth.ParseToken(tokenString)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid or expired token"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 20:48:41 +08:00
|
|
|
check(c, claims, tokenString, accountRepo, cache)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func check(c *gin.Context, claims *auth.Claims, tokenString string, accountRepo *account.AccountRepository, cache *rediscache.Client) {
|
|
|
|
|
key := fmt.Sprintf("account:%d", claims.AccountID)
|
|
|
|
|
|
|
|
|
|
// 先查 Redis
|
|
|
|
|
if cache != nil {
|
|
|
|
|
cacheCtx, cancel := context.WithTimeout(c.Request.Context(), 50*time.Millisecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
b, err := cache.GetBytes(cacheCtx, key)
|
|
|
|
|
if err == nil {
|
|
|
|
|
if string(b) != tokenString {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "token has been revoked"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Set("accountID", claims.AccountID)
|
|
|
|
|
c.Set("username", claims.Username)
|
|
|
|
|
c.Next()
|
2025-12-16 18:50:10 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-12-23 20:48:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redis 故障/未启用:查 DB 兜底
|
|
|
|
|
accountInfo, err := accountRepo.FindByID(c.Request.Context(), claims.AccountID)
|
|
|
|
|
if err != nil || accountInfo.Token == "" || accountInfo.Token != tokenString {
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "token has been revoked"})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-16 18:50:10 +08:00
|
|
|
|
2025-12-23 20:48:41 +08:00
|
|
|
if cache != nil {
|
|
|
|
|
cacheCtx, cancel := context.WithTimeout(c.Request.Context(), 50*time.Millisecond)
|
|
|
|
|
defer cancel()
|
2025-12-16 18:50:10 +08:00
|
|
|
|
2025-12-23 20:48:41 +08:00
|
|
|
if err := cache.SetBytes(cacheCtx, key, []byte(tokenString), 24*time.Hour); err != nil {
|
|
|
|
|
log.Printf("failed to set cache: %v", err)
|
|
|
|
|
}
|
2025-12-16 18:50:10 +08:00
|
|
|
}
|
2025-12-23 20:48:41 +08:00
|
|
|
|
|
|
|
|
c.Set("accountID", claims.AccountID)
|
|
|
|
|
c.Set("username", claims.Username)
|
|
|
|
|
c.Next()
|
|
|
|
|
|
2025-12-16 18:50:10 +08:00
|
|
|
}
|
2025-12-23 20:48:41 +08:00
|
|
|
|
2025-12-09 15:27:33 +08:00
|
|
|
func GetAccountID(c *gin.Context) (uint, error) {
|
|
|
|
|
uidValue, exists := c.Get("accountID")
|
|
|
|
|
if !exists {
|
|
|
|
|
return 0, errors.New("accountID not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accountID, ok := uidValue.(uint)
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0, errors.New("accountID has invalid type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return accountID, nil
|
|
|
|
|
}
|
2026-03-21 11:19:09 +08:00
|
|
|
|
|
|
|
|
func GetUsername(c *gin.Context) (string, error) {
|
|
|
|
|
val, exists := c.Get("username")
|
|
|
|
|
if !exists {
|
|
|
|
|
return "", errors.New("username not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
username, ok := val.(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return "", errors.New("username has invalid type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return username, nil
|
|
|
|
|
}
|