Skip to content

Commit 7bb2413

Browse files
committed
Ensure discovery has been attempted when calling userinfo endpoint
Signed-off-by: Stefan Bodewig <stefan.bodewig@innoq.com>
1 parent 7d3cbe5 commit 7bb2413

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

ChangeLog

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
04/08/2020
2+
- openidc.call_userinfo_endpoint will now invoke the discovery
3+
endpoint if this has not been done before in order to detect the URI
4+
of the userinfo endpoint
5+
16
02/06/2020
27
- ability to disable keepalive from lua-resty-http
38
By disabling keepalive we disable the native connection pool,
4-
avoiding errors when dealing with invalid connections. This is
9+
avoiding errors when dealing with invalid connections. This is
510
specially useful when proxying ajax requests.
611

712
02/05/2020

lib/resty/openidc.lua

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -509,39 +509,6 @@ function openidc.call_token_endpoint(opts, endpoint, body, auth, endpoint_name,
509509
return openidc_parse_json_response(res, ignore_body_on_success)
510510
end
511511

512-
-- make a call to the userinfo endpoint
513-
function openidc.call_userinfo_endpoint(opts, access_token)
514-
if not opts.discovery.userinfo_endpoint then
515-
log(DEBUG, "no userinfo endpoint supplied")
516-
return nil, nil
517-
end
518-
519-
local headers = {
520-
["Authorization"] = "Bearer " .. access_token,
521-
}
522-
523-
log(DEBUG, "authorization header '" .. headers.Authorization .. "'")
524-
525-
local httpc = http.new()
526-
openidc_configure_timeouts(httpc, opts.timeout)
527-
openidc_configure_proxy(httpc, opts.proxy_opts)
528-
local res, err = httpc:request_uri(opts.discovery.userinfo_endpoint,
529-
decorate_request(opts.http_request_decorator, {
530-
headers = headers,
531-
ssl_verify = (opts.ssl_verify ~= "no"),
532-
keepalive = (opts.keepalive ~= "no")
533-
}))
534-
if not res then
535-
err = "accessing (" .. opts.discovery.userinfo_endpoint .. ") failed: " .. err
536-
return nil, err
537-
end
538-
539-
log(DEBUG, "userinfo response: ", res.body)
540-
541-
-- parse the response from the user info endpoint
542-
return openidc_parse_json_response(res)
543-
end
544-
545512
-- computes access_token expires_in value (in seconds)
546513
local function openidc_access_token_expires_in(opts, expires_in)
547514
return (expires_in or opts.access_token_expires_in or 3600) - 1 - (opts.access_token_expires_leeway or 0)
@@ -614,6 +581,40 @@ local function openidc_ensure_discovered_data(opts)
614581
return err
615582
end
616583

584+
-- make a call to the userinfo endpoint
585+
function openidc.call_userinfo_endpoint(opts, access_token)
586+
openidc_ensure_discovered_data(opts)
587+
if not opts.discovery.userinfo_endpoint then
588+
log(DEBUG, "no userinfo endpoint supplied")
589+
return nil, nil
590+
end
591+
592+
local headers = {
593+
["Authorization"] = "Bearer " .. access_token,
594+
}
595+
596+
log(DEBUG, "authorization header '" .. headers.Authorization .. "'")
597+
598+
local httpc = http.new()
599+
openidc_configure_timeouts(httpc, opts.timeout)
600+
openidc_configure_proxy(httpc, opts.proxy_opts)
601+
local res, err = httpc:request_uri(opts.discovery.userinfo_endpoint,
602+
decorate_request(opts.http_request_decorator, {
603+
headers = headers,
604+
ssl_verify = (opts.ssl_verify ~= "no"),
605+
keepalive = (opts.keepalive ~= "no")
606+
}))
607+
if not res then
608+
err = "accessing (" .. opts.discovery.userinfo_endpoint .. ") failed: " .. err
609+
return nil, err
610+
end
611+
612+
log(DEBUG, "userinfo response: ", res.body)
613+
614+
-- parse the response from the user info endpoint
615+
return openidc_parse_json_response(res)
616+
end
617+
617618
local function can_use_token_auth_method(method, opts)
618619
local supported = supported_token_auth_methods[method]
619620
return supported and (type(supported) ~= 'function' or supported(opts))

0 commit comments

Comments
 (0)