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 secure_auth_only and not session.secure then
23 -- Sorry, not offering to insecure streams!
25 elseif not session.username then
26 features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
30 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth",
31 function (session, stanza)
32 if secure_auth_only and not session.secure then
33 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
37 local username = stanza.tags[1]:child_with_name("username");
38 local password = stanza.tags[1]:child_with_name("password");
39 local resource = stanza.tags[1]:child_with_name("resource");
40 if not (username and password and resource) then
41 local reply = st.reply(stanza);
42 session.send(reply:query("jabber:iq:auth")
45 :tag("resource"):up());
48 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
49 local reply = st.reply(stanza);
50 require "core.usermanager"
51 if usermanager.validate_credentials(session.host, username, password) then
52 -- Authentication successful!
53 local success, err = sessionmanager.make_authenticated(session, username);
55 local err_type, err_msg;
56 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
58 session.send(st.error_reply(stanza, err_type, err, err_msg));
62 session.send(st.reply(stanza));
65 local reply = st.reply(stanza);
66 reply.attr.type = "error";
67 reply:tag("error", { code = "401", type = "auth" })
68 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });