File size: 2,100 Bytes
421b222 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | package middleware
import (
"net/http"
"patbin/config"
"strings"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
)
type Claims struct {
UserID uint `json:"user_id"`
Username string `json:"username"`
jwt.RegisteredClaims
}
// AuthMiddleware validates JWT token and sets user info in context
func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
return func(c *gin.Context) {
// Try to get token from cookie first
tokenString, err := c.Cookie(cfg.CookieName)
if err != nil {
// Try Authorization header
authHeader := c.GetHeader("Authorization")
if authHeader != "" && strings.HasPrefix(authHeader, "Bearer ") {
tokenString = strings.TrimPrefix(authHeader, "Bearer ")
}
}
if tokenString == "" {
c.Next()
return
}
// Parse and validate token
claims := &Claims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(cfg.JWTSecret), nil
})
if err != nil || !token.Valid {
c.Next()
return
}
// Set user info in context
c.Set("user_id", claims.UserID)
c.Set("username", claims.Username)
c.Set("authenticated", true)
c.Next()
}
}
// RequireAuth ensures user is authenticated
func RequireAuth() gin.HandlerFunc {
return func(c *gin.Context) {
authenticated, exists := c.Get("authenticated")
if !exists || !authenticated.(bool) {
// Check if API request or web request
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"})
} else {
c.Redirect(http.StatusFound, "/login")
}
c.Abort()
return
}
c.Next()
}
}
// GetUserID returns the authenticated user ID from context
func GetUserID(c *gin.Context) (uint, bool) {
userID, exists := c.Get("user_id")
if !exists {
return 0, false
}
return userID.(uint), true
}
// GetUsername returns the authenticated username from context
func GetUsername(c *gin.Context) (string, bool) {
username, exists := c.Get("username")
if !exists {
return "", false
}
return username.(string), true
}
|