Files
VLoop/internal/middleware/jwt.go
2025-12-16 18:50:10 +08:00

98 lines
2.6 KiB
Go

// internal/middleware/jwt.go
package middleware
import (
"errors"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/auth"
)
// JWTAuth check jwt token and ensure it matches the currently stored token.
func JWTAuth(accountRepo *account.AccountRepository) gin.HandlerFunc {
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
}
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
}
c.Set("accountID", claims.AccountID)
c.Set("username", claims.Username)
c.Next()
}
}
func SoftJWTAuth(accountRepo *account.AccountRepository) gin.HandlerFunc {
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
}
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
}
c.Set("accountID", claims.AccountID)
c.Set("username", claims.Username)
c.Next()
}
}
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
}