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");
16 local ipairs = ipairs;
17 local hashes = require "util.hashes";
18 local jid_bare = require "util.jid".bare;
19 local config = require "core.configmanager";
23 function validate_credentials(host, username, password, method)
24 log("debug", "User '%s' is being validated", username);
25 local credentials = datamanager.load(username, host, "accounts") or {};
27 if method == nil then method = "PLAIN"; end
28 if method == "PLAIN" and credentials.password then -- PLAIN, do directly
29 if password == credentials.password then
32 return nil, "Auth failed. Invalid username or password.";
36 -- make credentials md5
37 local pwd = credentials.password;
38 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
40 if method == "PLAIN" then
41 password = hashes.md5(password or "", true);
42 elseif method ~= "DIGEST-MD5" then
43 return nil, "Unsupported auth method";
46 if password == pwd then
49 return nil, "Auth failed. Invalid username or password.";
53 function get_password(username, host)
54 return (datamanager.load(username, host, "accounts") or {}).password
57 function user_exists(username, host)
58 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
61 function create_user(username, password, host)
62 return datamanager.store(username, host, "accounts", {password = password});
65 function get_supported_methods(host)
66 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
69 function is_admin(jid)
70 local admins = config.get("*", "core", "admins");
71 if type(admins) == "table" then
73 for _,admin in ipairs(admins) do
74 if admin == jid then return true; end
76 else log("debug", "Option core.admins is not a table"); end