0.2->0.3
[prosody.git] / plugins / mod_legacyauth.lua
1 -- Prosody IM v0.3
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 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 module:add_feature("jabber:iq:auth");
15
16 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
17                 function (session, stanza)
18                         local username = stanza.tags[1]:child_with_name("username");
19                         local password = stanza.tags[1]:child_with_name("password");
20                         local resource = stanza.tags[1]:child_with_name("resource");
21                         if not (username and password and resource) then
22                                 local reply = st.reply(stanza);
23                                 session.send(reply:query("jabber:iq:auth")
24                                         :tag("username"):up()
25                                         :tag("password"):up()
26                                         :tag("resource"):up());
27                                 return true;                    
28                         else
29                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
30                                 local reply = st.reply(stanza);
31                                 require "core.usermanager"
32                                 if usermanager.validate_credentials(session.host, username, password) then
33                                         -- Authentication successful!
34                                         local success, err = sessionmanager.make_authenticated(session, username);
35                                         if success then
36                                                 local err_type, err_msg;
37                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
38                                                 if not success then
39                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
40                                                         return true;
41                                                 end
42                                         end
43                                         session.send(st.reply(stanza));
44                                         return true;
45                                 else
46                                         local reply = st.reply(stanza);
47                                         reply.attr.type = "error";
48                                         reply:tag("error", { code = "401", type = "auth" })
49                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
50                                         session.send(reply);
51                                         return true;
52                                 end
53                         end
54                         
55                 end);