util/sasl.lua
author Matthew Wild <mwild1@gmail.com>
Thu, 02 Oct 2008 01:08:58 +0100
changeset 38 3fdfd6e0cb4e
parent 32 a4de5ab077ab
child 50 56272224ca4c
permissions -rw-r--r--
SASL! (but before you get too excited, no resource binding yet. And yes, there are still plenty of rough edges to the code...) ((eg. must move <stream:features> out of xmlhandlers.lua o_O ))
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
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     2
local base64 = require "base64"
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     3
local log = require "util.logger".init("sasl");
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     4
local tostring = tostring;
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     5
local st = require "util.stanza";
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     6
local s_match = string.match;
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     7
module "sasl"
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     8
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
     9
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    10
local function new_plain(onAuth, onSuccess, onFail, onWrite)
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    11
	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
    12
	 				onWrite = onWrite}
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    13
	--local challenge = base64.encode("");
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    14
	--onWrite(st.stanza("challenge", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):text(challenge))
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    15
	object.feed = 	function(self, stanza)
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    16
						if stanza.name ~= "response" and stanza.name ~= "auth" then self.onFail("invalid-stanza-tag") end
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    17
						if stanza.attr.xmlns ~= "urn:ietf:params:xml:ns:xmpp-sasl" then self.onFail("invalid-stanza-namespace") end
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    18
						local response = base64.decode(stanza[1])
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    19
						local authorization = s_match(response, "([^&%z]+)")
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    20
						local authentication = s_match(response, "%z([^&%z]+)%z")
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    21
						local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    22
						if self.onAuth(authorization, password) == true then
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    23
							self.onWrite(st.stanza("success", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}))
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    24
							self.onSuccess(authentication)
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    25
						else
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    26
							self.onWrite(st.stanza("failure", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):tag("temporary-auth-failure"));
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    27
						end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    28
					end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    29
	return object
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
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    32
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    33
function new(mechanism, onAuth, onSuccess, onFail, onWrite)
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    34
	local object
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    35
	if mechanism == "PLAIN" then object = new_plain(onAuth, onSuccess, onFail, onWrite)
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    36
	else
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    37
		log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    38
		onFail("unsupported-mechanism")
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    39
	end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    40
	return object
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    41
end
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
    42
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
    43
return _M;