1f1789a9f0caf7c0c89e65d6b1650d1ff8f0e56f
[prosody.git] / plugins / mod_legacyauth.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local st = require "util.stanza";
23 local t_concat = table.concat;
24
25 require "core.discomanager".set("legacyauth", "jabber:iq:auth");
26
27 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
28                 function (session, stanza)
29                         local username = stanza.tags[1]:child_with_name("username");
30                         local password = stanza.tags[1]:child_with_name("password");
31                         local resource = stanza.tags[1]:child_with_name("resource");
32                         if not (username and password and resource) then
33                                 local reply = st.reply(stanza);
34                                 session.send(reply:query("jabber:iq:auth")
35                                         :tag("username"):up()
36                                         :tag("password"):up()
37                                         :tag("resource"):up());
38                                 return true;                    
39                         else
40                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
41                                 local reply = st.reply(stanza);
42                                 require "core.usermanager"
43                                 if usermanager.validate_credentials(session.host, username, password) then
44                                         -- Authentication successful!
45                                         local success, err = sessionmanager.make_authenticated(session, username);
46                                         if success then
47                                                 local err_type, err_msg;
48                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
49                                                 if not success then
50                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
51                                                         return true;
52                                                 end
53                                         end
54                                         session.send(st.reply(stanza));
55                                         return true;
56                                 else
57                                         local reply = st.reply(stanza);
58                                         reply.attr.type = "error";
59                                         reply:tag("error", { code = "401", type = "auth" })
60                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
61                                         session.send(reply);
62                                         return true;
63                                 end
64                         end
65                         
66                 end);