|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/gin-gonic/gin" |
| 8 | + "github.com/golang-jwt/jwt/v5" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + AccessSecret = []byte("your-access-secret") // Replace with env/config in production |
| 13 | + RefreshSecret = []byte("your-refresh-secret") // Replace with env/config in production |
| 14 | +) |
| 15 | + |
| 16 | +func GenerateAccessToken(userID int64) (string, error) { |
| 17 | + claims := jwt.MapClaims{ |
| 18 | + "user_id": userID, |
| 19 | + "exp": time.Now().Add(15 * time.Minute).Unix(), |
| 20 | + } |
| 21 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 22 | + return token.SignedString(AccessSecret) |
| 23 | +} |
| 24 | + |
| 25 | +func GenerateRefreshToken(userID int64) (string, error) { |
| 26 | + claims := jwt.MapClaims{ |
| 27 | + "user_id": userID, |
| 28 | + "exp": time.Now().Add(7 * 24 * time.Hour).Unix(), |
| 29 | + } |
| 30 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 31 | + return token.SignedString(RefreshSecret) |
| 32 | +} |
| 33 | + |
| 34 | +func ParseRefreshToken(tokenStr string) (int64, error) { |
| 35 | + token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) { |
| 36 | + return RefreshSecret, nil |
| 37 | + }) |
| 38 | + if err != nil || !token.Valid { |
| 39 | + return 0, errors.New("invalid refresh token") |
| 40 | + } |
| 41 | + claims, ok := token.Claims.(jwt.MapClaims) |
| 42 | + if !ok { |
| 43 | + return 0, errors.New("invalid claims") |
| 44 | + } |
| 45 | + userID, ok := claims["user_id"].(float64) |
| 46 | + if !ok { |
| 47 | + return 0, errors.New("invalid user_id") |
| 48 | + } |
| 49 | + return int64(userID), nil |
| 50 | +} |
| 51 | + |
| 52 | +func ParseAccessToken(tokenStr string) (int64, error) { |
| 53 | + token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) { |
| 54 | + return AccessSecret, nil |
| 55 | + }) |
| 56 | + if err != nil || !token.Valid { |
| 57 | + return 0, errors.New("invalid access token") |
| 58 | + } |
| 59 | + claims, ok := token.Claims.(jwt.MapClaims) |
| 60 | + if !ok { |
| 61 | + return 0, errors.New("invalid claims") |
| 62 | + } |
| 63 | + userID, ok := claims["user_id"].(float64) |
| 64 | + if !ok { |
| 65 | + return 0, errors.New("invalid user_id") |
| 66 | + } |
| 67 | + return int64(userID), nil |
| 68 | +} |
| 69 | + |
| 70 | +// Gin middleware to protect routes |
| 71 | +// Usage: router.Use(auth.AuthMiddleware()) |
| 72 | +func AuthMiddleware() func(c *gin.Context) { |
| 73 | + return func(c *gin.Context) { |
| 74 | + tokenStr := c.GetHeader("Authorization") |
| 75 | + if len(tokenStr) > 7 && tokenStr[:7] == "Bearer " { |
| 76 | + tokenStr = tokenStr[7:] |
| 77 | + } |
| 78 | + userID, err := ParseAccessToken(tokenStr) |
| 79 | + if err != nil { |
| 80 | + c.AbortWithStatus(401) |
| 81 | + return |
| 82 | + } |
| 83 | + c.Set("user_id", userID) |
| 84 | + c.Next() |
| 85 | + } |
| 86 | +} |
0 commit comments