util.stanza: Rewrite clone() to be more optimized.
[prosody.git] / plugins / mod_auth_internal_hashed.lua
index da406e0602c308e5ff096df47256bbddfcfa474b..3ce8c0768cdf10c8e38c64852cfc31415b884fc6 100644 (file)
@@ -22,7 +22,7 @@ local new_sasl = require "util.sasl".new;
 local nodeprep = require "util.encodings".stringprep.nodeprep;
 local hosts = hosts;
 
--- TODO: remove these two lines in near future
+-- COMPAT w/old trunk: remove these two lines before 0.8 release
 local hmac_sha1 = require "util.hmac".sha1;
 local sha1 = require "util.hashes".sha1;
 
@@ -76,7 +76,7 @@ function new_hashpass_provider(host)
                end
                
                -- convert hexpass to stored_key and server_key
-               -- TODO: remove this in near future
+               -- COMPAT w/old trunk: remove before 0.8 release
                if credentials.hashpass then
                        local salted_password = from_hex(credentials.hashpass);
                        credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key"), true);
@@ -121,14 +121,13 @@ function new_hashpass_provider(host)
                        log("debug", "account not found for username '%s' at host '%s'", username, module.host);
                        return nil, "Auth failed. Invalid username";
                end
-               --[[if (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
-                       log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
-                       return nil, "Auth failed. Password invalid.";
-               end]]
                return true;
        end
 
        function provider.create_user(username, password)
+               if password == nil then
+                       return datamanager.store(username, host, "accounts", {});
+               end
                local salt = generate_uuid();
                local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
                local stored_key_hex = to_hex(stored_key);
@@ -136,26 +135,32 @@ function new_hashpass_provider(host)
                return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
        end
 
+       function provider.delete_user(username)
+               return datamanager.store(username, host, "accounts", nil);
+       end
+
        function provider.get_sasl_handler()
                local realm = module:get_option("sasl_realm") or module.host;
                local testpass_authentication_profile = {
-                       plain_test = function(username, password, realm)
+                       plain_test = function(sasl, username, password, realm)
                                local prepped_username = nodeprep(username);
                                if not prepped_username then
                                        log("debug", "NODEprep failed on username: %s", username);
                                        return "", nil;
                                end
-                               return usermanager.test_password(prepped_username, password, realm), true;
+                               return usermanager.test_password(prepped_username, realm, password), true;
                        end,
-                       scram_sha_1 = function(username, realm)
-                               local credentials = datamanager.load(username, host, "accounts") or {};
+                       scram_sha_1 = function(sasl, username, realm)
+                               local credentials = datamanager.load(username, host, "accounts");
+                               if not credentials then return; end
                                if credentials.password then
                                        usermanager.set_password(username, credentials.password, host);
-                                       credentials = datamanager.load(username, host, "accounts") or {};
+                                       credentials = datamanager.load(username, host, "accounts");
+                                       if not credentials then return; end
                                end
                                
                                -- convert hexpass to stored_key and server_key
-                               -- TODO: remove this in near future
+                               -- COMPAT w/old trunk: remove before 0.8 release
                                if credentials.hashpass then
                                        local salted_password = from_hex(credentials.hashpass);
                                        credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key"), true);
@@ -163,7 +168,7 @@ function new_hashpass_provider(host)
                                        credentials.hashpass = nil
                                        datamanager.store(username, host, "accounts", credentials);
                                end
-                               
+                       
                                local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
                                stored_key = stored_key and from_hex(stored_key);
                                server_key = server_key and from_hex(server_key);