mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-12-22 22:32:41 +00:00
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
func AuthRequired() gin.HandlerFunc {
|
|
secret := os.Getenv("JWT_SECRET")
|
|
if secret == "" {
|
|
secret = "dev-secret-change-me"
|
|
}
|
|
return func(c *gin.Context) {
|
|
auth := c.GetHeader("Authorization")
|
|
if !strings.HasPrefix(auth, "Bearer ") {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
tokenStr := strings.TrimPrefix(auth, "Bearer ")
|
|
token, err := jwt.Parse(tokenStr, func(t *jwt.Token) (interface{}, error) {
|
|
return []byte(secret), nil
|
|
})
|
|
if err != nil || !token.Valid {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
|
if v, ok := claims["role"].(string); ok {
|
|
c.Set("role", v)
|
|
}
|
|
if v, ok := claims["id"].(float64); ok {
|
|
c.Set("user_id", int(v))
|
|
}
|
|
c.Next()
|
|
return
|
|
}
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
}
|
|
}
|
|
|
|
func RequireRole(roles ...string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
roleVal, ok := c.Get("role")
|
|
if !ok {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
role := roleVal.(string)
|
|
for _, r := range roles {
|
|
if r == role {
|
|
c.Next()
|
|
return
|
|
}
|
|
}
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
}
|
|
}
|