Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 94ebaaa

Browse files
author
Cristian Chiru
authored
Merge pull request #4 from Starefossen/use-jwks
Adding support for JWKS-based token validation
2 parents f1ef435 + 889cf8a commit 94ebaaa

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

kong/plugins/oidc/handler.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ end
115115

116116
function introspect(oidcConfig)
117117
if utils.has_bearer_access_token() or oidcConfig.bearer_only == "yes" then
118-
local res, err = require("resty.openidc").introspect(oidcConfig)
118+
local res, err
119+
if oidcConfig.use_jwks == "yes" then
120+
res, err = require("resty.openidc").bearer_jwt_verify(oidcConfig)
121+
else
122+
res, err = require("resty.openidc").introspect(oidcConfig)
123+
end
119124
if err then
120125
if oidcConfig.bearer_only == "yes" then
121126
ngx.header["WWW-Authenticate"] = 'Bearer realm="' .. oidcConfig.realm .. '",error="' .. err .. '"'

kong/plugins/oidc/schema.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ return {
1313
scope = { type = "string", required = true, default = "openid" },
1414
response_type = { type = "string", required = true, default = "code" },
1515
ssl_verify = { type = "string", required = true, default = "no" },
16+
use_jwks = { type = "string", required = true, default = "no" },
1617
token_endpoint_auth_method = { type = "string", required = true, default = "client_secret_post" },
1718
session_secret = { type = "string", required = false },
1819
recovery_page_path = { type = "string" },

kong/plugins/oidc/utils.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function M.get_options(config, ngx)
5858
scope = config.scope,
5959
response_type = config.response_type,
6060
ssl_verify = config.ssl_verify,
61+
use_jwks = config.use_jwks,
6162
token_endpoint_auth_method = config.token_endpoint_auth_method,
6263
recovery_page_path = config.recovery_page_path,
6364
filters = parseFilters((config.filters or "") .. "," .. (config.ignore_auth_filters or "")),
@@ -155,7 +156,7 @@ function M.setCredentials(user)
155156
end
156157

157158
function M.injectUser(user, headerName)
158-
ngx.log(ngx.DEBUG, "Injecting " .. headerName)
159+
ngx.log(ngx.DEBUG, "Injecting " .. headerName)
159160
local userinfo = cjson.encode(user)
160161
ngx.req.set_header(headerName, ngx.encode_base64(userinfo))
161162
end

test/unit/test_handler_mocking_openidc.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function TestHandler:test_authenticate_ok_with_userinfo()
3636
ngx.encode_base64 = function(x)
3737
return "eyJzdWIiOiJzdWIifQ=="
3838
end
39-
39+
4040
local headers = {}
4141
ngx.req.set_header = function(h, v)
4242
headers[h] = v
@@ -52,7 +52,7 @@ function TestHandler:test_authenticate_ok_with_no_accesstoken()
5252
self.module_resty.openidc.authenticate = function(opts)
5353
return {id_token = {sub = "sub"}}, true
5454
end
55-
55+
5656
local headers = {}
5757
ngx.req.set_header = function(h, v)
5858
headers[h] = v
@@ -67,7 +67,7 @@ function TestHandler:test_authenticate_ok_with_accesstoken()
6767
self.module_resty.openidc.authenticate = function(opts)
6868
return {id_token = { sub = "sub" } , access_token = "ACCESS_TOKEN"}, true
6969
end
70-
70+
7171
local headers = {}
7272
ngx.req.set_header = function(h, v)
7373
headers[h] = v
@@ -82,7 +82,7 @@ function TestHandler:test_authenticate_ok_with_no_idtoken()
8282
self.module_resty.openidc.authenticate = function(opts)
8383
return {}, true
8484
end
85-
85+
8686
local headers = {}
8787
ngx.req.set_header = function(h, v)
8888
headers[h] = v
@@ -101,7 +101,7 @@ function TestHandler:test_authenticate_ok_with_idtoken()
101101
ngx.encode_base64 = function(x)
102102
return "eyJzdWIiOiJzdWIifQ=="
103103
end
104-
104+
105105
local headers = {}
106106
ngx.req.set_header = function(h, v)
107107
headers[h] = v
@@ -205,6 +205,23 @@ function TestHandler:test_bearer_only_with_bad_token()
205205
lu.assertFalse(self:log_contains("introspect succeeded"))
206206
end
207207

208-
lu.run()
208+
function TestHandler:test_introspect_bearer_token_and_property_mapping()
209+
self.module_resty.openidc.bearer_jwt_verify = function(opts)
210+
return {foo = "bar"}, false
211+
end
212+
ngx.req.get_headers = function() return {Authorization = "Bearer xxx"} end
209213

214+
ngx.encode_base64 = function(x) return "x" end
210215

216+
local headers = {}
217+
ngx.req.set_header = function(h, v)
218+
headers[h] = v
219+
end
220+
221+
self.handler:access({introspection_endpoint = "x", bearer_only = "yes", use_jwks = "yes", mappings = {'foo:X-Foo', 'incorrect', 'not:present'}})
222+
lu.assertEquals(headers["X-Foo"], 'bar')
223+
lu.assertTrue(self:log_contains("not present on token"))
224+
lu.assertTrue(self:log_contains("Ignoring incorrect configuration"))
225+
end
226+
227+
lu.run()

0 commit comments

Comments
 (0)