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 config = require "core.configmanager";
15 local secure_auth_only = config.get(module:get_host(), "core", "require_encryption");
17 local sessionmanager = require "core.sessionmanager";
18 local usermanager = require "core.usermanager";
20 module:add_feature("jabber:iq:auth");
21 module:add_event_hook("stream-features", function (session, features)
22 if not session.username then features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up(); end
25 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth",
26 function (session, stanza)
27 if secure_auth_only and not session.secure then
28 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
32 local username = stanza.tags[1]:child_with_name("username");
33 local password = stanza.tags[1]:child_with_name("password");
34 local resource = stanza.tags[1]:child_with_name("resource");
35 if not (username and password and resource) then
36 local reply = st.reply(stanza);
37 session.send(reply:query("jabber:iq:auth")
40 :tag("resource"):up());
43 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
44 local reply = st.reply(stanza);
45 require "core.usermanager"
46 if usermanager.validate_credentials(session.host, username, password) then
47 -- Authentication successful!
48 local success, err = sessionmanager.make_authenticated(session, username);
50 local err_type, err_msg;
51 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
53 session.send(st.error_reply(stanza, err_type, err, err_msg));
57 session.send(st.reply(stanza));
60 local reply = st.reply(stanza);
61 reply.attr.type = "error";
62 reply:tag("error", { code = "401", type = "auth" })
63 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });