1Panel/backend/middleware/jwt.go

29 lines
670 B
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
package middleware
import (
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/constant"
jwtUtils "github.com/1Panel-dev/1Panel/backend/utils/jwt"
2022-08-16 23:30:23 +08:00
"github.com/gin-gonic/gin"
)
func JwtAuth() gin.HandlerFunc {
return func(c *gin.Context) {
2022-09-08 18:47:15 +08:00
token := c.Request.Header.Get(constant.JWTHeaderName)
2022-08-16 23:30:23 +08:00
if token == "" {
c.Next()
return
}
j := jwtUtils.NewJWT()
claims, err := j.ParseToken(token)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrTypeInternalServer, err)
return
}
c.Set("claims", claims)
c.Set("authMethod", constant.AuthMethodJWT)
c.Next()
}
}