-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
63 lines (52 loc) · 1.58 KB
/
middleware.go
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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
)
// JWTMiddleware validates JWT tokens in the Authorization header
func JWTMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// Extract the token from the Authorization header
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header missing or invalid"})
c.Abort()
return
}
// Validate the token
token, err := jwt.Parse(authHeader, func(token *jwt.Token) (interface{}, error) {
// Ensure the signing method is what we expect
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
return jwtSecret, nil
})
// Handle token validation errors
if err != nil || !token.Valid {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
c.Abort()
return
}
// Extract claims and pass them to the context
if claims, ok := token.Claims.(jwt.MapClaims); ok {
// Extract the user_id claim
userID, ok := claims["user_id"].(float64) // JWT numeric claims are often float64
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid user_id in token claims"})
c.Abort()
return
}
// Convert userID to int or string if needed
intUserID := int(userID)
// Save user ID in the context
c.Set("user_id", intUserID)
} else {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token claims"})
c.Abort()
return
}
// Token is valid; continue to the next handler
c.Next()
}
}