Annotate

plugins/mod_saslauth.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
child 46:d6b3f9dbb624
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "util.stanza";
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local send = require "core.sessionmanager".send_to_session;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local t_concat = table.concat;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local tostring = tostring;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local log = require "util.logger".init("mod_saslauth");
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local new_connhandler = require "net.connhandlers".new;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local new_sasl = require "util.sasl".new;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 add_handler("c2s_unauthed", "auth",
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 function (session, stanza)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if not session.sasl_handler then
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 session.sasl_handler = new_sasl(stanza.attr.mechanism,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function (username, password)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 -- onAuth
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 require "core.usermanager"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if usermanager_validate_credentials(session.host, username, password) then
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return true;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return false;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function (username)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- onSuccess
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local success, err = sessionmanager.make_authenticated(session, username);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if not success then
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 sessionmanager.destroy_session(session);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 session.sasl_handler = nil;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 session.connhandler = new_connhandler("xmpp-client", session);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 session.notopen = true;
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 function (reason)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- onFail
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 log("debug", "SASL failure, reason: %s", reason);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end,
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 function (stanza)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 -- onWrite
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 log("debug", "SASL writes: %s", tostring(stanza));
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 send(session, stanza);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 );
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 session.sasl_handler:feed(stanza);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 else
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 error("Client tried to negotiate SASL again", 0);
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end);