util/sasl.lua

Sun, 05 Oct 2008 19:10:21 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 05 Oct 2008 19:10:21 +0100
branch
tls
changeset 66
018705d57f09
parent 50
56272224ca4c
child 276
30893439d5d1
permissions
-rw-r--r--

Working TLS!

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]+)")
50
56272224ca4c Fix for using wrong auth token as username (fixes Gajim login)
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
22 if self.onAuth(authentication, 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;

mercurial