Skip to content

Commit a25f846

Browse files
authored
Merge pull request #171 from dhlin/master
add customized expiration time option for discovery and jwk cache
2 parents 6379b28 + 2931bee commit a25f846

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ lAc5Csj0o5Q+oEhPUAVBIF07m4rd0OvAVPOCQ2NJhQSL1oWASbf+fg==
269269
-- verified at all.
270270
--accept_unsupported_alg = true
271271
272+
-- the expiration time in seconds for jwk cache, default is 1 day.
273+
--jwk_expires_in = 24 * 60 * 60
274+
272275
}
273276
274277
-- call bearer_jwt_verify for OAuth 2.0 JWT validation

lib/resty/openidc.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ local function openidc_load_jwt_none_alg(enc_hdr, enc_payload)
466466
end
467467

468468
-- get the Discovery metadata from the specified URL
469-
local function openidc_discover(url, ssl_verify, timeout, proxy_opts)
469+
local function openidc_discover(url, ssl_verify, timeout, exptime, proxy_opts)
470470
ngx.log(ngx.DEBUG, "openidc_discover: URL is: "..url)
471471

472472
local json, err
@@ -489,7 +489,7 @@ local function openidc_discover(url, ssl_verify, timeout, proxy_opts)
489489
json, err = openidc_parse_json_response(res)
490490
if json then
491491
if string.sub(url, 1, string.len(json['issuer'])) == json['issuer'] then
492-
openidc_cache_set("discovery", url, cjson.encode(json), 24 * 60 * 60)
492+
openidc_cache_set("discovery", url, cjson.encode(json), exptime or 24 * 60 * 60)
493493
else
494494
err = "issuer field in Discovery data does not match URL"
495495
ngx.log(ngx.ERR, err)
@@ -512,7 +512,7 @@ end
512512
local function openidc_ensure_discovered_data(opts)
513513
local err
514514
if type(opts.discovery) == "string" then
515-
opts.discovery, err = openidc_discover(opts.discovery, opts.ssl_verify, opts.timeout, opts.proxy_opts)
515+
opts.discovery, err = openidc_discover(opts.discovery, opts.ssl_verify, opts.timeout, opts.jwk_expires_in, opts.proxy_opts)
516516
end
517517
return err
518518
end
@@ -527,7 +527,7 @@ function openidc.get_discovery_doc(opts)
527527
return opts.discovery, err
528528
end
529529

530-
local function openidc_jwks(url, force, ssl_verify, timeout, proxy_opts)
530+
local function openidc_jwks(url, force, ssl_verify, timeout, exptime, proxy_opts)
531531
ngx.log(ngx.DEBUG, "openidc_jwks: URL is: "..url.. " (force=" .. force .. ")")
532532

533533
local json, err, v
@@ -553,7 +553,7 @@ local function openidc_jwks(url, force, ssl_verify, timeout, proxy_opts)
553553
ngx.log(ngx.DEBUG, "response data: "..res.body)
554554
json, err = openidc_parse_json_response(res)
555555
if json then
556-
openidc_cache_set("jwks", url, cjson.encode(json), 24 * 60 * 60)
556+
openidc_cache_set("jwks", url, cjson.encode(json), exptime or 24 * 60 * 60)
557557
end
558558
end
559559

@@ -702,7 +702,7 @@ local function openidc_pem_from_jwk(opts, kid)
702702
local jwk, jwks
703703

704704
for force=0, 1 do
705-
jwks, err = openidc_jwks(opts.discovery.jwks_uri, force, opts.ssl_verify, opts.timeout, opts.proxy_opts)
705+
jwks, err = openidc_jwks(opts.discovery.jwks_uri, force, opts.ssl_verify, opts.timeout, opts.jwk_expires_in, opts.proxy_opts)
706706
if err then
707707
return nil, err
708708
end
@@ -728,7 +728,7 @@ local function openidc_pem_from_jwk(opts, kid)
728728
return nil, "don't know how to create RSA key/cert for " .. cjson.encode(jwt)
729729
end
730730

731-
openidc_cache_set("jwks", cache_id, pem, 24 * 60 * 60)
731+
openidc_cache_set("jwks", cache_id, pem, opts.jwk_expires_in or 24 * 60 * 60)
732732
return pem
733733
end
734734

0 commit comments

Comments
 (0)