plugins/mod_legacyauth.lua
changeset 30 bcf539295f2d
child 38 3fdfd6e0cb4e
equal deleted inserted replaced
29:b847875801e5 30:bcf539295f2d
       
     1 
       
     2 local st = require "util.stanza";
       
     3 local send = require "core.sessionmanager".send_to_session;
       
     4 local t_concat = table.concat;
       
     5 
       
     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")
       
    14 					:tag("username"):up()
       
    15 					:tag("password"):up()
       
    16 					:tag("resource"):up());
       
    17 				return true;			
       
    18 			else
       
    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 					session.username = username;
       
    25 					session.resource = resource;
       
    26 					session.full_jid = username.."@"..session.host.."/"..session.resource;
       
    27 					if session.type == "c2s_unauthed" then
       
    28 						session.type = "c2s";
       
    29 					end
       
    30 					if not hosts[session.host].sessions[username] then
       
    31 						hosts[session.host].sessions[username] = { sessions = {} };
       
    32 					end
       
    33 					hosts[session.host].sessions[username].sessions[resource] = session;
       
    34 					send(session, st.reply(stanza));
       
    35 					return true;
       
    36 				else
       
    37 					local reply = st.reply(stanza);
       
    38 					reply.attr.type = "error";
       
    39 					reply:tag("error", { code = "401", type = "auth" })
       
    40 						:tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
       
    41 					dispatch_stanza(reply);
       
    42 					return true;
       
    43 				end
       
    44 			end
       
    45 			
       
    46 		end);