plugins/mod_legacyauth.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 05 May 2009 18:07:13 +0100
changeset 1128 b2e548344d61
parent 1065 3806173670f2
child 1216 fd8ce71bc72b
permissions -rw-r--r--
util.serialization: Write nil for non-serializable data types, and bump the log level to 'error'
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
896
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 891
diff changeset
     1
-- Prosody IM v0.4
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     9
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local st = require "util.stanza";
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local t_concat = table.concat;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
1065
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    14
local sessionmanager = require "core.sessionmanager";
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    15
local usermanager = require "core.usermanager";
3806173670f2 mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    16
541
3521e0851c9e Change modules to use the new add_feature module API method.
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    17
module:add_feature("jabber:iq:auth");
891
236d1ce9fa99 mod_legacyauth: Added stream feature: <auth xmlns='http://jabber.org/features/iq-auth'/>
Waqas Hussain <waqas20@gmail.com>
parents: 760
diff changeset
    18
module:add_event_hook("stream-features", function (session, features)
236d1ce9fa99 mod_legacyauth: Added stream feature: <auth xmlns='http://jabber.org/features/iq-auth'/>
Waqas Hussain <waqas20@gmail.com>
parents: 760
diff changeset
    19
	if not session.username then features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up(); end
236d1ce9fa99 mod_legacyauth: Added stream feature: <auth xmlns='http://jabber.org/features/iq-auth'/>
Waqas Hussain <waqas20@gmail.com>
parents: 760
diff changeset
    20
end);
421
63be85693710 Modules now sending disco replies
Waqas Hussain <waqas20@gmail.com>
parents: 308
diff changeset
    21
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 421
diff changeset
    22
module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		function (session, stanza)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
			local username = stanza.tags[1]:child_with_name("username");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
			local password = stanza.tags[1]:child_with_name("password");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
			local resource = stanza.tags[1]:child_with_name("resource");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
			if not (username and password and resource) then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
				local reply = st.reply(stanza);
308
6345cf3e994a Fixed mod_legacyauth to use session.send for sending stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 304
diff changeset
    29
				session.send(reply:query("jabber:iq:auth")
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
					:tag("username"):up()
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
					:tag("password"):up()
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
					:tag("resource"):up());
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
				return true;			
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
			else
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
				username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
				local reply = st.reply(stanza);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
				require "core.usermanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
				if usermanager.validate_credentials(session.host, username, password) then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
					-- Authentication successful!
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    40
					local success, err = sessionmanager.make_authenticated(session, username);
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    41
					if success then
304
7b28fa8bbfe5 Code cleanup for resource binding
Waqas Hussain <waqas20@gmail.com>
parents: 154
diff changeset
    42
						local err_type, err_msg;
7b28fa8bbfe5 Code cleanup for resource binding
Waqas Hussain <waqas20@gmail.com>
parents: 154
diff changeset
    43
						success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    44
						if not success then
304
7b28fa8bbfe5 Code cleanup for resource binding
Waqas Hussain <waqas20@gmail.com>
parents: 154
diff changeset
    45
							session.send(st.error_reply(stanza, err_type, err, err_msg));
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    46
							return true;
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    47
						end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
					end
308
6345cf3e994a Fixed mod_legacyauth to use session.send for sending stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 304
diff changeset
    49
					session.send(st.reply(stanza));
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
					return true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
				else
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
					local reply = st.reply(stanza);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
					reply.attr.type = "error";
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
					reply:tag("error", { code = "401", type = "auth" })
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
						:tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
308
6345cf3e994a Fixed mod_legacyauth to use session.send for sending stanzas
Waqas Hussain <waqas20@gmail.com>
parents: 304
diff changeset
    56
					session.send(reply);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
					return true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
				end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
			end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
			
438
193f9dd64f17 Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents: 421
diff changeset
    61
		end);