# HG changeset patch
# User Tobias Markmann <tm@ayena.de>
# Date 1219705066 -7200
# Node ID c0d754774db29e3b10f29b8fda5d68f6586a379f
# Parent  90f22275f7aedcc7524f1c7563d2c5b54d2cdf85
adding SASL lib with PLAIN support, not tested yet

diff -r 90f22275f7ae -r c0d754774db2 util/sasl.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/sasl.lua	Tue Aug 26 00:57:46 2008 +0200
@@ -0,0 +1,33 @@
+require "base64"
+
+function sasl:new_plain(onAuth, onSuccess, onFail, onWrite)
+	local object = { mechanism = "PLAIN", onAuth = onAuth, onSuccess = onSuccess, onFail = onFail,
+	 				onWrite = onWrite}
+	local challenge = base64.encode("");
+	onWrite(stanza.stanza("challenge", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):text(challenge))
+	object.feed = 	function(self, stanza)
+						if (stanza.name ~= "response") then self.onFail() end
+						if (stanza.attr.xmlns ~= "urn:ietf:params:xml:ns:xmpp-sasl") then self.onFail() end
+						local response = stanza.tag[1]
+						local authorization = string.match(response, [[([^&\0]+)]])
+						local authentication = string.match(response, [[\0([^&\0]+)\0]])
+						local password = string.match(response, [[\0[^&\0]+\0([^&\0]+)]])
+						if self.onAuth(authorization, password) == true then
+							self.onWrite(stanza.stanza("success", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}))
+							self.onSuccess()
+						else
+							self.onWrite(stanza.stanza("failure", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):tag("temporary-auth-failure"));
+						end
+					end
+	return object
+end
+
+function sasl:new(mechanism, onAuth, onSuccess, onFail, onWrite)
+	local object
+	if mechanism == "PLAIN" then object = new_plain(onAuth, onSuccess, onFail, onWrite)
+	else onFail()
+	end
+	return object
+end
+
+module "sasl"