MUC: Added multi-session support to the room-exiting occupant use case.
[prosody.git] / plugins / mod_legacyauth.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10
11 local st = require "util.stanza";
12 local t_concat = table.concat;
13
14 local secure_auth_only = module:get_option("require_encryption");
15
16 local sessionmanager = require "core.sessionmanager";
17 local usermanager = require "core.usermanager";
18
19 module:add_feature("jabber:iq:auth");
20 module:add_event_hook("stream-features", function (session, features)
21         if secure_auth_only and not session.secure then
22                 -- Sorry, not offering to insecure streams!
23                 return;
24         elseif not session.username then
25                 features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
26         end
27 end);
28
29 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
30                 function (session, stanza)
31                         if secure_auth_only and not session.secure then
32                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
33                                 return true;
34                         end
35                         
36                         local username = stanza.tags[1]:child_with_name("username");
37                         local password = stanza.tags[1]:child_with_name("password");
38                         local resource = stanza.tags[1]:child_with_name("resource");
39                         if not (username and password and resource) then
40                                 local reply = st.reply(stanza);
41                                 session.send(reply:query("jabber:iq:auth")
42                                         :tag("username"):up()
43                                         :tag("password"):up()
44                                         :tag("resource"):up());
45                         else
46                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
47                                 local reply = st.reply(stanza);
48                                 if usermanager.validate_credentials(session.host, username, password) then
49                                         -- Authentication successful!
50                                         local success, err = sessionmanager.make_authenticated(session, username);
51                                         if success then
52                                                 local err_type, err_msg;
53                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
54                                                 if not success then
55                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
56                                                         return true; -- FIXME need to unauthenticate here
57                                                 end
58                                         end
59                                         session.send(st.reply(stanza));
60                                 else
61                                         session.send(st.error_reply(stanza, "auth", "not-authorized"));
62                                 end
63                         end
64                         return true;
65                 end);