|
| 1 | +local url = require "socket.url" |
| 2 | +local http = require "socket.http" |
1 | 3 | local https = require "ssl.https"
|
2 | 4 | local cjson_safe = require "cjson.safe"
|
3 | 5 | local convert = require "kong.plugins.jwt-keycloak.key_conversion"
|
4 | 6 |
|
5 |
| -local function parse_url(url) |
6 |
| - local chunk, protocol = url:match("^(([a-z0-9+]+)://)") |
7 |
| - url = url:sub((chunk and #chunk or 0) + 1) |
8 |
| - |
9 |
| - local auth |
10 |
| - chunk, auth = url:match('(([%w%p]+:?[%w%p]+)@)') |
11 |
| - url = url:sub((chunk and #chunk or 0) + 1) |
12 |
| - |
13 |
| - local host |
14 |
| - local hostname |
15 |
| - local port |
16 |
| - if protocol then |
17 |
| - host = url:match("^([%a%.%d-]+:?%d*)") |
18 |
| - if host then |
19 |
| - hostname = host:match("^([^:/]+)") |
20 |
| - port = host:match(":(%d+)$") |
21 |
| - end |
22 |
| - url = url:sub((host and #host or 0) + 1) |
| 7 | +local function get_request(url, scheme, port) |
| 8 | + local req |
| 9 | + if scheme == "https" then |
| 10 | + req = https.request |
| 11 | + else |
| 12 | + req = http.request |
23 | 13 | end
|
24 | 14 |
|
25 |
| - local parsed = { |
26 |
| - protocol = protocol, |
27 |
| - host = host, |
28 |
| - hostname = hostname, |
29 |
| - port = port, |
30 |
| - } |
31 |
| - |
32 |
| - return parsed |
33 |
| -end |
34 |
| - |
35 |
| -local function get_request(url, port) |
36 | 15 | local res
|
37 | 16 | local status
|
38 | 17 | local err
|
39 | 18 |
|
40 | 19 | local chunks = {}
|
41 |
| - res, status = https.request{ |
| 20 | + res, status = req{ |
42 | 21 | url = url,
|
43 | 22 | port = port,
|
44 | 23 | sink = ltn12.sink.table(chunks)
|
45 | 24 | }
|
46 | 25 |
|
47 | 26 | if status ~= 200 then
|
48 |
| - return nil, 'Failed calling url ' .. url |
| 27 | + return nil, 'Failed calling url ' .. url .. ' response status ' .. status |
49 | 28 | end
|
50 | 29 |
|
51 | 30 | res, err = cjson_safe.decode(table.concat(chunks))
|
|
62 | 41 |
|
63 | 42 | local function get_issuer_keys(well_known_endpoint)
|
64 | 43 | -- Get port of the request: This is done because keycloak 3.X.X does not play well with lua socket.http
|
65 |
| - local req = parse_url(well_known_endpoint) |
| 44 | + local req = url.parse(well_known_endpoint) |
66 | 45 |
|
67 |
| - local res, err = get_request(well_known_endpoint, req.port) |
| 46 | + local res, err = get_request(well_known_endpoint, req.scheme, req.port) |
68 | 47 | if err then
|
69 | 48 | return nil, err
|
70 | 49 | end
|
71 | 50 |
|
72 |
| - local res, err = get_request(res['jwks_uri'], req.port) |
| 51 | + local res, err = get_request(res['jwks_uri'], req.scheme, req.port) |
73 | 52 | if err then
|
74 | 53 | return nil, err
|
75 | 54 | end
|
|
0 commit comments