2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2010 Jeff Mitchell
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local datamanager = require "util.datamanager";
11 local log = require "util.logger".init("usermanager");
14 local ipairs = ipairs;
15 local hashes = require "util.hashes";
16 local jid_bare = require "util.jid".bare;
17 local config = require "core.configmanager";
18 local usermanager = require "core.usermanager";
21 local prosody = _G.prosody;
23 local is_cyrus = usermanager.is_cyrus;
25 function new_default_provider(host)
26 local provider = { name = "default" };
27 log("debug", "initializing default authentication provider for host '%s'", host);
29 function provider.test_password(username, password)
30 log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
31 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
32 local credentials = datamanager.load(username, host, "accounts") or {};
34 if password == credentials.password then
37 return nil, "Auth failed. Invalid username or password.";
41 function provider.get_password(username)
42 log("debug", "get_password for username '%s' at host '%s'", username, module.host);
43 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
44 return (datamanager.load(username, host, "accounts") or {}).password;
47 function provider.set_password(username, password)
48 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
49 local account = datamanager.load(username, host, "accounts");
51 account.password = password;
52 return datamanager.store(username, host, "accounts", account);
54 return nil, "Account not available.";
57 function provider.user_exists(username)
58 if is_cyrus(host) then return true; end
59 local account = datamanager.load(username, host, "accounts");
61 log("debug", "account not found for username '%s' at host '%s'", username, module.host);
62 return nil, "Auth failed. Invalid username";
64 if account.password == nil or string.len(account.password) == 0 then
65 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
66 return nil, "Auth failed. Password invalid.";
71 function provider.create_user(username, password)
72 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
73 return datamanager.store(username, host, "accounts", {password = password});
76 function provider.get_supported_methods()
77 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
80 function provider.is_admin(jid)
81 local admins = config.get(host, "core", "admins");
82 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
84 for _,admin in ipairs(admins) do
85 if admin == jid then return true; end
88 log("error", "Option 'admins' for host '%s' is not a table", host);
90 return is_admin(jid); -- Test whether it's a global admin instead
95 module:add_item("auth-provider", new_default_provider(module.host));