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