Skip to content

Commit

Permalink
fix(basic): add missing www-authenticate headers
Browse files Browse the repository at this point in the history
When server returns 401 Unauthorized response it should
return WWW-Authenticate header as well with proper challenge.
Not all basic auth 401 responses had this header.

Fix: #7772
KAG-321
  • Loading branch information
nowNick committed Oct 19, 2023
1 parent 30d90f3 commit e451320
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/basic_www_authenticate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Add missing WWW-Authenticate headers to 401 response in basic auth plugin.
type: bugfix
scope: Plugin
8 changes: 7 additions & 1 deletion kong/plugins/basic-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ end


local function fail_authentication()
return false, { status = 401, message = "Invalid authentication credentials" }
return false, {
status = 401,
message = "Invalid authentication credentials",
headers = {
["WWW-Authenticate"] = realm
}
}
end


Expand Down
8 changes: 8 additions & 0 deletions spec/03-plugins/10-basic-auth/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Unauthorized", json.message)
assert.equal('Basic realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("returns WWW-Authenticate header on missing credentials", function()
Expand Down Expand Up @@ -176,6 +177,7 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.equal('Basic realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("returns 401 Unauthorized on invalid credentials in Proxy-Authorization", function()
Expand All @@ -191,6 +193,7 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.equal('Basic realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("returns 401 Unauthorized on password only", function()
Expand All @@ -206,6 +209,7 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.equal('Basic realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("returns 401 Unauthorized on username only", function()
Expand All @@ -221,6 +225,7 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.equal('Basic realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("rejects gRPC call without credentials", function()
Expand Down Expand Up @@ -296,6 +301,7 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.not_nil(json)
assert.matches("Invalid authentication credentials", json.message)
assert.equal('Basic realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("authenticates valid credentials in Proxy-Authorization", function()
Expand Down Expand Up @@ -564,6 +570,7 @@ for _, strategy in helpers.each_strategy() do
}
})
assert.response(res).has.status(401)
assert.equal('Key realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

it("fails 401, with no credential provided", function()
Expand All @@ -575,6 +582,7 @@ for _, strategy in helpers.each_strategy() do
}
})
assert.response(res).has.status(401)
assert.equal('Key realm="' .. meta._NAME .. '"', res.headers["WWW-Authenticate"])
end)

end)
Expand Down

0 comments on commit e451320

Please sign in to comment.