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";
19 local prosody = _G.prosody;
23 local new_default_provider;
25 local function host_handler(host)
26 local host_session = hosts[host];
27 host_session.events.add_handler("item-added/auth-provider", function (provider)
28 if config.get(host, "core", "authentication") == provider.name then
29 host_session.users = provider;
32 host_session.events.add_handler("item-removed/auth-provider", function (provider)
33 if host_session.users == provider then
34 host_session.users = new_default_provider(host);
37 host_session.users = new_default_provider(host); -- Start with the default usermanager provider
39 prosody.events.add_handler("host-activated", host_handler);
40 prosody.events.add_handler("component-activated", host_handler);
42 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
44 function new_default_provider(host)
45 local provider = { name = "default" };
47 function provider.test_password(username, password)
48 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
49 local credentials = datamanager.load(username, host, "accounts") or {};
51 if password == credentials.password then
54 return nil, "Auth failed. Invalid username or password.";
58 function provider.get_password(username)
59 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
60 return (datamanager.load(username, host, "accounts") or {}).password;
63 function provider.set_password(username, password)
64 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
65 local account = datamanager.load(username, host, "accounts");
67 account.password = password;
68 return datamanager.store(username, host, "accounts", account);
70 return nil, "Account not available.";
73 function provider.user_exists(username)
74 if is_cyrus(host) then return true; end
75 return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
78 function provider.create_user(username, password)
79 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
80 return datamanager.store(username, host, "accounts", {password = password});
83 function provider.get_supported_methods()
84 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
87 function provider.is_admin(jid)
88 local admins = config.get(host, "core", "admins");
89 if admins ~= config.get("*", "core", "admins") then
90 if type(admins) == "table" then
92 for _,admin in ipairs(admins) do
93 if admin == jid then return true; end
96 log("error", "Option 'admins' for host '%s' is not a table", host);
99 return is_admin(jid); -- Test whether it's a global admin instead
104 function validate_credentials(host, username, password, method)
105 return hosts[host].users.test_password(username, password);
108 function get_password(username, host)
109 return hosts[host].users.get_password(username);
112 function set_password(username, host, password)
113 return hosts[host].users.set_password(username, password);
116 function user_exists(username, host)
117 return hosts[host].users.user_exists(username);
120 function create_user(username, password, host)
121 return hosts[host].users.create_user(username, password);
124 function get_supported_methods(host)
125 return hosts[host].users.get_supported_methods();
128 function is_admin(jid, host)
129 if host and host ~= "*" then
130 return hosts[host].users.is_admin(jid);
131 else -- Test only whether this JID is a global admin
132 local admins = config.get("*", "core", "admins");
133 if type(admins) == "table" then
135 for _,admin in ipairs(admins) do
136 if admin == jid then return true; end
139 log("error", "Option 'admins' for host '%s' is not a table", host);
145 _M.new_default_provider = new_default_provider;