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