2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 require "util.datamanager"
12 local datamanager = datamanager;
13 local log = require "util.logger".init("usermanager");
15 local hashes = require "util.hashes";
19 function validate_credentials(host, username, password, method)
20 log("debug", "User '%s' is being validated", username);
21 local credentials = datamanager.load(username, host, "accounts") or {};
22 if method == nil then method = "PLAIN"; end
23 if method == "PLAIN" and credentials.password then -- PLAIN, do directly
24 if password == credentials.password then
27 return nil, "Auth failed. Invalid username or password.";
31 -- make credentials md5
32 local pwd = credentials.password;
33 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
35 if method == "PLAIN" then
36 password = hashes.md5(password or "", true);
37 elseif method ~= "DIGEST-MD5" then
38 return nil, "Unsupported auth method";
41 if password == pwd then
44 return nil, "Auth failed. Invalid username or password.";
48 function user_exists(username, host)
49 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
52 function create_user(username, password, host)
53 return datamanager.store(username, host, "accounts", {password = password});
56 function get_supported_methods(host)
57 local methods = {["PLAIN"] = true}; -- TODO this should be taken from the config
58 methods["DIGEST-MD5"] = true;