Forced merge.
[prosody.git] / core / usermanager.lua
1
2 require "util.datamanager"
3 local datamanager = datamanager;
4 local log = require "util.logger".init("usermanager");
5 local error = error;
6 local hashes = require "util.hashes";
7
8 module "usermanager"
9
10 function validate_credentials(host, username, password, method)
11         log("debug", "User '%s' is being validated", username);
12         local credentials = datamanager.load(username, host, "accounts") or {};
13         if method == nil then method = "PLAIN"; end
14         if method == "PLAIN" and credentials.password then -- PLAIN, do directly
15                 if password == credentials.password then
16                         return true;
17                 else
18                         return nil, "Auth failed. Invalid username or password.";
19                 end
20         end
21         -- must do md5
22         -- make credentials md5
23         local pwd = credentials.password;
24         if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
25         -- make password md5
26         if method == "PLAIN" then
27                 password = hashes.md5(password or "", true);
28         elseif method ~= "DIGEST-MD5" then
29                 return nil, "Unsupported auth method";
30         end
31         -- compare
32         if password == pwd then
33                 return true;
34         else
35                 return nil, "Auth failed. Invalid username or password.";
36         end
37 end
38
39 function user_exists(username, host)
40         return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
41 end
42
43 function create_user(username, password, host)
44         return datamanager.store(username, host, "accounts", {password = password});
45 end
46
47 function get_supported_methods(host)
48         local methods = {["PLAIN"] = true}; -- TODO this should be taken from the config
49         methods["DIGEST-MD5"] = true;
50         return methods;
51 end
52
53 return _M;