/* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy * @LastEditTime: 2021-06-10 21:49:20 * @FilePath: /potato/internal/middleware/jwt.go */ package middleware import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" "github.com/viletyy/potato/pkg/app" "github.com/viletyy/potato/pkg/errcode" ) func JWT() gin.HandlerFunc { return func(c *gin.Context) { var ( token string ecode = errcode.Success ) if s, exist := c.GetQuery("token"); exist { token = s } else { token = c.GetHeader("token") } if token == "" { ecode = errcode.InvalidParams } else { _, err := app.ParseToken(token) if err != nil { switch err.(*jwt.ValidationError).Errors { case jwt.ValidationErrorExpired: ecode = errcode.UnauthorizedTokenTimeout default: ecode = errcode.UnauthorizedTokenError } } } if ecode != errcode.Success { response := app.NewResponse(c) response.ToErrorResponse(ecode) c.Abort() return } c.Next() } }