util/sasl.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 30 Sep 2008 21:35:39 +0100
changeset 32 a4de5ab077ab
parent 17 9a2685f39f9f
child 38 3fdfd6e0cb4e
permissions -rw-r--r--
Fix quoting in util/sasl.lua
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     1
require "base64"
17
9a2685f39f9f module table was missing
Tobias Markmann <tm@ayena.de>
parents: 16
diff changeset
     2
sasl = {}
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     3
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     4
function sasl:new_plain(onAuth, onSuccess, onFail, onWrite)
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     5
	local object = { mechanism = "PLAIN", onAuth = onAuth, onSuccess = onSuccess, onFail = onFail,
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     6
	 				onWrite = onWrite}
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     7
	local challenge = base64.encode("");
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     8
	onWrite(stanza.stanza("challenge", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):text(challenge))
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
     9
	object.feed = 	function(self, stanza)
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    10
						if (stanza.name ~= "response") then self.onFail() end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    11
						if (stanza.attr.xmlns ~= "urn:ietf:params:xml:ns:xmpp-sasl") then self.onFail() end
16
ba8e796f9f94 * missing base64 decode of SASL response
Tobias Markmann <tm@ayena.de>
parents: 15
diff changeset
    12
						local response = base64.decode(stanza.tag[1])
32
a4de5ab077ab Fix quoting in util/sasl.lua
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
    13
						local authorization = string.match(response, "([^&\0]+)")
a4de5ab077ab Fix quoting in util/sasl.lua
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
    14
						local authentication = string.match(response, "\0([^&\0]+)\0")
a4de5ab077ab Fix quoting in util/sasl.lua
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
    15
						local password = string.match(response, "\0[^&\0]+\0([^&\0]+)")
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    16
						if self.onAuth(authorization, password) == true then
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    17
							self.onWrite(stanza.stanza("success", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}))
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    18
							self.onSuccess()
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    19
						else
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    20
							self.onWrite(stanza.stanza("failure", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):tag("temporary-auth-failure"));
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    21
						end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    22
					end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    23
	return object
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    24
end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    25
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    26
function sasl:new(mechanism, onAuth, onSuccess, onFail, onWrite)
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    27
	local object
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    28
	if mechanism == "PLAIN" then object = new_plain(onAuth, onSuccess, onFail, onWrite)
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    29
	else onFail()
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    30
	end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    31
	return object
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    32
end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    33
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    34
module "sasl"