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