util.sql: Return failure if set_encoding() fails
[prosody.git] / util / sasl.lua
index 393a09198f2abc1f1a79508d5bbb9ccadb920c3e..b91e29a6cd51c4c8da4b54e8e4fec318f4c20488 100644 (file)
@@ -18,7 +18,6 @@ local type = type
 local setmetatable = setmetatable;
 local assert = assert;
 local require = require;
-local print = print
 
 module "sasl"
 
@@ -48,7 +47,7 @@ local backend_mechanism = {};
 local mechanism_channelbindings = {};
 
 -- register a new SASL mechanims
-local function registerMechanism(name, backends, f, cb_backends)
+function registerMechanism(name, backends, f, cb_backends)
        assert(type(name) == "string", "Parameter name MUST be a string.");
        assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
        assert(type(f) == "function", "Parameter f MUST be a function.");
@@ -68,13 +67,17 @@ end
 
 -- create a new SASL object which can be used to authenticate clients
 function new(realm, profile)
-       local mechanisms = {};
-       for backend, f in pairs(profile) do
-               if backend_mechanism[backend] then
-                       for _, mechanism in ipairs(backend_mechanism[backend]) do
-                               mechanisms[mechanism] = true;
+       local mechanisms = profile.mechanisms;
+       if not mechanisms then
+               mechanisms = {};
+               for backend, f in pairs(profile) do
+                       if backend_mechanism[backend] then
+                               for _, mechanism in ipairs(backend_mechanism[backend]) do
+                                       mechanisms[mechanism] = true;
+                               end
                        end
                end
+               profile.mechanisms = mechanisms;
        end
        return setmetatable({ profile = profile, realm = realm, mechs = mechanisms }, method);
 end
@@ -97,14 +100,16 @@ end
 function method:mechanisms()
        local current_mechs = {};
        for mech, _ in pairs(self.mechs) do
-               if mechanism_channelbindings[mech] and self.profile.cb then
-                       local ok = false;
-                       for cb_name, _ in pairs(self.profile.cb) do
-                               if mechanism_channelbindings[mech][cb_name] then
-                                       ok = true;
+               if mechanism_channelbindings[mech] then
+                       if self.profile.cb then
+                               local ok = false;
+                               for cb_name, _ in pairs(self.profile.cb) do
+                                       if mechanism_channelbindings[mech][cb_name] then
+                                               ok = true;
+                                       end
                                end
+                               if ok == true then current_mechs[mech] = true; end
                        end
-                       if ok == true then current_mechs[mech] = true; end
                else
                        current_mechs[mech] = true;
                end
@@ -131,5 +136,6 @@ require "util.sasl.plain"     .init(registerMechanism);
 require "util.sasl.digest-md5".init(registerMechanism);
 require "util.sasl.anonymous" .init(registerMechanism);
 require "util.sasl.scram"     .init(registerMechanism);
+require "util.sasl.external"  .init(registerMechanism);
 
 return _M;