Merge with 0.4
[prosody.git] / util / sasl.lua
index 311c3aff24539ba343c8ad9d8f18f5a578626d6c..2740b4273359ee34137941ab1765614e87d6568f 100644 (file)
@@ -1,5 +1,5 @@
--- sasl.lua v0.1
--- Copyright (C) 2008 Tobias Markmann
+-- sasl.lua v0.4
+-- Copyright (C) 2008-2009 Tobias Markmann
 -- 
 --    All rights reserved.
 --    
@@ -176,13 +176,13 @@ local function new_digest_md5(realm, password_handler)
                        if not response["cnonce"] then return "failure", "malformed-request", "Missing entry for cnonce in SASL message." end
                        if not response["qop"] then response["qop"] = "auth" end
                        
-                       if response["realm"] == nil then
-                               response["realm"] = ""
+                       if response["realm"] == nil or response["realm"] == "" then
+                               response["realm"] = self.realm;
                        elseif response["realm"] ~= self.realm then
                                return "failure", "not-authorized", "Incorrect realm value";
                        end
-                       local decoder;
                        
+                       local decoder;
                        if response["charset"] == nil then
                                decoder = utf8tolatin1ifpossible;
                        elseif response["charset"] ~= "utf-8" then
@@ -235,10 +235,22 @@ local function new_digest_md5(realm, password_handler)
        return object
 end
 
+local function new_anonymous(realm, password_handler)
+       local object = { mechanism = "ANONYMOUS", realm = realm, password_handler = password_handler}
+               function object.feed(self, message)
+                       return "success"
+               end
+       --TODO: From XEP-0175 "It is RECOMMENDED for the node identifier to be a UUID as specified in RFC 4122 [5]." So util.uuid() should (or have an option to) behave as specified in RFC 4122.
+       object["username"] = generate_uuid()
+       return object
+end
+
+
 function new(mechanism, realm, password_handler)
        local object
        if mechanism == "PLAIN" then object = new_plain(realm, password_handler)
        elseif mechanism == "DIGEST-MD5" then object = new_digest_md5(realm, password_handler)
+       elseif mechanism == "ANONYMOUS" then object = new_anonymous(realm, password_handler)
        else
                log("debug", "Unsupported SASL mechanism: "..tostring(mechanism));
                return nil