Skip to content
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(jwt) : add feature to jwt plugin:allow set headers from claim #13368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions kong/plugins/jwt/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ local pairs = pairs
local tostring = tostring
local re_gmatch = ngx.re.gmatch

local consts = {
JWT_CLAIM_HEADER_PREFIX = "X-Jwt-Claim"
}
Comment on lines +15 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't generally create extra tables for local constants. Please change this to just

Suggested change
local consts = {
JWT_CLAIM_HEADER_PREFIX = "X-Jwt-Claim"
}
JWT_CLAIM_HEADER_PREFIX = "X-Jwt-Claim"


local JwtHandler = {
VERSION = kong_meta.version,
Expand Down Expand Up @@ -150,6 +153,15 @@ local function unauthorized(message, www_auth_content, errors)
return { status = 401, message = message, headers = { ["WWW-Authenticate"] = www_auth_content }, errors = errors }
end

-- set header keys from claims
local function set_headers(conf, claims)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rename the function to set_headers_from_claims, the comment is no longer needed.

local set_header = kong.service.request.set_header
for _, v in ipairs(conf.headers_to_set) do
if claims[v] then
set_header(consts.JWT_CLAIM_HEADER_PREFIX.."-"..v, claims[v])
end
end
end

local function do_authentication(conf)
local token, err = retrieve_tokens(conf)
Expand Down Expand Up @@ -254,6 +266,8 @@ local function do_authentication(conf)

set_consumer(consumer, jwt_secret, token)

set_headers(conf, claims)

return true
end

Expand Down
5 changes: 5 additions & 0 deletions kong/plugins/jwt/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ return {
type = "string",
one_of = { "exp", "nbf" },
}, }, },
{ headers_to_set = {
type = "set",
elements = {
type = "string"
}, }, },
{ anonymous = { description = "An optional string (consumer UUID or username) value to use as an “anonymous” consumer if authentication fails.", type = "string" }, },
{ run_on_preflight = { description = "A boolean value that indicates whether the plugin should run (and try to authenticate) on OPTIONS preflight requests. If set to false, then OPTIONS requests will always be allowed.", type = "boolean", required = true, default = true }, },
{ maximum_expiration = {
Expand Down
Loading