Skip to content

Commit

Permalink
Return HTTP status 400 if missing JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong authored and aldas committed Jul 28, 2023
1 parent 52fbbba commit 2fe4a09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
return tmpErr
}

message := "invalid or expired jwt"
if lastTokenErr == nil {
message = "missing or malformed jwt"
return echo.NewHTTPError(http.StatusBadRequest, "missing or malformed jwt").SetInternal(err)
}
return echo.NewHTTPError(http.StatusUnauthorized, message).SetInternal(err)

return echo.NewHTTPError(http.StatusUnauthorized, "invalid or expired jwt").SetInternal(err)
}
}, nil
}
Expand Down
12 changes: 6 additions & 6 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ func TestJWT_combinations(t *testing.T) {
config: Config{
SigningKey: validKey,
},
expectError: "code=401, message=missing or malformed jwt, internal=invalid value in request header",
expectError: "code=400, message=missing or malformed jwt, internal=invalid value in request header",
},
{
name: "Empty header auth field",
config: Config{
SigningKey: validKey,
},
expectError: "code=401, message=missing or malformed jwt, internal=invalid value in request header",
expectError: "code=400, message=missing or malformed jwt, internal=invalid value in request header",
},
{
name: "Valid query method",
Expand All @@ -180,7 +180,7 @@ func TestJWT_combinations(t *testing.T) {
TokenLookup: "query:jwt",
},
reqURL: "/?a=b&jwtxyz=" + token,
expectError: "code=401, message=missing or malformed jwt, internal=missing value in the query string",
expectError: "code=400, message=missing or malformed jwt, internal=missing value in the query string",
},
{
name: "Invalid query param value",
Expand All @@ -198,7 +198,7 @@ func TestJWT_combinations(t *testing.T) {
TokenLookup: "query:jwt",
},
reqURL: "/?a=b",
expectError: "code=401, message=missing or malformed jwt, internal=missing value in the query string",
expectError: "code=400, message=missing or malformed jwt, internal=missing value in the query string",
},
{
config: Config{
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestJWT_combinations(t *testing.T) {
SigningKey: validKey,
TokenLookup: "cookie:jwt",
},
expectError: "code=401, message=missing or malformed jwt, internal=missing value in cookies",
expectError: "code=400, message=missing or malformed jwt, internal=missing value in cookies",
},
{
name: "Valid form method",
Expand All @@ -264,7 +264,7 @@ func TestJWT_combinations(t *testing.T) {
SigningKey: validKey,
TokenLookup: "form:jwt",
},
expectError: "code=401, message=missing or malformed jwt, internal=missing value in the form",
expectError: "code=400, message=missing or malformed jwt, internal=missing value in the form",
},
}

Expand Down

0 comments on commit 2fe4a09

Please sign in to comment.