gin funn and testing
This commit is contained in:
46
security/middleware/jwt/jwt.go
Normal file
46
security/middleware/jwt/jwt.go
Normal file
@ -0,0 +1,46 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gin-server/lib/messages"
|
||||
"gin-server/security"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// JWT is jwt middleware
|
||||
func JWT() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var code int
|
||||
var data interface{}
|
||||
|
||||
code = messages.SUCCESS
|
||||
token := c.Query("token")
|
||||
if token == "" {
|
||||
code = messages.INVALID_PARAMS
|
||||
} else {
|
||||
_, err := security.ParseToken(token)
|
||||
if err != nil {
|
||||
switch err.(*jwt.ValidationError).Errors {
|
||||
case jwt.ValidationErrorExpired:
|
||||
code = messages.ERROR_AUTH_CHECK_TOKEN_TIMEOUT
|
||||
default:
|
||||
code = messages.ERROR_AUTH_CHECK_TOKEN_FAIL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if code != messages.SUCCESS {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"code": code,
|
||||
"msg": messages.GetMsg(code),
|
||||
"data": data,
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user