From 119241c3cd63e441d7179a52a88cccf90d159f24 Mon Sep 17 00:00:00 2001 From: Starballoon <30925057+Starballoon@users.noreply.github.com> Date: Sun, 5 Jun 2022 21:58:31 +0800 Subject: [PATCH] Add an option to parse token from POST form data. (#293) --- auth_jwt.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/auth_jwt.go b/auth_jwt.go index 89ec00d..eec9aae 100644 --- a/auth_jwt.go +++ b/auth_jwt.go @@ -711,6 +711,16 @@ func (mw *GinJWTMiddleware) jwtFromParam(c *gin.Context, key string) (string, er return token, nil } +func (mw *GinJWTMiddleware) jwtFromForm(c *gin.Context, key string) (string, error) { + token := c.PostForm(key) + + if token == "" { + return "", ErrEmptyParamToken + } + + return token, nil +} + // ParseToken parse jwt token from gin context func (mw *GinJWTMiddleware) ParseToken(c *gin.Context) (*jwt.Token, error) { var token string @@ -733,6 +743,8 @@ func (mw *GinJWTMiddleware) ParseToken(c *gin.Context) (*jwt.Token, error) { token, err = mw.jwtFromCookie(c, v) case "param": token, err = mw.jwtFromParam(c, v) + case "form": + token, err = mw.jwtFromForm(c, v) } }