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 local sessionmanager = require "core.sessionmanager";
15 local usermanager = require "core.usermanager";
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
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")
32 :tag("resource"):up());
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);
42 local err_type, err_msg;
43 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
45 session.send(st.error_reply(stanza, err_type, err, err_msg));
49 session.send(st.reply(stanza));
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" });