Backed out changeset 72a2eec4204a (incomplete fix)
[prosody.git] / plugins / mod_auth_anonymous.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local new_sasl = require "util.sasl".new;
10 local datamanager = require "util.datamanager";
11
12 function new_default_provider(host)
13         local provider = { name = "anonymous" };
14
15         function provider.test_password(username, password)
16                 return nil, "Password based auth not supported.";
17         end
18
19         function provider.get_password(username)
20                 return nil, "Password not available.";
21         end
22
23         function provider.set_password(username, password)
24                 return nil, "Password based auth not supported.";
25         end
26
27         function provider.user_exists(username)
28                 return nil, "Only anonymous users are supported."; -- FIXME check if anonymous user is connected?
29         end
30
31         function provider.create_user(username, password)
32                 return nil, "Account creation/modification not supported.";
33         end
34
35         function provider.get_sasl_handler()
36                 local anonymous_authentication_profile = {
37                         anonymous = function(sasl, username, realm)
38                                 return true; -- for normal usage you should always return true here
39                         end
40                 };
41                 return new_sasl(module.host, anonymous_authentication_profile);
42         end
43
44         return provider;
45 end
46
47 local function dm_callback(username, host, datastore, data)
48         if host == module.host then
49                 return false;
50         end
51         return username, host, datastore, data;
52 end
53
54 if not module:get_option_boolean("allow_anonymous_s2s", false) then
55         module:hook("route/remote", function (event)
56                 return false; -- Block outgoing s2s from anonymous users
57         end, 300);
58 end
59
60 function module.load()
61         datamanager.add_callback(dm_callback);
62 end
63 function module.unload()
64         datamanager.remove_callback(dm_callback);
65 end
66
67 module:add_item("auth-provider", new_default_provider(module.host));
68