appleboy / gin-jwt

JWT Middleware for Gin framework
MIT License
2.73k stars 382 forks source link

How do I get multipart/form-data's token? #292

Closed 2w1nd closed 2 years ago

2w1nd commented 2 years ago

image my token in form-data,but TokenLookup seem no support it


    // TokenLookup is a string in the form of "<source>:<name>" that is used
    // to extract token from the request.
    // Optional. Default value "header:Authorization".
    // Possible values:
    // - "header:<name>"
    // - "query:<name>"
    // - "cookie:<name>"
    TokenLookup string
Starballoon commented 2 years ago

Ha, I just want to raise an issue about this problem. I guess that you have joined the training camp organized by ByteDance also. I modified a function of the 'auth_jwt.go' file as following to add an option to get token from form data in the request body.

func (mw *GinJWTMiddleware) jwtFromForm(c *gin.Context, key string) (string, error) {
    token := c.PostForm(key)
    if token == "" {return "", ErrEmptyParamToken}
        return token, nil
}
func (mw *GinJWTMiddleware) ParseToken(c *gin.Context) (*jwt.Token, error) {
    for _, method := range methods {
        switch k {
        case "header":   token, err = mw.jwtFromHeader(c, v)
        case "query":    token, err = mw.jwtFromQuery(c, v)
        case "cookie":   token, err = mw.jwtFromCookie(c, v)
        case "param":    token, err = mw.jwtFromParam(c, v)
        case "form":     token, err = mw.jwtFromForm(c, v)
        }
    }
}
2w1nd commented 2 years ago

yes, you are right, I also participated. I should read the source code carefully😂