Skip to content

Commit 2152e4e

Browse files
committed
Support form fields in jwt middleware
1 parent 502cce2 commit 2152e4e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

middleware/jwt.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type (
5757
// - "query:<name>"
5858
// - "param:<name>"
5959
// - "cookie:<name>"
60+
// - "form:<name>"
6061
TokenLookup string
6162

6263
// AuthScheme to be used in the Authorization header.
@@ -167,6 +168,8 @@ func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
167168
extractor = jwtFromParam(parts[1])
168169
case "cookie":
169170
extractor = jwtFromCookie(parts[1])
171+
case "form":
172+
extractor = jwtFromForm(parts[1])
170173
}
171174

172175
return func(next echo.HandlerFunc) echo.HandlerFunc {
@@ -266,3 +269,14 @@ func jwtFromCookie(name string) jwtExtractor {
266269
return cookie.Value, nil
267270
}
268271
}
272+
273+
// jwtFromForm returns a `jwtExtractor` that extracts token from the form field.
274+
func jwtFromForm(name string) jwtExtractor {
275+
return func(c echo.Context) (string, error) {
276+
field := c.FormValue(name)
277+
if field == "" {
278+
return "", ErrJWTMissing
279+
}
280+
return field, nil
281+
}
282+
}

middleware/jwt_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package middleware
33
import (
44
"net/http"
55
"net/http/httptest"
6+
"net/url"
7+
"strings"
68
"testing"
79

810
"github.com/dgrijalva/jwt-go"
@@ -75,6 +77,7 @@ func TestJWT(t *testing.T) {
7577
reqURL string // "/" if empty
7678
hdrAuth string
7779
hdrCookie string // test.Request doesn't provide SetCookie(); use name=val
80+
formValues map[string]string
7881
info string
7982
}{
8083
{
@@ -192,12 +195,48 @@ func TestJWT(t *testing.T) {
192195
expErrCode: http.StatusBadRequest,
193196
info: "Empty cookie",
194197
},
198+
{
199+
config: JWTConfig{
200+
SigningKey: validKey,
201+
TokenLookup: "form:jwt",
202+
},
203+
formValues: map[string]string{"jwt": token},
204+
info: "Valid form method",
205+
},
206+
{
207+
config: JWTConfig{
208+
SigningKey: validKey,
209+
TokenLookup: "form:jwt",
210+
},
211+
expErrCode: http.StatusUnauthorized,
212+
formValues: map[string]string{"jwt": "invalid"},
213+
info: "Invalid token with form method",
214+
},
215+
{
216+
config: JWTConfig{
217+
SigningKey: validKey,
218+
TokenLookup: "form:jwt",
219+
},
220+
expErrCode: http.StatusBadRequest,
221+
info: "Empty form field",
222+
},
195223
} {
196224
if tc.reqURL == "" {
197225
tc.reqURL = "/"
198226
}
199227

200-
req := httptest.NewRequest(http.MethodGet, tc.reqURL, nil)
228+
var req *http.Request
229+
if len(tc.formValues) > 0 {
230+
form := url.Values{}
231+
for k, v := range tc.formValues {
232+
form.Set(k, v)
233+
}
234+
req = httptest.NewRequest(http.MethodPost, tc.reqURL, strings.NewReader(form.Encode()))
235+
req.Header.Set(echo.HeaderContentType, "application/x-www-form-urlencoded")
236+
req.ParseForm()
237+
} else {
238+
req = httptest.NewRequest(http.MethodGet, tc.reqURL, nil)
239+
}
201240
res := httptest.NewRecorder()
202241
req.Header.Set(echo.HeaderAuthorization, tc.hdrAuth)
203242
req.Header.Set(echo.HeaderCookie, tc.hdrCookie)

0 commit comments

Comments
 (0)