Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 1f12bd0

Browse files
committed
Add support for both http and https. Remove custom regex url parsing
1 parent 74412ba commit 1f12bd0

File tree

3 files changed

+17
-38
lines changed

3 files changed

+17
-38
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ IMAGE?=kong-plugin-jwt-keycloak
55
KONG_VERSION?=1.1.2
66
FULL_IMAGE_NAME:=${REPOSITORY}/${IMAGE}:${KONG_VERSION}
77

8-
PLUGIN_VERSION?=1.0.2-1
8+
PLUGIN_VERSION?=1.0.3-1
99

1010
TEST_VERSIONS?=1.0.3 1.1.2
1111

src/keycloak_keys.lua

+13-34
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,30 @@
1+
local url = require "socket.url"
2+
local http = require "socket.http"
13
local https = require "ssl.https"
24
local cjson_safe = require "cjson.safe"
35
local convert = require "kong.plugins.jwt-keycloak.key_conversion"
46

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
2313
end
2414

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)
3615
local res
3716
local status
3817
local err
3918

4019
local chunks = {}
41-
res, status = https.request{
20+
res, status = req{
4221
url = url,
4322
port = port,
4423
sink = ltn12.sink.table(chunks)
4524
}
4625

4726
if status ~= 200 then
48-
return nil, 'Failed calling url ' .. url
27+
return nil, 'Failed calling url ' .. url .. ' response status ' .. status
4928
end
5029

5130
res, err = cjson_safe.decode(table.concat(chunks))
@@ -62,14 +41,14 @@ end
6241

6342
local function get_issuer_keys(well_known_endpoint)
6443
-- 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)
6645

67-
local res, err = get_request(well_known_endpoint, req.port)
46+
local res, err = get_request(well_known_endpoint, req.scheme, req.port)
6847
if err then
6948
return nil, err
7049
end
7150

72-
local res, err = get_request(res['jwks_uri'], req.port)
51+
local res, err = get_request(res['jwks_uri'], req.scheme, req.port)
7352
if err then
7453
return nil, err
7554
end

tests/unit_tests/tests/key_conversion_spec.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("Keycloak key conversion", function()
1010
local issuer = "http://localhost:8080/auth/realms/master"
1111

1212
res1, err1 = get_issuer_keys(get_wellknown_endpoint(well_known_template, issuer))
13-
res2, err2 = get_request(issuer)
13+
res2, err2 = get_request(issuer, "http")
1414

1515
assert.same(res2['public_key'], res1[1])
1616
end)
@@ -22,7 +22,7 @@ describe("Keycloak key conversion", function()
2222
res1, err1 = get_issuer_keys(get_wellknown_endpoint(well_known_template, issuer))
2323

2424
assert.same(nil, res1)
25-
assert.same('Failed calling url http://localhost:8080/auth/realms/does_not_exist/.well-known/openid-configuration', err1)
25+
assert.same('Failed calling url http://localhost:8080/auth/realms/does_not_exist/.well-known/openid-configuration response status 404', err1)
2626
end)
2727

2828
it("should fail on bad issuer", function()
@@ -31,7 +31,7 @@ describe("Keycloak key conversion", function()
3131
res1, err1 = get_issuer_keys(get_wellknown_endpoint(well_known_template, issuer))
3232

3333
assert.same(nil, res1)
34-
assert.same('Failed calling url http://localhost:8081/auth/realms/does_not_exist/.well-known/openid-configuration', err1)
34+
assert.same('Failed calling url http://localhost:8081/auth/realms/does_not_exist/.well-known/openid-configuration response status closed', err1)
3535
end)
3636

3737
end)

0 commit comments

Comments
 (0)