2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2010 Jeff Mitchell
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local datamanager = require "util.datamanager";
11 local log = require "util.logger".init("auth_internal_hashed");
12 local getAuthenticationDatabaseSHA1 = require "util.sasl.scram".getAuthenticationDatabaseSHA1;
13 local usermanager = require "core.usermanager";
14 local generate_uuid = require "util.uuid".generate;
15 local new_sasl = require "util.sasl".new;
16 local nodeprep = require "util.encodings".stringprep.nodeprep;
20 local function replace_byte_with_hex(byte)
21 return ("%02x"):format(byte:byte());
23 function to_hex(binary_string)
24 return binary_string:gsub(".", replace_byte_with_hex);
30 local function replace_hex_with_byte(hex)
31 return string.char(tonumber(hex, 16));
33 function from_hex(hex_string)
34 return hex_string:gsub("..", replace_hex_with_byte);
39 -- Default; can be set per-user
40 local iteration_count = 4096;
42 local host = module.host;
43 -- define auth provider
45 log("debug", "initializing internal_hashed authentication provider for host '%s'", host);
47 function provider.test_password(username, password)
48 local credentials = datamanager.load(username, host, "accounts") or {};
50 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
51 if credentials.password ~= password then
52 return nil, "Auth failed. Provided password is incorrect.";
55 if provider.set_password(username, credentials.password) == nil then
56 return nil, "Auth failed. Could not set hashed password from plaintext.";
62 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
63 return nil, "Auth failed. Stored salt and iteration count information is not complete.";
66 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
68 local stored_key_hex = to_hex(stored_key);
69 local server_key_hex = to_hex(server_key);
71 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
74 return nil, "Auth failed. Invalid username, password, or password hash information.";
78 function provider.set_password(username, password)
79 local account = datamanager.load(username, host, "accounts");
81 account.salt = account.salt or generate_uuid();
82 account.iteration_count = account.iteration_count or iteration_count;
83 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
84 local stored_key_hex = to_hex(stored_key);
85 local server_key_hex = to_hex(server_key);
87 account.stored_key = stored_key_hex
88 account.server_key = server_key_hex
90 account.password = nil;
91 return datamanager.store(username, host, "accounts", account);
93 return nil, "Account not available.";
96 function provider.user_exists(username)
97 local account = datamanager.load(username, host, "accounts");
99 log("debug", "account not found for username '%s' at host '%s'", username, host);
100 return nil, "Auth failed. Invalid username";
105 function provider.users()
106 return datamanager.users(host, "accounts");
109 function provider.create_user(username, password)
110 if password == nil then
111 return datamanager.store(username, host, "accounts", {});
113 local salt = generate_uuid();
114 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
115 local stored_key_hex = to_hex(stored_key);
116 local server_key_hex = to_hex(server_key);
117 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
120 function provider.delete_user(username)
121 return datamanager.store(username, host, "accounts", nil);
124 function provider.get_sasl_handler()
125 local testpass_authentication_profile = {
126 plain_test = function(sasl, username, password, realm)
127 local prepped_username = nodeprep(username);
128 if not prepped_username then
129 log("debug", "NODEprep failed on username: %s", username);
132 return usermanager.test_password(prepped_username, realm, password), true;
134 scram_sha_1 = function(sasl, username, realm)
135 local credentials = datamanager.load(username, host, "accounts");
136 if not credentials then return; end
137 if credentials.password then
138 usermanager.set_password(username, credentials.password, host);
139 credentials = datamanager.load(username, host, "accounts");
140 if not credentials then return; end
143 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
144 stored_key = stored_key and from_hex(stored_key);
145 server_key = server_key and from_hex(server_key);
146 return stored_key, server_key, iteration_count, salt, true;
149 return new_sasl(host, testpass_authentication_profile);
152 module:provides("auth", provider);