Comparison

util/sasl.lua @ 15:c0d754774db2

adding SASL lib with PLAIN support, not tested yet
author Tobias Markmann <tm@ayena.de>
date Tue, 26 Aug 2008 00:57:46 +0200
child 16:ba8e796f9f94
comparison
equal deleted inserted replaced
12:90f22275f7ae 15:c0d754774db2
1 require "base64"
2
3 function sasl:new_plain(onAuth, onSuccess, onFail, onWrite)
4 local object = { mechanism = "PLAIN", onAuth = onAuth, onSuccess = onSuccess, onFail = onFail,
5 onWrite = onWrite}
6 local challenge = base64.encode("");
7 onWrite(stanza.stanza("challenge", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):text(challenge))
8 object.feed = function(self, stanza)
9 if (stanza.name ~= "response") then self.onFail() end
10 if (stanza.attr.xmlns ~= "urn:ietf:params:xml:ns:xmpp-sasl") then self.onFail() end
11 local response = stanza.tag[1]
12 local authorization = string.match(response, [[([^&\0]+)]])
13 local authentication = string.match(response, [[\0([^&\0]+)\0]])
14 local password = string.match(response, [[\0[^&\0]+\0([^&\0]+)]])
15 if self.onAuth(authorization, password) == true then
16 self.onWrite(stanza.stanza("success", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}))
17 self.onSuccess()
18 else
19 self.onWrite(stanza.stanza("failure", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):tag("temporary-auth-failure"));
20 end
21 end
22 return object
23 end
24
25 function sasl:new(mechanism, onAuth, onSuccess, onFail, onWrite)
26 local object
27 if mechanism == "PLAIN" then object = new_plain(onAuth, onSuccess, onFail, onWrite)
28 else onFail()
29 end
30 return object
31 end
32
33 module "sasl"