-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(plugins) anonymous authentication in auth plugins #1666
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ local singletons = require "kong.singletons" | |
local constants = require "kong.constants" | ||
local responses = require "kong.tools.responses" | ||
|
||
local ngx_set_header = ngx.req.set_header | ||
local ngx_get_headers = ngx.req.get_headers | ||
|
||
local realm = 'Basic realm="'.._KONG._NAME..'"' | ||
|
||
local _M = {} | ||
|
@@ -81,11 +84,12 @@ local function load_credential_from_db(username) | |
return credential | ||
end | ||
|
||
function _M.execute(conf) | ||
local function do_authentication(conf) | ||
-- If both headers are missing, return 401 | ||
if not (ngx.req.get_headers()["authorization"] or ngx.req.get_headers()["proxy-authorization"]) then | ||
local headers = ngx_get_headers() | ||
if not (headers["authorization"] or headers["proxy-authorization"]) then | ||
ngx.header["WWW-Authenticate"] = realm | ||
return responses.send_HTTP_UNAUTHORIZED() | ||
return false, {status = 401} | ||
end | ||
|
||
local credential | ||
|
@@ -101,7 +105,7 @@ function _M.execute(conf) | |
end | ||
|
||
if not credential or not validate_credentials(credential, given_password) then | ||
return responses.send_HTTP_FORBIDDEN("Invalid authentication credentials") | ||
return false, {status = 403, message = "Invalid authentication credentials"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just return same for the other plugins |
||
end | ||
|
||
-- Retrieve consumer | ||
|
@@ -113,12 +117,26 @@ function _M.execute(conf) | |
return result | ||
end) | ||
|
||
ngx.req.set_header(constants.HEADERS.CONSUMER_ID, consumer.id) | ||
ngx.req.set_header(constants.HEADERS.CONSUMER_CUSTOM_ID, consumer.custom_id) | ||
ngx.req.set_header(constants.HEADERS.CONSUMER_USERNAME, consumer.username) | ||
ngx.req.set_header(constants.HEADERS.CREDENTIAL_USERNAME, credential.username) | ||
ngx_set_header(constants.HEADERS.ANONYMOUS, nil) -- In case of auth plugins concatenation | ||
ngx_set_header(constants.HEADERS.CONSUMER_ID, consumer.id) | ||
ngx_set_header(constants.HEADERS.CONSUMER_CUSTOM_ID, consumer.custom_id) | ||
ngx_set_header(constants.HEADERS.CONSUMER_USERNAME, consumer.username) | ||
ngx_set_header(constants.HEADERS.CREDENTIAL_USERNAME, credential.username) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we cache those constants locally? the above are 15 table lookups same for the other plugins |
||
ngx.ctx.authenticated_credential = credential | ||
ngx.ctx.authenticated_consumer = consumer | ||
|
||
return true | ||
end | ||
|
||
function _M.execute(conf) | ||
local ok, err = do_authentication(conf) | ||
if not ok then | ||
if conf.anonymous then | ||
ngx_set_header(constants.HEADERS.ANONYMOUS, true) | ||
else | ||
return responses.send(err.status, err.message, err.headers) | ||
end | ||
end | ||
end | ||
|
||
return _M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
return { | ||
no_consumer = true, | ||
fields = { | ||
anonymous = {type = "boolean", default = false}, | ||
hide_credentials = {type = "boolean", default = false} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use trailing comma for table constructors, to keep the diff clean
same for the other plugins