Fix the reversed to/from on the final db:result. Fixes M-Link and Gmail. Thanks dwd!!
[prosody.git] / plugins / mod_saslauth.lua
index ffeacce34fe2cdcc03674bdac881dcaa1d8b41bd..6ceb0be31fa2dd20198c3e3ae0e7b18ff29b9bdc 100644 (file)
@@ -1,8 +1,8 @@
 
 local st = require "util.stanza";
-local send = require "core.sessionmanager".send_to_session;
 local sm_bind_resource = require "core.sessionmanager".bind_resource;
 local jid
+local base64 = require "base64"
 
 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
 local t_concat, t_insert = table.concat, table.insert;
@@ -89,7 +89,7 @@ add_event_hook("stream-features",
                                                        t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
                                                        -- TODO: Provide PLAIN only if TLS is active, this is a SHOULD from the introduction of RFC 4616. This behavior could be overridden via configuration but will issuing a warning or so.
                                                                t_insert(features, "<mechanism>PLAIN</mechanism>");
-                                                               -- t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
+                                                               t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
                                                        t_insert(features, "</mechanisms>");
                                                else
                                                        t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
@@ -104,7 +104,6 @@ add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
                        local resource;
                        if stanza.attr.type == "set" then
                                local bind = stanza.tags[1];
-                               
                                if bind and bind.attr.xmlns == xmlns_bind then
                                        resource = bind:child_with_name("resource");
                                        if resource then
@@ -112,31 +111,18 @@ add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
                                        end
                                end
                        end
-                       local success, err = sm_bind_resource(session, resource);
+                       local success, err_type, err, err_msg = sm_bind_resource(session, resource);
                        if not success then
-                               local reply = st.reply(stanza);
-                               reply.attr.type = "error";
-                               if err == "conflict" then
-                                       reply:tag("error", { type = "modify" })
-                                               :tag("conflict", { xmlns = xmlns_stanzas });
-                               elseif err == "constraint" then
-                                       reply:tag("error", { type = "cancel" })
-                                               :tag("resource-constraint", { xmlns = xmlns_stanzas });
-                               elseif err == "auth" then
-                                       reply:tag("error", { type = "cancel" })
-                                               :tag("not-allowed", { xmlns = xmlns_stanzas });
-                               end
-                               send(session, reply);
+                               session.send(st.error_reply(stanza, err_type, err, err_msg));
                        else
-                               local reply = st.reply(stanza);
-                               reply:tag("bind", { xmlns = xmlns_bind})
-                                       :tag("jid"):text(session.full_jid);
-                               send(session, reply);
+                               session.send(st.reply(stanza)
+                                       :tag("bind", { xmlns = xmlns_bind})
+                                       :tag("jid"):text(session.full_jid));
                        end
                end);
                
 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
                function (session, stanza)
                        log("debug", "Client tried to bind to a resource");
-                       send(session, st.reply(stanza));
+                       session.send(st.reply(stanza));
                end);