2 local st = require "util.stanza";
3 local send = require "core.sessionmanager".send_to_session;
4 local t_concat = table.concat;
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")
16 :tag("resource"):up());
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);
26 success, err = sessionmanager.bind_resource(session, resource);
27 --FIXME: Reply with error
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" });
45 send(session, st.reply(stanza));
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" });