2 require "util.datamanager"
3 local datamanager = datamanager;
4 local log = require "util.logger".init("usermanager");
6 local hashes = require "util.hashes";
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
18 return nil, "Auth failed. Invalid username or password.";
22 if not hashes.md5 then
23 return nil, "Server misconfiguration, the md5 library is not available.";
25 -- make credentials md5
26 local pwd = credentials.password;
27 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd); end
29 if method == "PLAIN" then
30 password = hashes.md5(password or "");
31 elseif method ~= "DIGEST-MD5" then
32 return nil, "Unsupported auth method";
35 if password == pwd then
38 return nil, "Auth failed. Invalid username or password.";
42 function user_exists(username, host)
43 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
46 function create_user(username, password, host)
47 return datamanager.store(username, host, "accounts", {password = password});
50 function get_supported_methods(host)
51 local methods = {["PLAIN"] = true}; -- TODO this should be taken from the config
53 methods["DIGEST-MD5"] = true;