package middlewares import ( "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "net/http" "zhiyuan/pkg/app" "zhiyuan/pkg/errcode" "zhiyuan/pkg/utils" ) func JWT() gin.HandlerFunc { return func(c *gin.Context) { code := errcode.Success token := c.GetHeader("token") if token == "" { code = errcode.TokenEmpty } else { _, err := utils.ParseToken(token) if err != nil { switch err.(*jwt.ValidationError).Errors { case jwt.ValidationErrorExpired: code = errcode.TokenExpired default: code = errcode.TokenInvalid } } } if code.Code != errcode.Success.Code { app.Response(c, http.StatusUnauthorized, code, nil) c.Abort() return } c.Next() } } //var identityKey = "id" // //func GinJWTMiddlewareInit() (authMiddleware *jwt.GinJWTMiddleware) { // // authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{ // Realm: "test zone", // Key: []byte("secret key"), // Timeout: time.Hour * 12, // MaxRefresh: time.Hour, // IdentityKey: identityKey, // PayloadFunc: func(data interface{}) jwt.MapClaims { // if v, ok := data.(*models.User); ok { // return jwt.MapClaims{ // identityKey: v.ID, // } // } // return jwt.MapClaims{} // }, // IdentityHandler: func(c *gin.Context) interface{} { // claims := jwt.ExtractClaims(c) // return &models.User{ // ID: int(claims[identityKey].(float64)), // } // }, // Authenticator: func(c *gin.Context) (interface{}, error) { // var loginVals models.User // if err := c.ShouldBind(&loginVals); err != nil { // return "", jwt.ErrMissingLoginValues // } // username := loginVals.Username // password := loginVals.Password // // fmt.Println(username + "---" + password) // userID := models.CheckLogin(username, password) // fmt.Println(userID) // if userID > 0 { // return &models.User{ // ID: userID, // }, nil // } // // return nil, jwt.ErrFailedAuthentication // }, // LoginResponse: func(c *gin.Context, code int, token string, expire time.Time) { // c.JSON(code, utils.EchoSuc(gin.H{"token": token, "expire": expire})) // }, // Authorizator: func(data interface{}, c *gin.Context) bool { // return true // }, // Unauthorized: func(c *gin.Context, code int, message string) { // c.JSON(code, gin.H{ // "code": code, // "message": message, // }) // }, // // TokenLookup is a string in the form of ":" that is used // // to extract token from the request. // // Optional. Default value "header:Authorization". // // Possible values: // // - "header:" // // - "query:" // // - "cookie:" // // - "param:" // TokenLookup: "header: Authorization, query: token, cookie: jwt", // // TokenLookup: "query:token", // // TokenLookup: "cookie:token", // // // TokenHeadName is a string in the header. Default value is "Bearer" // TokenHeadName: "Bearer", // // // TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens. // TimeFunc: time.Now, // }) // if err != nil { // log.Fatal("JWT Error:" + err.Error()) // } // return //} // //func NoRouteHandler(c *gin.Context) { // claims := jwt.ExtractClaims(c) // log.Printf("NoRoute claims: %#v\n", claims) // c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"}) //}