Skip to content

Commit

Permalink
Add WWW-Authenticate header for reponses w/ status 401 Unauthorized.
Browse files Browse the repository at this point in the history
  • Loading branch information
ybv committed Oct 13, 2015
1 parent 1120a7c commit 3f67edc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions kong/plugins/basic-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function _M.execute(conf)
-- If both headers are missing, return 401
if not (ngx.req.get_headers()[AUTHORIZATION] or ngx.req.get_headers()[PROXY_AUTHORIZATION]) then
ngx.ctx.stop_phases = true
ngx.header["WWW-Authenticate"] = "Basic realm=\""..constants.NAME.."\""
return responses.send_HTTP_UNAUTHORIZED()
end

Expand Down
1 change: 1 addition & 0 deletions kong/plugins/key-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function _M.execute(conf)
-- No key found in the request's headers or parameters
if not key_found then
ngx.ctx.stop_phases = true
ngx.header["WWW-Authenticate"] = "Key realm=\""..constants.NAME.."\""
return responses.send_HTTP_UNAUTHORIZED("No API Key found in headers, body or querystring")
end

Expand Down
11 changes: 7 additions & 4 deletions spec/plugins/basic-auth/access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"
local constants = require "kong.constants"
local cjson = require "cjson"

local PROXY_URL = spec_helper.PROXY_URL
Expand Down Expand Up @@ -32,10 +33,11 @@ describe("Authentication Plugin", function()

describe("Basic Authentication", function()

it("should return invalid credentials when the credential is missing", function()
local response, status = http_client.get(PROXY_URL.."/get", {}, {host = "basicauth.com"})
it("should return invalid credentials and www-authenticate header when the credential is missing", function()
local response, status, headers = http_client.get(PROXY_URL.."/get", {}, {host = "basicauth.com"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Basic realm=\""..constants.NAME.."\"")
assert.equal("Unauthorized", body.message)
end)

Expand Down Expand Up @@ -67,10 +69,11 @@ describe("Authentication Plugin", function()
assert.equal("Invalid authentication credentials", body.message)
end)

it("should reply 401 when authorization is missing", function()
local response, status = http_client.get(PROXY_URL.."/get", {}, {host = "basicauth.com", authorization123 = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
it("should reply 401 and www-authenticate header when authorization is missing", function()
local response, status, headers = http_client.get(PROXY_URL.."/get", {}, {host = "basicauth.com", authorization123 = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Basic realm=\""..constants.NAME.."\"")
assert.equal("Unauthorized", body.message)
end)

Expand Down
31 changes: 19 additions & 12 deletions spec/plugins/key-auth/access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"
local constants = require "kong.constants"
local cjson = require "cjson"

local STUB_GET_URL = spec_helper.STUB_GET_URL
Expand Down Expand Up @@ -35,10 +36,11 @@ describe("Authentication Plugin", function()

describe("Query Authentication", function()

it("should return invalid credentials when the credential is missing", function()
local response, status = http_client.get(STUB_GET_URL, {}, {host = "keyauth1.com"})
it("should return invalid credentials and www-authenticate header when the credential is missing", function()
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "keyauth1.com"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Key realm=\""..constants.NAME.."\"")
assert.equal("No API Key found in headers, body or querystring", body.message)
end)

Expand All @@ -49,24 +51,27 @@ describe("Authentication Plugin", function()
assert.equal("Invalid authentication credentials", body.message)
end)

it("should reply 401 when the credential parameter is missing", function()
local response, status = http_client.get(STUB_GET_URL, {apikey123 = "apikey123"}, {host = "keyauth1.com"})
it("should reply with 401 and www-authenticate header when the credential parameter is missing", function()
local response, status, headers = http_client.get(STUB_GET_URL, {apikey123 = "apikey123"}, {host = "keyauth1.com"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Key realm=\""..constants.NAME.."\"")
assert.equal("No API Key found in headers, body or querystring", body.message)
end)

it("should reply 401 when the credential parameter name is wrong in GET", function()
local response, status = http_client.get(STUB_GET_URL, {apikey123 = "apikey123"}, {host = "keyauth1.com"})
it("should reply 401 and www-authenticate header when the credential parameter name is wrong in GET", function()
local response, status, headers = http_client.get(STUB_GET_URL, {apikey123 = "apikey123"}, {host = "keyauth1.com"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Key realm=\""..constants.NAME.."\"")
assert.equal("No API Key found in headers, body or querystring", body.message)
end)

it("should reply 401 when the credential parameter name is wrong in POST", function()
local response, status = http_client.post(STUB_POST_URL, {apikey123 = "apikey123"}, {host = "keyauth1.com"})
it("should reply 401 and www-authenticate header when the credential parameter name is wrong in POST", function()
local response, status, headers = http_client.post(STUB_POST_URL, {apikey123 = "apikey123"}, {host = "keyauth1.com"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Key realm=\""..constants.NAME.."\"")
assert.equal("No API Key found in headers, body or querystring", body.message)
end)

Expand All @@ -77,17 +82,19 @@ describe("Authentication Plugin", function()
assert.equal("apikey123", parsed_response.queryString.apikey)
end)

it("should reply 401 when the credential parameter name is wrong in GET header", function()
local response, status = http_client.get(STUB_GET_URL, {}, {host = "keyauth1.com", apikey123 = "apikey123"})
it("should reply 401 and www-authenticate header when the credential parameter name is wrong in GET header", function()
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "keyauth1.com", apikey123 = "apikey123"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Key realm=\""..constants.NAME.."\"")
assert.equal("No API Key found in headers, body or querystring", body.message)
end)

it("should reply 401 when the credential parameter name is wrong in POST header", function()
local response, status = http_client.post(STUB_POST_URL, {}, {host = "keyauth1.com", apikey123 = "apikey123"})
it("should reply 401 and www-authenticate header when the credential parameter name is wrong in POST header", function()
local response, status, headers = http_client.post(STUB_POST_URL, {}, {host = "keyauth1.com", apikey123 = "apikey123"})
local body = cjson.decode(response)
assert.equal(401, status)
assert.equal(headers["www-authenticate"], "Key realm=\""..constants.NAME.."\"")
assert.equal("No API Key found in headers, body or querystring", body.message)
end)

Expand Down

0 comments on commit 3f67edc

Please sign in to comment.