plugins/mod_legacyauth.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 01 May 2013 13:54:44 +0100
branchtls
changeset 5556 7407b1160b46
parent 45 363c0af290bc
child 154 1fee9396ca2f
permissions -rw-r--r--
Close 'tls' branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local st = require "util.stanza";
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local send = require "core.sessionmanager".send_to_session;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local t_concat = table.concat;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
		function (session, stanza)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
			local username = stanza.tags[1]:child_with_name("username");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
			local password = stanza.tags[1]:child_with_name("password");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
			local resource = stanza.tags[1]:child_with_name("resource");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
			if not (username and password and resource) then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
				local reply = st.reply(stanza);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
				send(session, reply:query("jabber:iq:auth")
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
					:tag("username"):up()
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
					:tag("password"):up()
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
					:tag("resource"):up());
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
				return true;			
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
			else
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
				username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
				local reply = st.reply(stanza);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
				require "core.usermanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
				if usermanager.validate_credentials(session.host, username, password) then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
					-- Authentication successful!
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    24
					local success, err = sessionmanager.make_authenticated(session, username);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    25
					if success then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    26
						success, err = sessionmanager.bind_resource(session, resource);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    27
						--FIXME: Reply with error
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    28
						if not success then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    29
							local reply = st.reply(stanza);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    30
							reply.attr.type = "error";
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    31
							if err == "conflict" then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    32
								reply:tag("error", { code = "409", type = "cancel" })
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    33
									:tag("conflict", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    34
							elseif err == "constraint" then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    35
								reply:tag("error", { code = "409", type = "cancel" })
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    36
									:tag("already-bound", { xmlns = "x-lxmppd:extensions:legacyauth" });
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    37
							elseif err == "auth" then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    38
								reply:tag("error", { code = "401", type = "auth" })
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    39
									:tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    40
							end
45
363c0af290bc Small fix for sending stanzas in case of resource binding error
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
    41
							send(session, reply);
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    42
							return true;
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    43
						end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
					end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
					send(session, st.reply(stanza));
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
					return true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
				else
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
					local reply = st.reply(stanza);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
					reply.attr.type = "error";
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
					reply:tag("error", { code = "401", type = "auth" })
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
						:tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
					dispatch_stanza(reply);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
					return true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
				end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		end);