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.
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;
17 local log = module._log;
18 local host = module.host;
20 local accounts = module:open_store("accounts");
24 local function replace_byte_with_hex(byte)
25 return ("%02x"):format(byte:byte());
27 function to_hex(binary_string)
28 return binary_string:gsub(".", replace_byte_with_hex);
34 local function replace_hex_with_byte(hex)
35 return string.char(tonumber(hex, 16));
37 function from_hex(hex_string)
38 return hex_string:gsub("..", replace_hex_with_byte);
43 -- Default; can be set per-user
44 local default_iteration_count = 4096;
46 -- define auth provider
49 function provider.test_password(username, password)
50 log("debug", "test password for user '%s'", username);
51 local credentials = accounts:get(username) or {};
53 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
54 if credentials.password ~= password then
55 return nil, "Auth failed. Provided password is incorrect.";
58 if provider.set_password(username, credentials.password) == nil then
59 return nil, "Auth failed. Could not set hashed password from plaintext.";
65 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
66 return nil, "Auth failed. Stored salt and iteration count information is not complete.";
69 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
71 local stored_key_hex = to_hex(stored_key);
72 local server_key_hex = to_hex(server_key);
74 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
77 return nil, "Auth failed. Invalid username, password, or password hash information.";
81 function provider.set_password(username, password)
82 log("debug", "set_password for username '%s'", username);
83 local account = accounts:get(username);
85 account.salt = generate_uuid();
86 account.iteration_count = max(account.iteration_count or 0, default_iteration_count);
87 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
88 local stored_key_hex = to_hex(stored_key);
89 local server_key_hex = to_hex(server_key);
91 account.stored_key = stored_key_hex
92 account.server_key = server_key_hex
94 account.password = nil;
95 return accounts:set(username, account);
97 return nil, "Account not available.";
100 function provider.user_exists(username)
101 local account = accounts:get(username);
103 log("debug", "account not found for username '%s'", username);
104 return nil, "Auth failed. Invalid username";
109 function provider.users()
110 return accounts:users();
113 function provider.create_user(username, password)
114 if password == nil then
115 return accounts:set(username, {});
117 local salt = generate_uuid();
118 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, default_iteration_count);
119 local stored_key_hex = to_hex(stored_key);
120 local server_key_hex = to_hex(server_key);
121 return accounts:set(username, {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = default_iteration_count});
124 function provider.delete_user(username)
125 return accounts:set(username, nil);
128 function provider.get_sasl_handler()
129 local testpass_authentication_profile = {
130 plain_test = function(sasl, username, password, realm)
131 return usermanager.test_password(username, realm, password), true;
133 scram_sha_1 = function(sasl, username, realm)
134 local credentials = accounts:get(username);
135 if not credentials then return; end
136 if credentials.password then
137 usermanager.set_password(username, credentials.password, host);
138 credentials = accounts:get(username);
139 if not credentials then return; end
142 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
143 stored_key = stored_key and from_hex(stored_key);
144 server_key = server_key and from_hex(server_key);
145 return stored_key, server_key, iteration_count, salt, true;
148 return new_sasl(host, testpass_authentication_profile);
151 module:provides("auth", provider);