Merge 0.9->trunk
[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 -- define auth provider
13 local provider = {};
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 -- datamanager callback to disable writes
45 local function dm_callback(username, host, datastore, data)
46         if host == module.host then
47                 return false;
48         end
49         return username, host, datastore, data;
50 end
51
52 if not module:get_option_boolean("allow_anonymous_s2s", false) then
53         module:hook("route/remote", function (event)
54                 return false; -- Block outgoing s2s from anonymous users
55         end, 300);
56 end
57
58 function module.load()
59         datamanager.add_callback(dm_callback);
60 end
61 function module.unload()
62         datamanager.remove_callback(dm_callback);
63 end
64
65 module:provides("auth", provider);
66