8ac10bd4cbe38066bb39a67aed0b859236c2f3ba
[prosody.git] / plugins / mod_legacyauth.lua
1
2 local st = require "util.stanza";
3 local send = require "core.sessionmanager".send_to_session;
4 local t_concat = table.concat;
5
6 add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
7                 function (session, stanza)
8                         local username = stanza.tags[1]:child_with_name("username");
9                         local password = stanza.tags[1]:child_with_name("password");
10                         local resource = stanza.tags[1]:child_with_name("resource");
11                         if not (username and password and resource) then
12                                 local reply = st.reply(stanza);
13                                 send(session, reply:query("jabber:iq:auth")
14                                         :tag("username"):up()
15                                         :tag("password"):up()
16                                         :tag("resource"):up());
17                                 return true;                    
18                         else
19                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
20                                 local reply = st.reply(stanza);
21                                 require "core.usermanager"
22                                 if usermanager.validate_credentials(session.host, username, password) then
23                                         -- Authentication successful!
24                                         local success, err = sessionmanager.make_authenticated(session, username);
25                                         if success then
26                                                 success, err = sessionmanager.bind_resource(session, resource);
27                                                 --FIXME: Reply with error
28                                                 if not success then
29                                                         local reply = st.reply(stanza);
30                                                         reply.attr.type = "error";
31                                                         if err == "conflict" then
32                                                                 reply:tag("error", { code = "409", type = "cancel" })
33                                                                         :tag("conflict", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
34                                                         elseif err == "constraint" then
35                                                                 reply:tag("error", { code = "409", type = "cancel" })
36                                                                         :tag("already-bound", { xmlns = "x-lxmppd:extensions:legacyauth" });
37                                                         elseif err == "auth" then
38                                                                 reply:tag("error", { code = "401", type = "auth" })
39                                                                         :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
40                                                         end
41                                                         send(session, reply);
42                                                         return true;
43                                                 end
44                                         end
45                                         send(session, st.reply(stanza));
46                                         return true;
47                                 else
48                                         local reply = st.reply(stanza);
49                                         reply.attr.type = "error";
50                                         reply:tag("error", { code = "401", type = "auth" })
51                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
52                                         dispatch_stanza(reply);
53                                         return true;
54                                 end
55                         end
56                         
57                 end);