| 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 |
| } |
|
|
| |
| func AuthMiddleware(cfg *config.Config) gin.HandlerFunc { |
| return func(c *gin.Context) { |
| |
| tokenString, err := c.Cookie(cfg.CookieName) |
| if err != nil { |
| |
| authHeader := c.GetHeader("Authorization") |
| if authHeader != "" && strings.HasPrefix(authHeader, "Bearer ") { |
| tokenString = strings.TrimPrefix(authHeader, "Bearer ") |
| } |
| } |
|
|
| if tokenString == "" { |
| c.Next() |
| return |
| } |
|
|
| |
| 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 |
| } |
|
|
| |
| c.Set("user_id", claims.UserID) |
| c.Set("username", claims.Username) |
| c.Set("authenticated", true) |
|
|
| c.Next() |
| } |
| } |
|
|
| |
| func RequireAuth() gin.HandlerFunc { |
| return func(c *gin.Context) { |
| authenticated, exists := c.Get("authenticated") |
| if !exists || !authenticated.(bool) { |
| |
| 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() |
| } |
| } |
|
|
| |
| func GetUserID(c *gin.Context) (uint, bool) { |
| userID, exists := c.Get("user_id") |
| if !exists { |
| return 0, false |
| } |
| return userID.(uint), true |
| } |
|
|
| |
| func GetUsername(c *gin.Context) (string, bool) { |
| username, exists := c.Get("username") |
| if !exists { |
| return "", false |
| } |
| return username.(string), true |
| } |
|
|