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 require_provisioning = config.get("*", "core", "cyrus_require_provisioning") or false;
21 local prosody = _G.prosody;
25 local new_default_provider;
27 local function host_handler(host)
28 local host_session = hosts[host];
29 host_session.events.add_handler("item-added/auth-provider", function (provider)
30 if config.get(host, "core", "authentication") == provider.name then
31 host_session.users = provider;
34 host_session.events.add_handler("item-removed/auth-provider", function (provider)
35 if host_session.users == provider then
36 host_session.users = new_default_provider(host);
39 host_session.users = new_default_provider(host); -- Start with the default usermanager provider
41 prosody.events.add_handler("host-activated", host_handler);
42 prosody.events.add_handler("component-activated", host_handler);
44 local function is_cyrus(host) return config.get(host, "core", "sasl_backend") == "cyrus"; end
46 function new_default_provider(host)
47 local provider = { name = "default" };
49 function provider:test_password(username, password)
50 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
51 local credentials = datamanager.load(username, host, "accounts") or {};
53 if password == credentials.password then
56 return nil, "Auth failed. Invalid username or password.";
60 function provider:get_password(username)
61 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
62 return (datamanager.load(username, host, "accounts") or {}).password;
65 function provider:set_password(username, password)
66 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
67 local account = datamanager.load(username, host, "accounts");
69 account.password = password;
70 return datamanager.store(username, host, "accounts", account);
72 return nil, "Account not available.";
75 function provider:user_exists(username)
76 if not(require_provisioning) and is_cyrus(host) then return true; end
77 local account, err = datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
78 return (account or err) ~= nil; -- FIXME also check for empty credentials
81 function provider:create_user(username, password)
82 if not(require_provisioning) and is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
83 return datamanager.store(username, host, "accounts", {password = password});
86 function provider:get_supported_methods()
87 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
90 function provider:is_admin(jid)
91 local admins = config.get(host, "core", "admins");
92 if admins ~= config.get("*", "core", "admins") then
93 if type(admins) == "table" then
95 for _,admin in ipairs(admins) do
96 if admin == jid then return true; end
99 log("error", "Option 'admins' for host '%s' is not a table", host);
102 return is_admin(jid); -- Test whether it's a global admin instead
107 function validate_credentials(host, username, password, method)
108 return hosts[host].users:test_password(username, password);
111 function get_password(username, host)
112 return hosts[host].users:get_password(username);
115 function set_password(username, host, password)
116 return hosts[host].users:set_password(username, password);
119 function user_exists(username, host)
120 return hosts[host].users:user_exists(username);
123 function create_user(username, password, host)
124 return hosts[host].users:create_user(username, password);
127 function get_supported_methods(host)
128 return hosts[host].users:get_supported_methods();
131 function is_admin(jid, host)
132 if host and host ~= "*" then
133 return hosts[host].users:is_admin(jid);
134 else -- Test only whether this JID is a global admin
135 local admins = config.get("*", "core", "admins");
136 if type(admins) == "table" then
138 for _,admin in ipairs(admins) do
139 if admin == jid then return true; end
142 log("error", "Option 'admins' for host '%s' is not a table", host);
148 _M.new_default_provider = new_default_provider;