9 local sasl_mechanisms = features_stanza:get_child("mechanisms", xmlns_sasl); |
9 local sasl_mechanisms = features_stanza:get_child("mechanisms", xmlns_sasl); |
10 if not sasl_mechanisms then return end |
10 if not sasl_mechanisms then return end |
11 |
11 |
12 local mechanisms = {}; |
12 local mechanisms = {}; |
13 local preference = {}; |
13 local preference = {}; |
|
14 local offered = {}; |
14 |
15 |
15 for mech in sasl_mechanisms:childtags("mechanism") do |
16 for mech in sasl_mechanisms:childtags("mechanism") do |
16 mech = mech:get_text(); |
17 mech = mech:get_text(); |
17 stream:debug("Server offers %s", mech); |
18 stream:debug("Server offers %s", mech); |
|
19 offered[mech] = true; |
18 if not mechanisms[mech] then |
20 if not mechanisms[mech] then |
19 local name = mech:match("[^-]+"); |
21 local name = mech:match("[^-]+"); |
20 local ok, impl = pcall(require, "util.sasl."..name:lower()); |
22 local ok, impl = pcall(require, "util.sasl."..name:lower()); |
21 if ok then |
23 if ok then |
22 stream:debug("Loaded SASL %s module", name); |
24 stream:debug("Loaded SASL %s module", name); |
30 local supported = {}; -- by the server |
32 local supported = {}; -- by the server |
31 for mech in pairs(mechanisms) do |
33 for mech in pairs(mechanisms) do |
32 table.insert(supported, mech); |
34 table.insert(supported, mech); |
33 end |
35 end |
34 if not supported[1] then |
36 if not supported[1] then |
35 stream:event("authentication-failure", { condition = "no-supported-sasl-mechanisms", mechanisms = mechanisms }); |
37 stream:event("authentication-failure", { condition = "no-supported-sasl-mechanisms", mechanisms = offered }); |
36 stream:close(); |
38 stream:close(); |
37 return; |
39 return; |
38 end |
40 end |
39 table.sort(supported, function (a, b) return preference[a] > preference[b]; end); |
41 table.sort(supported, function (a, b) return preference[a] > preference[b]; end); |
40 local mechanism, initial_data = supported[1]; |
42 local mechanism, initial_data = supported[1]; |