Merge 0.10->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 local hosts = prosody.hosts;
12
13 -- define auth provider
14 local provider = {};
15
16 function provider.test_password(username, password)
17         return nil, "Password based auth not supported.";
18 end
19
20 function provider.get_password(username)
21         return nil, "Password not available.";
22 end
23
24 function provider.set_password(username, password)
25         return nil, "Password based auth not supported.";
26 end
27
28 function provider.user_exists(username)
29         return nil, "Only anonymous users are supported."; -- FIXME check if anonymous user is connected?
30 end
31
32 function provider.create_user(username, password)
33         return nil, "Account creation/modification not supported.";
34 end
35
36 function provider.get_sasl_handler()
37         local anonymous_authentication_profile = {
38                 anonymous = function(sasl, username, realm)
39                         return true; -- for normal usage you should always return true here
40                 end
41         };
42         return new_sasl(module.host, anonymous_authentication_profile);
43 end
44
45 function provider.users()
46         return next, hosts[module.host].sessions, nil;
47 end
48
49 -- datamanager callback to disable writes
50 local function dm_callback(username, host, datastore, data)
51         if host == module.host then
52                 return false;
53         end
54         return username, host, datastore, data;
55 end
56
57 if not module:get_option_boolean("allow_anonymous_s2s", false) then
58         module:hook("route/remote", function (event)
59                 return false; -- Block outgoing s2s from anonymous users
60         end, 300);
61 end
62
63 function module.load()
64         datamanager.add_callback(dm_callback);
65 end
66 function module.unload()
67         datamanager.remove_callback(dm_callback);
68 end
69
70 module:provides("auth", provider);
71