Skip to content

Commit 0e859f9

Browse files
committed
refactor(plugins/ldap-auth): optimize the process of parsing and handling
authentication headers 1. use the `ngx.re.find` and `ngx.re.match` functions for more robust and efficient string matching operations. 2. adds error handling and logging for potential errors during authentication header parsing and credential decoding. 3. tweak the handling position for the case where `proxy_authorization_value` does not exist. Fix: [FTI-5329](https://konghq.atlassian.net/browse/FTI-5329) Signed-off-by: sabertobihwy <[email protected]>
1 parent 989ccfe commit 0e859f9

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

kong/plugins/ldap-auth/access.lua

+37-11
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ local kong = kong
66
local error = error
77
local decode_base64 = ngx.decode_base64
88
local tostring = tostring
9-
local match = string.match
9+
local re_find = ngx.re.find
10+
local re_match = ngx.re.match
1011
local lower = string.lower
1112
local upper = string.upper
12-
local find = string.find
1313
local sub = string.sub
1414
local fmt = string.format
1515
local tcp = ngx.socket.tcp
@@ -24,15 +24,37 @@ local _M = {}
2424

2525

2626
local function retrieve_credentials(authorization_header_value, conf)
27+
local lower_header_type = lower(conf.header_type)
28+
local regex = "^\\s*" .. lower_header_type .. "\\s+"
29+
local from, to, err = re_find(lower(authorization_header_value), regex, "jo")
30+
if err then
31+
kong.log.err("error while find header_type: ", lower_header_type, " in authorization header value")
32+
return nil
33+
end
34+
35+
if not from then
36+
kong.log.info("header_type: ", lower_header_type, " not found in authorization header value")
37+
return nil
38+
end
39+
2740
local username, password
28-
if authorization_header_value then
29-
local s, e = find(lower(authorization_header_value), "^%s*" ..
30-
lower(conf.header_type) .. "%s+")
31-
if s == 1 then
32-
local cred = sub(authorization_header_value, e + 1)
33-
local decoded_cred = decode_base64(cred)
34-
username, password = match(decoded_cred, "(.-):(.+)")
41+
if from == 1 then
42+
local cred = sub(authorization_header_value, to + 1)
43+
local decoded_cred = decode_base64(cred)
44+
local m, err = re_match(decoded_cred, "^(.*?):(.+)$", "jo")
45+
if err then
46+
kong.log.err("error while decoding credentials: ", err)
47+
return nil
3548
end
49+
50+
if m and type(m) == "table" and (m[1] and m[2]) then
51+
username = m[1]
52+
password = m[2]
53+
else
54+
kong.log.err("no valid credentials found in authorization header value")
55+
return nil
56+
end
57+
3658
end
3759

3860
return username, password
@@ -231,8 +253,12 @@ local function do_authentication(conf)
231253
}
232254
end
233255

234-
local is_authorized, credential = authenticate(conf, proxy_authorization_value)
235-
if not is_authorized then
256+
local is_authorized, credential
257+
if proxy_authorization_value then
258+
is_authorized, credential = authenticate(conf, proxy_authorization_value)
259+
end
260+
261+
if not is_authorized and authorization_value then
236262
is_authorized, credential = authenticate(conf, authorization_value)
237263
end
238264

0 commit comments

Comments
 (0)