Mon, 28 May 2012 17:32:38 +0200
plugins.sasl: Collect text message on SASL failure
8 | 1 | local base64 = require "mime".b64; |
2 | local xmlns_sasl = "urn:ietf:params:xml:ns:xmpp-sasl"; | |
3 | ||
4 | function verse.plugins.sasl(stream) | |
5 | local function handle_features(features_stanza) | |
6 | if stream.authenticated then return; end | |
7 | stream:debug("Authenticating with SASL..."); | |
8 | local initial_data = base64("\0"..stream.username.."\0"..stream.password); | |
9 | ||
10 | --stream.sasl_state, initial_data = sasl_new({"PLAIN"}, stream.username, stream.password, stream.jid); | |
11 | ||
12 | stream:debug("Selecting PLAIN mechanism..."); | |
197
7e98cf2c1d8d
plugins.*: Use verse.stanza() & co instead of require util.stanza
Kim Alvefur <zash@zash.se>
parents:
147
diff
changeset
|
13 | local auth_stanza = verse.stanza("auth", { xmlns = xmlns_sasl, mechanism = "PLAIN" }); |
8 | 14 | if initial_data then |
15 | auth_stanza:text(initial_data); | |
16 | end | |
17 | stream:send(auth_stanza); | |
18 | return true; | |
19 | end | |
20 | ||
21 | local function handle_sasl(sasl_stanza) | |
22 | if sasl_stanza.name == "success" then | |
23 | stream.authenticated = true; | |
24 | stream:event("authentication-success"); | |
25 | elseif sasl_stanza.name == "failure" then | |
26 | local err = sasl_stanza.tags[1]; | |
302
0c83cb476246
plugins.sasl: Collect text message on SASL failure
Kim Alvefur <zash@zash.se>
parents:
197
diff
changeset
|
27 | local text = sasl_stanza:get_child_text("text"); |
0c83cb476246
plugins.sasl: Collect text message on SASL failure
Kim Alvefur <zash@zash.se>
parents:
197
diff
changeset
|
28 | stream:event("authentication-failure", { condition = err.name, text = text }); |
8 | 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 |