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 getAuthenticationDatabaseSHA1 = require "util.sasl.scram".getAuthenticationDatabaseSHA1;
11 local usermanager = require "core.usermanager";
12 local generate_uuid = require "util.uuid".generate;
13 local new_sasl = require "util.sasl".new;
14
15 local log = module._log;
16 local host = module.host;
17
18 local accounts = module:open_store("accounts");
19
20 local to_hex;
21 do
22         local function replace_byte_with_hex(byte)
23                 return ("%02x"):format(byte:byte());
24         end
25         function to_hex(binary_string)
26                 return binary_string:gsub(".", replace_byte_with_hex);
27         end
28 end
29
30 local from_hex;
31 do
32         local function replace_hex_with_byte(hex)
33                 return string.char(tonumber(hex, 16));
34         end
35         function from_hex(hex_string)
36                 return hex_string:gsub("..", replace_hex_with_byte);
37         end
38 end
39
40
41 -- Default; can be set per-user
42 local iteration_count = 4096;
43
44 -- define auth provider
45 local provider = {};
46
47 function provider.test_password(username, password)
48         log("debug", "test password for user '%s'", username);
49         local credentials = accounts:get(username) or {};
50
51         if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
52                 if credentials.password ~= password then
53                         return nil, "Auth failed. Provided password is incorrect.";
54                 end
55
56                 if provider.set_password(username, credentials.password) == nil then
57                         return nil, "Auth failed. Could not set hashed password from plaintext.";
58                 else
59                         return true;
60                 end
61         end
62
63         if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
64                 return nil, "Auth failed. Stored salt and iteration count information is not complete.";
65         end
66
67         local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
68
69         local stored_key_hex = to_hex(stored_key);
70         local server_key_hex = to_hex(server_key);
71
72         if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
73                 return true;
74         else
75                 return nil, "Auth failed. Invalid username, password, or password hash information.";
76         end
77 end
78
79 function provider.set_password(username, password)
80         log("debug", "set_password for username '%s'", username);
81         local account = accounts:get(username);
82         if account then
83                 account.salt = account.salt or generate_uuid();
84                 account.iteration_count = account.iteration_count or iteration_count;
85                 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
86                 local stored_key_hex = to_hex(stored_key);
87                 local server_key_hex = to_hex(server_key);
88
89                 account.stored_key = stored_key_hex
90                 account.server_key = server_key_hex
91
92                 account.password = nil;
93                 return accounts:set(username, account);
94         end
95         return nil, "Account not available.";
96 end
97
98 function provider.user_exists(username)
99         local account = accounts:get(username);
100         if not account then
101                 log("debug", "account not found for username '%s'", username);
102                 return nil, "Auth failed. Invalid username";
103         end
104         return true;
105 end
106
107 function provider.users()
108         return accounts:users();
109 end
110
111 function provider.create_user(username, password)
112         if password == nil then
113                 return accounts:set(username, {});
114         end
115         local salt = generate_uuid();
116         local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count);
117         local stored_key_hex = to_hex(stored_key);
118         local server_key_hex = to_hex(server_key);
119         return accounts:set(username, {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
120 end
121
122 function provider.delete_user(username)
123         return accounts:set(username, nil);
124 end
125
126 function provider.get_sasl_handler()
127         local testpass_authentication_profile = {
128                 plain_test = function(sasl, username, password, realm)
129                         return usermanager.test_password(username, realm, password), true;
130                 end,
131                 scram_sha_1 = function(sasl, username, realm)
132                         local credentials = accounts:get(username);
133                         if not credentials then return; end
134                         if credentials.password then
135                                 usermanager.set_password(username, credentials.password, host);
136                                 credentials = accounts:get(username);
137                                 if not credentials then return; end
138                         end
139
140                         local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
141                         stored_key = stored_key and from_hex(stored_key);
142                         server_key = server_key and from_hex(server_key);
143                         return stored_key, server_key, iteration_count, salt, true;
144                 end
145         };
146         return new_sasl(host, testpass_authentication_profile);
147 end
148
149 module:provides("auth", provider);
150