2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local st = require "util.stanza";
12 local t_concat = table.concat;
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
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")
29 :tag("resource"):up());
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);
39 local err_type, err_msg;
40 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
42 session.send(st.error_reply(stanza, err_type, err, err_msg));
46 session.send(st.reply(stanza));
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" });