Merge 0.9->0.10
[prosody.git] / plugins / mod_auth_internal_hashed.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2010 Jeff Mitchell
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local max = math.max;
11
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
17 local log = module._log;
18 local host = module.host;
19
20 local accounts = module:open_store("accounts");
21
22 local to_hex;
23 do
24         local function replace_byte_with_hex(byte)
25                 return ("%02x"):format(byte:byte());
26         end
27         function to_hex(binary_string)
28                 return binary_string:gsub(".", replace_byte_with_hex);
29         end
30 end
31
32 local from_hex;
33 do
34         local function replace_hex_with_byte(hex)
35                 return string.char(tonumber(hex, 16));
36         end
37         function from_hex(hex_string)
38                 return hex_string:gsub("..", replace_hex_with_byte);
39         end
40 end
41
42
43 -- Default; can be set per-user
44 local default_iteration_count = 4096;
45
46 -- define auth provider
47 local provider = {};
48
49 function provider.test_password(username, password)
50         log("debug", "test password for user '%s'", username);
51         local credentials = accounts:get(username) or {};
52
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.";
56                 end
57
58                 if provider.set_password(username, credentials.password) == nil then
59                         return nil, "Auth failed. Could not set hashed password from plaintext.";
60                 else
61                         return true;
62                 end
63         end
64
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.";
67         end
68
69         local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
70
71         local stored_key_hex = to_hex(stored_key);
72         local server_key_hex = to_hex(server_key);
73
74         if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
75                 return true;
76         else
77                 return nil, "Auth failed. Invalid username, password, or password hash information.";
78         end
79 end
80
81 function provider.set_password(username, password)
82         log("debug", "set_password for username '%s'", username);
83         local account = accounts:get(username);
84         if account then
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);
90
91                 account.stored_key = stored_key_hex
92                 account.server_key = server_key_hex
93
94                 account.password = nil;
95                 return accounts:set(username, account);
96         end
97         return nil, "Account not available.";
98 end
99
100 function provider.user_exists(username)
101         local account = accounts:get(username);
102         if not account then
103                 log("debug", "account not found for username '%s'", username);
104                 return nil, "Auth failed. Invalid username";
105         end
106         return true;
107 end
108
109 function provider.users()
110         return accounts:users();
111 end
112
113 function provider.create_user(username, password)
114         if password == nil then
115                 return accounts:set(username, {});
116         end
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});
122 end
123
124 function provider.delete_user(username)
125         return accounts:set(username, nil);
126 end
127
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;
132                 end,
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
140                         end
141
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;
146                 end
147         };
148         return new_sasl(host, testpass_authentication_profile);
149 end
150
151 module:provides("auth", provider);
152