Software /
code /
verse
Annotate
plugins/sasl.lua @ 305:4a0206505b9d
plugins.archive: Don't modify the query params table
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 25 Jun 2012 02:27:43 +0200 |
parent | 302:0c83cb476246 |
child | 315:3742107e2505 |
rev | line source |
---|---|
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 |