Skip to content

Commit 10f5dc5

Browse files
committed
revert 'Return HTTP status 400 if missing JWT' (labstack#13) back to returning 401
1 parent 580269f commit 10f5dc5

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

jwt.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ func (e *TokenError) Unwrap() error { return e.Err }
146146
// JWT returns a JSON Web Token (JWT) auth middleware.
147147
//
148148
// For valid token, it sets the user in context and calls next handler.
149-
// For invalid token, it returns "401 - Unauthorized" error.
150-
// For missing token, it returns "400 - Bad Request" error.
149+
// For invalid or missing token, middleware returns "401 - Unauthorized" error.
151150
//
152151
// See: https://jwt.io/introduction
153152
func JWT(signingKey interface{}) echo.MiddlewareFunc {
@@ -157,8 +156,7 @@ func JWT(signingKey interface{}) echo.MiddlewareFunc {
157156
// WithConfig returns a JSON Web Token (JWT) auth middleware or panics if configuration is invalid.
158157
//
159158
// For valid token, it sets the user in context and calls next handler.
160-
// For invalid token, it returns "401 - Unauthorized" error.
161-
// For missing token, it returns "400 - Bad Request" error.
159+
// For invalid or missing token, middleware returns "401 - Unauthorized" error.
162160
//
163161
// See: https://jwt.io/introduction
164162
func WithConfig(config Config) echo.MiddlewareFunc {
@@ -255,7 +253,7 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
255253
}
256254

257255
if lastTokenErr == nil {
258-
return echo.NewHTTPError(http.StatusBadRequest, "missing or malformed jwt").SetInternal(err)
256+
return echo.NewHTTPError(http.StatusUnauthorized, "missing or malformed jwt").SetInternal(err)
259257
}
260258

261259
return echo.NewHTTPError(http.StatusUnauthorized, "invalid or expired jwt").SetInternal(err)

jwt_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ func TestJWT_combinations(t *testing.T) {
156156
config: Config{
157157
SigningKey: validKey,
158158
},
159-
expectError: "code=400, message=missing or malformed jwt, internal=invalid value in request header",
159+
expectError: "code=401, message=missing or malformed jwt, internal=invalid value in request header",
160160
},
161161
{
162162
name: "Empty header auth field",
163163
config: Config{
164164
SigningKey: validKey,
165165
},
166-
expectError: "code=400, message=missing or malformed jwt, internal=invalid value in request header",
166+
expectError: "code=401, message=missing or malformed jwt, internal=invalid value in request header",
167167
},
168168
{
169169
name: "Valid query method",
@@ -180,7 +180,7 @@ func TestJWT_combinations(t *testing.T) {
180180
TokenLookup: "query:jwt",
181181
},
182182
reqURL: "/?a=b&jwtxyz=" + token,
183-
expectError: "code=400, message=missing or malformed jwt, internal=missing value in the query string",
183+
expectError: "code=401, message=missing or malformed jwt, internal=missing value in the query string",
184184
},
185185
{
186186
name: "Invalid query param value",
@@ -198,7 +198,7 @@ func TestJWT_combinations(t *testing.T) {
198198
TokenLookup: "query:jwt",
199199
},
200200
reqURL: "/?a=b",
201-
expectError: "code=400, message=missing or malformed jwt, internal=missing value in the query string",
201+
expectError: "code=401, message=missing or malformed jwt, internal=missing value in the query string",
202202
},
203203
{
204204
config: Config{
@@ -239,7 +239,7 @@ func TestJWT_combinations(t *testing.T) {
239239
SigningKey: validKey,
240240
TokenLookup: "cookie:jwt",
241241
},
242-
expectError: "code=400, message=missing or malformed jwt, internal=missing value in cookies",
242+
expectError: "code=401, message=missing or malformed jwt, internal=missing value in cookies",
243243
},
244244
{
245245
name: "Valid form method",
@@ -264,7 +264,7 @@ func TestJWT_combinations(t *testing.T) {
264264
SigningKey: validKey,
265265
TokenLookup: "form:jwt",
266266
},
267-
expectError: "code=400, message=missing or malformed jwt, internal=missing value in the form",
267+
expectError: "code=401, message=missing or malformed jwt, internal=missing value in the form",
268268
},
269269
}
270270

0 commit comments

Comments
 (0)