Skip to content

Commit 3962d5d

Browse files
committed
Only use JWT auth methods if the necessary key has been specified
closes #238 Signed-off-by: Stefan Bodewig <[email protected]>
1 parent 8f35802 commit 3962d5d

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
12/17/2018
2+
- don't select one of the jwt token auth methods if the required key
3+
material is not present; see #238
4+
15
11/13/2018
26
- fixed a bad error return value in certain setups of
37
bearer_jwt_verify; see #234; thanks @JoshTheGoldfish

lib/resty/openidc.lua

+19-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,21 @@ local DEBUG = ngx.DEBUG
6666
local ERROR = ngx.ERR
6767
local WARN = ngx.WARN
6868

69+
local function token_auth_method_precondition(method, required_field)
70+
return function(opts)
71+
if not opts[required_field] then
72+
ngx.log(ngx.DEBUG, "Can't use " .. method .. " without opts." .. required_field)
73+
return false
74+
end
75+
return true
76+
end
77+
end
78+
6979
local supported_token_auth_methods = {
7080
client_secret_basic = true,
7181
client_secret_post = true,
72-
private_key_jwt = true,
73-
client_secret_jwt = true
82+
private_key_jwt = token_auth_method_precondition('private_key_jwt', 'client_rsa_private_key'),
83+
client_secret_jwt = token_auth_method_precondition('client_secret_jwt', 'client_secret')
7484
}
7585

7686
local openidc = {
@@ -582,10 +592,15 @@ local function openidc_ensure_discovered_data(opts)
582592
return err
583593
end
584594

595+
local function can_use_token_auth_method(method, opts)
596+
local supported = supported_token_auth_methods[method]
597+
return supported and (type(supported) ~= 'function' or supported(opts))
598+
end
599+
585600
-- get the token endpoint authentication method
586601
local function openidc_get_token_auth_method(opts)
587602

588-
if opts.token_endpoint_auth_method ~= nil and not supported_token_auth_methods[opts.token_endpoint_auth_method] then
603+
if opts.token_endpoint_auth_method ~= nil and not can_use_token_auth_method(opts.token_endpoint_auth_method, opts) then
589604
log(ERROR, "configured value for token_endpoint_auth_method (" .. opts.token_endpoint_auth_method .. ") is not supported, ignoring it")
590605
opts.token_endpoint_auth_method = nil
591606
end
@@ -609,7 +624,7 @@ local function openidc_get_token_auth_method(opts)
609624
else
610625
for index, value in ipairs(opts.discovery.token_endpoint_auth_methods_supported) do
611626
log(DEBUG, index .. " => " .. value)
612-
if supported_token_auth_methods[value] then
627+
if can_use_token_auth_method(value, opts) then
613628
result = value
614629
log(DEBUG, "no configuration setting for option so select the first supported method specified by the OP: " .. result)
615630
break

tests/spec/token_request_spec.lua

+34
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ describe("when 'private_key_jwt' auth method is configured", function()
114114
end)
115115
end)
116116

117+
describe("when 'private_key_jwt' auth method is configured but no key specified", function()
118+
test_support.start_server({
119+
oidc_opts = {
120+
discovery = {
121+
token_endpoint_auth_methods_supported = { "client_secret_basic", "client_secret_post", "private_key_jwt" },
122+
},
123+
token_endpoint_auth_method = "private_key_jwt",
124+
}
125+
})
126+
teardown(test_support.stop_server)
127+
test_support.login()
128+
it("then it is not used", function()
129+
assert.error_log_contains("token authorization header: Basic")
130+
end)
131+
end)
132+
117133
describe("if token endpoint is not resolvable", function()
118134
test_support.start_server({
119135
oidc_opts = {
@@ -312,3 +328,21 @@ describe("when the token endpoint is invoked using client_secret_jwt", function(
312328
end)
313329
end)
314330
end)
331+
332+
describe("when 'client_secret_jwt' auth method is configured but no key specified", function()
333+
test_support.start_server({
334+
oidc_opts = {
335+
discovery = {
336+
token_endpoint_auth_methods_supported = { "client_secret_basic", "client_secret_post", "client_secret_jwt" },
337+
},
338+
token_endpoint_auth_method = "client_secret_jwt",
339+
},
340+
remove_oidc_config_keys = { "client_secret" }
341+
})
342+
teardown(test_support.stop_server)
343+
test_support.login()
344+
it("then it is not used", function()
345+
assert.error_log_contains("token authorization header: Basic")
346+
end)
347+
end)
348+

0 commit comments

Comments
 (0)