Skip to content

Commit

Permalink
fix(jwt-auth) return 401 unauthorized instead of 403 (Kong#2433)
Browse files Browse the repository at this point in the history
On invalid claims the JWT plugin returned `403 forbidden` instead
of the `401 unauthorized`.

fixes Kong#2409
  • Loading branch information
Tieske authored Apr 21, 2017
1 parent 5616c94 commit 35467cc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
- hmac: Better handling of invalid base64-encoded signatures. Previously Kong
would return an HTTP 500 error. We now properly return HTTP 403 Forbidden.
[#2283](https://github.com/Mashape/kong/pull/2283)
- jwt: Returns `401 unauthorized` on invalid claims, instead of previous
`403 forbidden`.
[#2433](https://github.com/Mashape/kong/pull/2433)
- Admin API:
- Detect conflicts between SNI Objects in the `/snis` and `/certificates`
endpoint.
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/jwt/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ local function do_authentication(conf)
-- Verify the JWT registered claims
local ok_claims, errors = jwt:verify_registered_claims(conf.claims_to_verify)
if not ok_claims then
return false, {status = 403, message = errors}
return false, {status = 401, message = errors}
end

-- Retrieve the consumer
Expand Down
23 changes: 20 additions & 3 deletions spec/03-plugins/17-jwt/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ describe("Plugin: jwt (access)", function()
["Host"] = "jwt3.com"
}
})
local body = assert.res_status(403, res)
local body = assert.res_status(401, res)
assert.equal('{"nbf":"must be a number","exp":"must be a number"}', body)
end)
it("checks if the fields are valid", function()
it("checks if the fields are valid: `exp` claim", function()
local payload = {
iss = jwt_secret.key,
exp = os.time() - 10,
Expand All @@ -293,9 +293,26 @@ describe("Plugin: jwt (access)", function()
["Host"] = "jwt3.com"
}
})
local body = assert.res_status(403, res)
local body = assert.res_status(401, res)
assert.equal('{"exp":"token expired"}', body)
end)
it("checks if the fields are valid: `nbf` claim", function()
local payload = {
iss = jwt_secret.key,
exp = os.time() + 10,
nbf = os.time() + 5
}
local jwt = jwt_encoder.encode(payload, jwt_secret.secret)
local res = assert(proxy_client:send {
method = "GET",
path = "/request/?jwt="..jwt,
headers = {
["Host"] = "jwt3.com"
}
})
local body = assert.res_status(401, res)
assert.equal('{"nbf":"token not valid yet"}', body)
end)
end)

describe("config.anonymous", function()
Expand Down

0 comments on commit 35467cc

Please sign in to comment.