Software /
code /
verse
Annotate
plugins/sasl.lua @ 230:44a6da432e7e
doc/example_pubsub.lua: Example plugins.pubsub usage
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 06 Nov 2011 21:09:07 +0100 |
parent | 197:7e98cf2c1d8d |
child | 302:0c83cb476246 |
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]; | |
27 stream:event("authentication-failure", { condition = err.name }); | |
28 end | |
29 stream:reopen(); | |
30 return true; | |
31 end | |
32 | |
33 stream:hook("stream-features", handle_features, 300); | |
34 stream:hook("stream/"..xmlns_sasl, handle_sasl); | |
35 | |
36 return true; | |
37 end | |
38 |