Skip to content

Commit d387399

Browse files
committed
take some configuration options into account for cache keys
see #399 Signed-off-by: Stefan Bodewig <[email protected]>
1 parent a73ebd9 commit d387399

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

Diff for: lib/resty/openidc.lua

+49-14
Original file line numberDiff line numberDiff line change
@@ -1613,17 +1613,41 @@ local function openidc_get_bearer_access_token(opts)
16131613
return access_token, err
16141614
end
16151615

1616+
local function get_introspection_endpoint(opts)
1617+
local introspection_endpoint = opts.introspection_endpoint
1618+
if not introspection_endpoint then
1619+
local err = openidc_ensure_discovered_data(opts)
1620+
if err then
1621+
return nil, "opts.introspection_endpoint not said and " .. err
1622+
end
1623+
local endpoint = opts.discovery and opts.discovery.introspection_endpoint
1624+
if endpoint then
1625+
return endpoint
1626+
end
1627+
end
1628+
return introspection_endpoint
1629+
end
1630+
1631+
local function get_introspection_cache_prefix(opts)
1632+
return (get_introspection_endpoint(opts) or 'nil-endpoint') .. ','
1633+
.. (opts.client_id or 'no-client_id') .. ','
1634+
.. (opts.client_secret and 'secret' or 'no-client_secret') .. ':'
1635+
end
1636+
16161637
local function get_cached_introspection(opts, access_token)
16171638
local introspection_cache_ignore = opts.introspection_cache_ignore or false
16181639
if not introspection_cache_ignore then
1619-
return openidc_cache_get("introspection", access_token)
1640+
return openidc_cache_get("introspection",
1641+
get_introspection_cache_prefix(opts) .. access_token)
16201642
end
16211643
end
16221644

16231645
local function set_cached_introspection(opts, access_token, encoded_json, ttl)
16241646
local introspection_cache_ignore = opts.introspection_cache_ignore or false
16251647
if not introspection_cache_ignore then
1626-
openidc_cache_set("introspection", access_token, encoded_json, ttl)
1648+
openidc_cache_set("introspection",
1649+
get_introspection_cache_prefix(opts) .. access_token,
1650+
encoded_json, ttl)
16271651
end
16281652
end
16291653

@@ -1665,16 +1689,10 @@ function openidc.introspect(opts)
16651689
end
16661690

16671691
-- call the introspection endpoint
1668-
local introspection_endpoint = opts.introspection_endpoint
1669-
if not introspection_endpoint then
1670-
err = openidc_ensure_discovered_data(opts)
1671-
if err then
1672-
return nil, "opts.introspection_endpoint not said and " .. err
1673-
end
1674-
local endpoint = opts.discovery and opts.discovery.introspection_endpoint
1675-
if endpoint then
1676-
introspection_endpoint = endpoint
1677-
end
1692+
local introspection_endpoint
1693+
introspection_endpoint, err = get_introspection_endpoint(opts)
1694+
if err then
1695+
return nil, err
16781696
end
16791697
json, err = openidc.call_token_endpoint(opts, introspection_endpoint, body, opts.introspection_endpoint_auth_method, "introspection")
16801698

@@ -1711,17 +1729,34 @@ function openidc.introspect(opts)
17111729

17121730
end
17131731

1732+
local function get_jwt_verification_cache_prefix(opts)
1733+
local signing_alg_values_expected = (opts.accept_none_alg and 'none' or 'no-none')
1734+
local expected_algs = opts.token_signing_alg_values_expected or {}
1735+
if type(expected_algs) == 'string' then
1736+
expected_algs = { expected_algs }
1737+
end
1738+
for _, alg in ipairs(expected_algs) do
1739+
signing_alg_values_expected = signing_alg_values_expected .. ',' .. alg
1740+
end
1741+
return (opts.public_key or 'no-pubkey') .. ','
1742+
.. (opts.symmetric_key or 'no-symkey') .. ','
1743+
.. signing_alg_values_expected .. ':'
1744+
end
1745+
17141746
local function get_cached_jwt_verification(opts, access_token)
17151747
local jwt_verification_cache_ignore = opts.jwt_verification_cache_ignore or false
17161748
if not jwt_verification_cache_ignore then
1717-
return openidc_cache_get("jwt_verification", access_token)
1749+
return openidc_cache_get("jwt_verification",
1750+
get_jwt_verification_cache_prefix(opts) .. access_token)
17181751
end
17191752
end
17201753

17211754
local function set_cached_jwt_verification(opts, access_token, encoded_json, ttl)
17221755
local jwt_verification_cache_ignore = opts.jwt_verification_cache_ignore or false
17231756
if not jwt_verification_cache_ignore then
1724-
openidc_cache_set("jwt_verification", access_token, encoded_json, ttl)
1757+
openidc_cache_set("jwt_verification",
1758+
get_jwt_verification_cache_prefix(opts) .. access_token,
1759+
encoded_json, ttl)
17251760
end
17261761
end
17271762

0 commit comments

Comments
 (0)