Skip to content

Commit d1ce86e

Browse files
committed
revert 'Return HTTP status 400 if missing JWT' (#13) back to returning 401
1 parent 830d94d commit d1ce86e

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
@@ -147,8 +147,7 @@ func (e *TokenError) Unwrap() error { return e.Err }
147147
// JWT returns a JSON Web Token (JWT) auth middleware.
148148
//
149149
// For valid token, it sets the user in context and calls next handler.
150-
// For invalid token, it returns "401 - Unauthorized" error.
151-
// For missing token, it returns "400 - Bad Request" error.
150+
// For invalid or missing token, middleware returns "401 - Unauthorized" error.
152151
//
153152
// See: https://jwt.io/introduction
154153
func JWT(signingKey interface{}) echo.MiddlewareFunc {
@@ -158,8 +157,7 @@ func JWT(signingKey interface{}) echo.MiddlewareFunc {
158157
// WithConfig returns a JSON Web Token (JWT) auth middleware or panics if configuration is invalid.
159158
//
160159
// For valid token, it sets the user in context and calls next handler.
161-
// For invalid token, it returns "401 - Unauthorized" error.
162-
// For missing token, it returns "400 - Bad Request" error.
160+
// For invalid or missing token, middleware returns "401 - Unauthorized" error.
163161
//
164162
// See: https://jwt.io/introduction
165163
func WithConfig(config Config) echo.MiddlewareFunc {
@@ -256,7 +254,7 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
256254
}
257255

258256
if lastTokenErr == nil {
259-
return echo.NewHTTPError(http.StatusBadRequest, "missing or malformed jwt").SetInternal(err)
257+
return echo.NewHTTPError(http.StatusUnauthorized, "missing or malformed jwt").SetInternal(err)
260258
}
261259

262260
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)