Software /
code /
verse
Annotate
plugins/sasl.lua @ 348:34b878d58948
plugins.pubsub: Implement fetching of items
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 06 Jul 2013 08:40:18 +0200 |
parent | 315:3742107e2505 |
child | 354:58cd27b74ba5 |
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 --stream.sasl_state, initial_data = sasl_new({"PLAIN"}, stream.username, stream.password, stream.jid); | |
315
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
9 local mechanism , initial_data |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
10 if stream.username then |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
11 mechanism = "PLAIN" |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
12 initial_data = base64("\0"..stream.username.."\0"..stream.password); |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
13 else |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
14 mechanism = "ANONYMOUS" |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
15 end |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
16 stream:debug("Selecting %s mechanism...",mechanism); |
3742107e2505
plugins/sasl: Use ANONYMOUS authentication when no username provided
James Callahan <james@chatid.com>
parents:
302
diff
changeset
|
17 local auth_stanza = verse.stanza("auth", { xmlns = xmlns_sasl, mechanism = mechanism }); |
8 | 18 if initial_data then |
19 auth_stanza:text(initial_data); | |
20 end | |
21 stream:send(auth_stanza); | |
22 return true; | |
23 end | |
24 | |
25 local function handle_sasl(sasl_stanza) | |
26 if sasl_stanza.name == "success" then | |
27 stream.authenticated = true; | |
28 stream:event("authentication-success"); | |
29 elseif sasl_stanza.name == "failure" then | |
30 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
|
31 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
|
32 stream:event("authentication-failure", { condition = err.name, text = text }); |
8 | 33 end |
34 stream:reopen(); | |
35 return true; | |
36 end | |
37 | |
38 stream:hook("stream-features", handle_features, 300); | |
39 stream:hook("stream/"..xmlns_sasl, handle_sasl); | |
40 | |
41 return true; | |
42 end | |
43 |