Annotate

plugins/mod_defaultauth.lua @ 3163:a23168cc4af5

Working defaultauth
author Jeff Mitchell <jeff@jefferai.org>
date Thu, 20 May 2010 18:06:21 -0400
parent 3162:plugins/mod_auth_default.lua@f246719abcd2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
1 -- Prosody IM
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
4 -- Copyright (C) 2010 Jeff Mitchell
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
5 --
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
8 --
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
9
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
10 local datamanager = require "util.datamanager";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
11 local log = require "util.logger".init("usermanager");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
12 local type = type;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
13 local error = error;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
14 local ipairs = ipairs;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
15 local hashes = require "util.hashes";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
16 local jid_bare = require "util.jid".bare;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
17 local config = require "core.configmanager";
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
18 local usermanager = require "core.usermanager";
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
19 local hosts = hosts;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
20
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
21 local prosody = _G.prosody;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
22
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
23 local is_cyrus = usermanager.is_cyrus;
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
24
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
25 function new_default_provider(host)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
26 local provider = { name = "default" };
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
27 log("debug", "initializing default authentication provider for host '%s'", host);
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
28
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
29 function provider.test_password(username, password)
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
30 log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
31 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
32 local credentials = datamanager.load(username, host, "accounts") or {};
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
33
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
34 if password == credentials.password then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
35 return true;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
36 else
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
37 return nil, "Auth failed. Invalid username or password.";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
38 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
39 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
40
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
41 function provider.get_password(username)
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
42 log("debug", "get_password for username '%s' at host '%s'", username, module.host);
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
43 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
44 return (datamanager.load(username, host, "accounts") or {}).password;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
45 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
46
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
47 function provider.set_password(username, password)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
48 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
49 local account = datamanager.load(username, host, "accounts");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
50 if account then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
51 account.password = password;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
52 return datamanager.store(username, host, "accounts", account);
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
53 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
54 return nil, "Account not available.";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
55 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
56
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
57 function provider.user_exists(username)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
58 if is_cyrus(host) then return true; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
59 local account = datamanager.load(username, host, "accounts");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
60 if not account then
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
61 log("debug", "account not found for username '%s' at host '%s'", username, module.host);
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
62 return nil, "Auth failed. Invalid username";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
63 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
64 if account.password == nil or string.len(account.password) == 0 then
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
65 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
66 return nil, "Auth failed. Password invalid.";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
67 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
68 return true;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
69 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
70
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
71 function provider.create_user(username, password)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
72 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
73 return datamanager.store(username, host, "accounts", {password = password});
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
74 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
75
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
76 function provider.get_supported_methods()
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
77 return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
78 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
79
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
80 function provider.is_admin(jid)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
81 local admins = config.get(host, "core", "admins");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
82 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
83 jid = jid_bare(jid);
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
84 for _,admin in ipairs(admins) do
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
85 if admin == jid then return true; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
86 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
87 elseif admins then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
88 log("error", "Option 'admins' for host '%s' is not a table", host);
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
89 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
90 return is_admin(jid); -- Test whether it's a global admin instead
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
91 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
92 return provider;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
93 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
94
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
95 module:add_item("auth-provider", new_default_provider(module.host));
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
96