util.sasl.plain: Typo.
[prosody.git] / util / sasl / plain.lua
index d9fdb9a2f9ff1f5b25bba685fa3a4227301f5551..3982118299ac562ab2ccb5b02fe306aee4dd6992 100644 (file)
@@ -1,5 +1,5 @@
 -- sasl.lua v0.4
--- Copyright (C) 2008-2009 Tobias Markmann
+-- Copyright (C) 2008-2010 Tobias Markmann
 --
 --    All rights reserved.
 --
@@ -17,24 +17,41 @@ local log = require "util.logger".init("sasl");
 
 module "plain"
 
---=========================
---SASL PLAIN according to RFC 4616
+-- ================================
+-- SASL PLAIN according to RFC 4616
+
+--[[
+Supported Authentication Backends
+
+plain:
+       function(username, realm)
+               return password, state;
+       end
+
+plain_test:
+       function(username, realm, password)
+               return true or false, state;
+       end
+]]
+
 local function plain(self, message)
-       local response = message
-       local authorization = s_match(response, "([^%z]+)")
-       local authentication = s_match(response, "%z([^%z]+)%z")
-       local password = s_match(response, "%z[^%z]+%z([^%z]+)")
+       if not message then
+               return "failure", "malformed-request";
+       end
+
+       local authorization, authentication, password = s_match(message, "^([^%z]*)%z([^%z]+)%z([^%z]+)");
 
-       if authentication == nil or password == nil then
+       if not authorization then
                return "failure", "malformed-request";
        end
-       
+
        -- SASLprep password and authentication
        authentication = saslprep(authentication);
        password = saslprep(password);
-       
+
        if (not password) or (password == "") or (not authentication) or (authentication == "") then
-               log("debug", "Username or password violates either SASLprep.");
+               log("debug", "Username or password violates SASLprep.");
+               return "failure", "malformed-request", "Invalid username or password.";
        end
 
        local correct, state = false, false;
@@ -54,7 +71,7 @@ local function plain(self, message)
        if correct then
                return "success";
        else
-               return "failure", "not-authorized";
+               return "failure", "not-authorized", "Unable to authorize you with the authentication credentials you've sent.";
        end
 end
 
@@ -62,4 +79,4 @@ function init(registerMechanism)
        registerMechanism("PLAIN", {"plain", "plain_test"}, plain);
 end
 
-return _M;
\ No newline at end of file
+return _M;