Comparison

util/sasl.lua @ 38:3fdfd6e0cb4e

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 ))
author Matthew Wild <mwild1@gmail.com>
date Thu, 02 Oct 2008 01:08:58 +0100
parent 32:a4de5ab077ab
child 50:56272224ca4c
comparison
equal deleted inserted replaced
37:06eadafafefa 38:3fdfd6e0cb4e
1 require "base64"
2 sasl = {}
3 1
4 function sasl:new_plain(onAuth, onSuccess, onFail, onWrite) 2 local base64 = require "base64"
3 local log = require "util.logger".init("sasl");
4 local tostring = tostring;
5 local st = require "util.stanza";
6 local s_match = string.match;
7 module "sasl"
8
9
10 local function new_plain(onAuth, onSuccess, onFail, onWrite)
5 local object = { mechanism = "PLAIN", onAuth = onAuth, onSuccess = onSuccess, onFail = onFail, 11 local object = { mechanism = "PLAIN", onAuth = onAuth, onSuccess = onSuccess, onFail = onFail,
6 onWrite = onWrite} 12 onWrite = onWrite}
7 local challenge = base64.encode(""); 13 --local challenge = base64.encode("");
8 onWrite(stanza.stanza("challenge", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):text(challenge)) 14 --onWrite(st.stanza("challenge", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):text(challenge))
9 object.feed = function(self, stanza) 15 object.feed = function(self, stanza)
10 if (stanza.name ~= "response") then self.onFail() end 16 if stanza.name ~= "response" and stanza.name ~= "auth" then self.onFail("invalid-stanza-tag") end
11 if (stanza.attr.xmlns ~= "urn:ietf:params:xml:ns:xmpp-sasl") then self.onFail() end 17 if stanza.attr.xmlns ~= "urn:ietf:params:xml:ns:xmpp-sasl" then self.onFail("invalid-stanza-namespace") end
12 local response = base64.decode(stanza.tag[1]) 18 local response = base64.decode(stanza[1])
13 local authorization = string.match(response, "([^&\0]+)") 19 local authorization = s_match(response, "([^&%z]+)")
14 local authentication = string.match(response, "\0([^&\0]+)\0") 20 local authentication = s_match(response, "%z([^&%z]+)%z")
15 local password = string.match(response, "\0[^&\0]+\0([^&\0]+)") 21 local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
16 if self.onAuth(authorization, password) == true then 22 if self.onAuth(authorization, password) == true then
17 self.onWrite(stanza.stanza("success", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"})) 23 self.onWrite(st.stanza("success", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}))
18 self.onSuccess() 24 self.onSuccess(authentication)
19 else 25 else
20 self.onWrite(stanza.stanza("failure", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):tag("temporary-auth-failure")); 26 self.onWrite(st.stanza("failure", {xmlns = "urn:ietf:params:xml:ns:xmpp-sasl"}):tag("temporary-auth-failure"));
21 end 27 end
22 end 28 end
23 return object 29 return object
24 end 30 end
25 31
26 function sasl:new(mechanism, onAuth, onSuccess, onFail, onWrite) 32
33 function new(mechanism, onAuth, onSuccess, onFail, onWrite)
27 local object 34 local object
28 if mechanism == "PLAIN" then object = new_plain(onAuth, onSuccess, onFail, onWrite) 35 if mechanism == "PLAIN" then object = new_plain(onAuth, onSuccess, onFail, onWrite)
29 else onFail() 36 else
37 log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));
38 onFail("unsupported-mechanism")
30 end 39 end
31 return object 40 return object
32 end 41 end
33 42
34 module "sasl" 43 return _M;