8
|
1 local st = require "util.stanza";
|
|
2 local base64 = require "mime".b64;
|
|
3 local xmlns_sasl = "urn:ietf:params:xml:ns:xmpp-sasl";
|
|
4
|
|
5 function verse.plugins.sasl(stream)
|
|
6 local function handle_features(features_stanza)
|
|
7 if stream.authenticated then return; end
|
|
8 stream:debug("Authenticating with SASL...");
|
|
9 local initial_data = base64("\0"..stream.username.."\0"..stream.password);
|
|
10
|
|
11 --stream.sasl_state, initial_data = sasl_new({"PLAIN"}, stream.username, stream.password, stream.jid);
|
|
12
|
|
13 stream:debug("Selecting PLAIN mechanism...");
|
|
14 local auth_stanza = st.stanza("auth", { xmlns = xmlns_sasl, mechanism = "PLAIN" });
|
|
15 if initial_data then
|
|
16 auth_stanza:text(initial_data);
|
|
17 end
|
|
18 stream:send(auth_stanza);
|
|
19 return true;
|
|
20 end
|
|
21
|
|
22 local function handle_sasl(sasl_stanza)
|
|
23 if sasl_stanza.name == "success" then
|
|
24 stream.authenticated = true;
|
|
25 stream:event("authentication-success");
|
|
26 elseif sasl_stanza.name == "failure" then
|
|
27 local err = sasl_stanza.tags[1];
|
|
28 stream:event("authentication-failure", { condition = err.name });
|
|
29 end
|
|
30 stream:reopen();
|
|
31 return true;
|
|
32 end
|
|
33
|
|
34 stream:hook("stream-features", handle_features, 300);
|
|
35 stream:hook("stream/"..xmlns_sasl, handle_sasl);
|
|
36
|
|
37 return true;
|
|
38 end
|
|
39
|