Files
xinfra/server/internal/handler/context.go
T

44 lines
992 B
Go
Raw Normal View History

2026-07-13 14:39:56 +08:00
package handler
import (
"net/http"
"strings"
"github.com/1024XEngineer/xinfra/server/internal/auth"
"github.com/1024XEngineer/xinfra/server/internal/config"
2026-07-13 14:39:56 +08:00
"github.com/gin-gonic/gin"
)
const ClaimsKey = "claims"
func AuthMiddleware(cfg config.Config) gin.HandlerFunc {
return func(c *gin.Context) {
value := c.GetHeader("Authorization")
tokenValue := ""
if strings.HasPrefix(value, "Bearer ") {
tokenValue = strings.TrimPrefix(value, "Bearer ")
}
if tokenValue == "" {
2026-07-13 14:39:56 +08:00
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing bearer token"})
return
}
claims, err := auth.Parse(cfg.JWTSecret, tokenValue)
2026-07-13 14:39:56 +08:00
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
return
}
c.Set(ClaimsKey, claims)
c.Next()
}
}
func CurrentClaims(c *gin.Context) (*auth.Claims, bool) {
value, ok := c.Get(ClaimsKey)
if !ok {
return nil, false
}
claims, ok := value.(*auth.Claims)
return claims, ok
}