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

Adding support for JWKS-based token validation and token attribute mapping #151

Open
wants to merge 3 commits 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
11 changes: 8 additions & 3 deletions kong/plugins/oidc/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ function handle(oidcConfig)
if oidcConfig.introspection_endpoint then
response = introspect(oidcConfig)
if response then
utils.injectUser(response)
utils.injectUser(response, oidcConfig)
end
end

if response == nil then
response = make_oidc(oidcConfig)
if response then
if (response.user) then
utils.injectUser(response.user)
utils.injectUser(response.user, oidcConfig)
end
if (response.access_token) then
utils.injectAccessToken(response.access_token)
Expand All @@ -65,7 +65,12 @@ end

function introspect(oidcConfig)
if utils.has_bearer_access_token() or oidcConfig.bearer_only == "yes" then
local res, err = require("resty.openidc").introspect(oidcConfig)
local res, err
if oidcConfig.use_jwks == "yes" then
res, err = require("resty.openidc").bearer_jwt_verify(oidcConfig)
else
res, err = require("resty.openidc").introspect(oidcConfig)
end
if err then
if oidcConfig.bearer_only == "yes" then
ngx.header["WWW-Authenticate"] = 'Bearer realm="' .. oidcConfig.realm .. '",error="' .. err .. '"'
Expand Down
4 changes: 3 additions & 1 deletion kong/plugins/oidc/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ return {
scope = { type = "string", required = true, default = "openid" },
response_type = { type = "string", required = true, default = "code" },
ssl_verify = { type = "string", required = true, default = "no" },
use_jwks = { type = "string", required = true, default = "no" },
token_endpoint_auth_method = { type = "string", required = true, default = "client_secret_post" },
session_secret = { type = "string", required = false },
recovery_page_path = { type = "string" },
logout_path = { type = "string", required = false, default = '/logout' },
redirect_after_logout_uri = { type = "string", required = false, default = '/' },
filters = { type = "string" }
filters = { type = "string" },
mappings = { type = "array", default = {}, }
}
}
22 changes: 21 additions & 1 deletion kong/plugins/oidc/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ function M.get_options(config, ngx)
scope = config.scope,
response_type = config.response_type,
ssl_verify = config.ssl_verify,
use_jwks = config.use_jwks,
token_endpoint_auth_method = config.token_endpoint_auth_method,
recovery_page_path = config.recovery_page_path,
filters = parseFilters(config.filters),
logout_path = config.logout_path,
redirect_after_logout_uri = config.redirect_after_logout_uri,
mappings = config.mappings,
}
end

Expand All @@ -76,13 +78,31 @@ function M.injectIDToken(idToken)
ngx.req.set_header("X-ID-Token", ngx.encode_base64(tokenStr))
end

function M.injectUser(user)
function M.injectUser(user, oidcConfig)
local tmp_user = user
tmp_user.id = user.sub
tmp_user.username = user.preferred_username
ngx.ctx.authenticated_credential = tmp_user
local userinfo = cjson.encode(user)
ngx.req.set_header("X-Userinfo", ngx.encode_base64(userinfo))

if oidcConfig.mappings ~= nil then
for i, value in ipairs(oidcConfig.mappings) do
local f, from, to
f = string.gmatch(value, "[^:]+")
from = f()
to = f()
if from ~= nil and to ~= nil then
if user[from] ~= nil then
ngx.req.set_header(to, user[from])
else
ngx.log(ngx.WARN, "Key '" .. from .. "' not present on token")
end
else
ngx.log(ngx.ERR, "Ignoring incorrect configuration: " .. value)
end
end
end
end

function M.has_bearer_access_token()
Expand Down
1 change: 1 addition & 0 deletions test/unit/mockable_case.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function MockableCase:setUp()
self.mocked_ngx = {
DEBUG = "debug",
ERR = "error",
WARN = "warn",
HTTP_UNAUTHORIZED = 401,
ctx = {},
header = {},
Expand Down
19 changes: 19 additions & 0 deletions test/unit/test_handler_mocking_openidc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ function TestHandler:test_bearer_only_with_bad_token()
lu.assertFalse(self:log_contains("introspect succeeded"))
end

function TestHandler:test_introspect_bearer_token_and_property_mapping()
self.module_resty.openidc.bearer_jwt_verify = function(opts)
return {foo = "bar"}, false
end
ngx.req.get_headers = function() return {Authorization = "Bearer xxx"} end

ngx.encode_base64 = function(x) return "x" end

local headers = {}
ngx.req.set_header = function(h, v)
headers[h] = v
end

self.handler:access({introspection_endpoint = "x", bearer_only = "yes", use_jwks = "yes", mappings = {'foo:X-Foo', 'incorrect', 'not:present'}})
lu.assertEquals(headers["X-Foo"], 'bar')
lu.assertTrue(self:log_contains("not present on token"))
lu.assertTrue(self:log_contains("Ignoring incorrect configuration"))
end

lu.run()