plugins/mod_auth_internal.lua

Mon, 07 Jun 2010 02:38:20 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Mon, 07 Jun 2010 02:38:20 +0500
changeset 3191
a475fbce1990
parent 3190
b5f261123013
child 3219
fd06023cbdcc
permissions
-rw-r--r--

mod_auth_internal, mod_auth_internal_hashed: Fixed a global access.

3157
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";
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
18 local usermanager = require "core.usermanager";
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
19 local new_sasl = require "util.sasl".new;
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
20 local nodeprep = require "util.encodings".stringprep.nodeprep;
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
21 local hosts = hosts;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
22
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
23 local prosody = _G.prosody;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
24
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
25 local is_cyrus = usermanager.is_cyrus;
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
26
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
27 function new_default_provider(host)
3180
99be525bcfb4 Rename mod_defaultauth -> mod_auth_internal, mod_hashpassauth -> mod_auth_internal_hashed, and the providers to internal and internal_hashed respectively. Also no longer auto-load defaultauth, but instead auto-load the plugin selected for each host at startup based on the provider name.
Matthew Wild <mwild1@gmail.com>
parents: 3158
diff changeset
28 local provider = { name = "internal" };
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
29 log("debug", "initializing default authentication provider for host '%s'", host);
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
30
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
31 function provider.test_password(username, password)
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
32 log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
33 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
34 local credentials = datamanager.load(username, host, "accounts") or {};
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
35
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
36 if password == credentials.password then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
37 return true;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
38 else
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
39 return nil, "Auth failed. Invalid username or password.";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
40 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
41 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
42
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
43 function provider.get_password(username)
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
44 log("debug", "get_password for username '%s' at host '%s'", username, module.host);
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
45 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
46 return (datamanager.load(username, host, "accounts") or {}).password;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
47 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
48
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
49 function provider.set_password(username, password)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
50 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
51 local account = datamanager.load(username, host, "accounts");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
52 if account then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
53 account.password = password;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
54 return datamanager.store(username, host, "accounts", account);
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 return nil, "Account not available.";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
57 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
58
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
59 function provider.user_exists(username)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
60 if is_cyrus(host) then return true; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
61 local account = datamanager.load(username, host, "accounts");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
62 if not account then
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
63 log("debug", "account not found for username '%s' at host '%s'", username, module.host);
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
64 return nil, "Auth failed. Invalid username";
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
65 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
66 if account.password == nil or string.len(account.password) == 0 then
3158
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3157
diff changeset
67 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
68 return nil, "Auth failed. Password invalid.";
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 return true;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
71 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
72
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
73 function provider.create_user(username, password)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
74 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
75 return datamanager.store(username, host, "accounts", {password = password});
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
76 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
77
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
78 function provider.get_sasl_handler()
3191
a475fbce1990 mod_auth_internal, mod_auth_internal_hashed: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents: 3190
diff changeset
79 local realm = module:get_option("sasl_realm") or module.host;
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
80 local getpass_authentication_profile = {
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
81 plain = function(username, realm)
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
82 local prepped_username = nodeprep(username);
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
83 if not prepped_username then
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
84 log("debug", "NODEprep failed on username: %s", username);
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
85 return "", nil;
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
86 end
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
87 local password = usermanager.get_password(prepped_username, realm);
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
88 if not password then
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
89 return "", nil;
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
90 end
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
91 return password, true;
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
92 end
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
93 };
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
94 return new_sasl(realm, getpass_authentication_profile);
3157
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
95 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
96
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
97 function provider.is_admin(jid)
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
98 local admins = config.get(host, "core", "admins");
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
99 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
100 jid = jid_bare(jid);
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
101 for _,admin in ipairs(admins) do
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
102 if admin == jid then return true; end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
103 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
104 elseif admins then
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
105 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
106 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
107 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
108 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
109 return provider;
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
110 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
111
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
112 module:add_item("auth-provider", new_default_provider(module.host));
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
113

mercurial