From a7012d49236954006b922d2f41ecadf87b105d1f Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Tue, 26 Aug 2008 00:57:46 +0200 Subject: adding SASL lib with PLAIN support, not tested yet --- util/sasl.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 util/sasl.lua diff --git a/util/sasl.lua b/util/sasl.lua new file mode 100644 index 00000000..3f82350b --- /dev/null +++ b/util/sasl.lua @@ -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" -- cgit v1.2.3 From 0b6ee8b38ad4a7bc89abb54df4c485ab2bb944c9 Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Tue, 26 Aug 2008 01:01:13 +0200 Subject: * missing base64 decode of SASL response --- util/sasl.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/sasl.lua b/util/sasl.lua index 3f82350b..6e85c985 100644 --- a/util/sasl.lua +++ b/util/sasl.lua @@ -8,7 +8,7 @@ function sasl:new_plain(onAuth, onSuccess, onFail, onWrite) 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 response = base64.decode(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]+)]]) -- cgit v1.2.3 From 53b44292cbf272619c73a04fd7bfeaa58e32b8be Mon Sep 17 00:00:00 2001 From: Tobias Markmann Date: Tue, 26 Aug 2008 14:11:52 +0200 Subject: module table was missing --- util/sasl.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/util/sasl.lua b/util/sasl.lua index 6e85c985..fb3aff94 100644 --- a/util/sasl.lua +++ b/util/sasl.lua @@ -1,4 +1,5 @@ require "base64" +sasl = {} function sasl:new_plain(onAuth, onSuccess, onFail, onWrite) local object = { mechanism = "PLAIN", onAuth = onAuth, onSuccess = onSuccess, onFail = onFail, -- cgit v1.2.3