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 -- make credentials md5
23 local pwd = credentials.password;
24 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
26 if method == "PLAIN" then
27 password = hashes.md5(password or "", true);
28 elseif method ~= "DIGEST-MD5" then
29 return nil, "Unsupported auth method";
32 if password == pwd then
35 return nil, "Auth failed. Invalid username or password.";
39 function user_exists(username, host)
40 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
43 function create_user(username, password, host)
44 return datamanager.store(username, host, "accounts", {password = password});
47 function get_supported_methods(host)
48 local methods = {["PLAIN"] = true}; -- TODO this should be taken from the config
49 methods["DIGEST-MD5"] = true;