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.
9 local datamanager = require "util.datamanager";
10 local log = require "util.logger".init("usermanager");
13 local ipairs = ipairs;
14 local hashes = require "util.hashes";
15 local jid_bare = require "util.jid".bare;
16 local config = require "core.configmanager";
20 function validate_credentials(host, username, password, method)
21 log("debug", "User '%s' is being validated", username);
22 local credentials = datamanager.load(username, host, "accounts") or {};
24 if method == nil then method = "PLAIN"; end
25 if method == "PLAIN" and credentials.password then -- PLAIN, do directly
26 if password == credentials.password then
29 return nil, "Auth failed. Invalid username or password.";
33 -- make credentials md5
34 local pwd = credentials.password;
35 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
37 if method == "PLAIN" then
38 password = hashes.md5(password or "", true);
39 elseif method ~= "DIGEST-MD5" then
40 return nil, "Unsupported auth method";
43 if password == pwd then
46 return nil, "Auth failed. Invalid username or password.";
50 function get_password(username, host)
51 return (datamanager.load(username, host, "accounts") or {}).password
54 function user_exists(username, host)
55 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
58 function create_user(username, password, host)
59 return datamanager.store(username, host, "accounts", {password = password});
62 function get_supported_methods(host)
63 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
66 function is_admin(jid, host)
68 local admins = config.get(host, "core", "admins");
69 if host ~= "*" and admins == config.get("*", "core", "admins") then
72 if type(admins) == "table" then
74 for _,admin in ipairs(admins) do
75 if admin == jid then return true; end
77 elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end