2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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";
21 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
23 function validate_credentials(host, username, password, method)
24 log("debug", "User '%s' is being validated", username);
25 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
26 local credentials = datamanager.load(username, host, "accounts") or {};
28 if method == nil then method = "PLAIN"; end
29 if method == "PLAIN" and credentials.password then -- PLAIN, do directly
30 if password == credentials.password then
33 return nil, "Auth failed. Invalid username or password.";
37 -- make credentials md5
38 local pwd = credentials.password;
39 if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
41 if method == "PLAIN" then
42 password = hashes.md5(password or "", true);
43 elseif method ~= "DIGEST-MD5" then
44 return nil, "Unsupported auth method";
47 if password == pwd then
50 return nil, "Auth failed. Invalid username or password.";
54 function get_password(username, host)
55 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
56 return (datamanager.load(username, host, "accounts") or {}).password
58 function set_password(username, host, password)
59 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
60 local account = datamanager.load(username, host, "accounts");
62 account.password = password;
63 return datamanager.store(username, host, "accounts", account);
65 return nil, "Account not available.";
68 function user_exists(username, host)
69 if is_cyrus(host) then return true; end
70 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
73 function create_user(username, password, host)
74 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
75 return datamanager.store(username, host, "accounts", {password = password});
78 function get_supported_methods(host)
79 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
82 function is_admin(jid, host)
84 local admins = config.get(host, "core", "admins");
85 if host ~= "*" and admins == config.get("*", "core", "admins") then
88 if type(admins) == "table" then
90 for _,admin in ipairs(admins) do
91 if admin == jid then return true; end
93 elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end