mod_dialback: Keep the same dialback secret across module reloads
[prosody.git] / plugins / mod_auth_internal_hashed.lua
index 573967318a604aacda7c43dddb00c5eaeb502e69..954392c96a738a8a1b7a10fff0c8dbc2f3ac6981 100644 (file)
@@ -7,12 +7,16 @@
 -- COPYING file in the source package for more information.
 --
 
-local log = require "util.logger".init("auth_internal_hashed");
+local max = math.max;
+
 local getAuthenticationDatabaseSHA1 = require "util.sasl.scram".getAuthenticationDatabaseSHA1;
 local usermanager = require "core.usermanager";
 local generate_uuid = require "util.uuid".generate;
 local new_sasl = require "util.sasl".new;
 
+local log = module._log;
+local host = module.host;
+
 local accounts = module:open_store("accounts");
 
 local to_hex;
@@ -37,14 +41,13 @@ end
 
 
 -- Default; can be set per-user
-local iteration_count = 4096;
+local default_iteration_count = 4096;
 
-local host = module.host;
 -- define auth provider
 local provider = {};
-log("debug", "initializing internal_hashed authentication provider for host '%s'", host);
 
 function provider.test_password(username, password)
+       log("debug", "test password for user '%s'", username);
        local credentials = accounts:get(username) or {};
 
        if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
@@ -76,10 +79,11 @@ function provider.test_password(username, password)
 end
 
 function provider.set_password(username, password)
+       log("debug", "set_password for username '%s'", username);
        local account = accounts:get(username);
        if account then
-               account.salt = account.salt or generate_uuid();
-               account.iteration_count = account.iteration_count or iteration_count;
+               account.salt = generate_uuid();
+               account.iteration_count = max(account.iteration_count or 0, default_iteration_count);
                local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
                local stored_key_hex = to_hex(stored_key);
                local server_key_hex = to_hex(server_key);
@@ -96,7 +100,7 @@ end
 function provider.user_exists(username)
        local account = accounts:get(username);
        if not account then
-               log("debug", "account not found for username '%s' at host '%s'", username, host);
+               log("debug", "account not found for username '%s'", username);
                return nil, "Auth failed. Invalid username";
        end
        return true;
@@ -111,10 +115,10 @@ function provider.create_user(username, password)
                return accounts:set(username, {});
        end
        local salt = generate_uuid();
-       local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
+       local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, default_iteration_count);
        local stored_key_hex = to_hex(stored_key);
        local server_key_hex = to_hex(server_key);
-       return accounts:set(username, {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
+       return accounts:set(username, {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = default_iteration_count});
 end
 
 function provider.delete_user(username)