-local function new_plain(realm, password_handler)
- local object = { mechanism = "PLAIN", realm = realm, password_handler = password_handler}
- object.feed = function(self, message)
- --print(message:gsub("%W", function (c) return string.format("\\%d", string.byte(c)) end));
-
- if message == "" or message == nil then return "failure", "malformed-request" end
- 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]+)")
-
- local password_encoding, correct_password = self.password_handler(authentication, self.realm, "PLAIN")
-
- local claimed_password = ""
- if password_encoding == nil then claimed_password = password
- else claimed_password = password_encoding(password) end
-
- self.username = authentication
- if claimed_password == correct_password then
- log("debug", "success")
- return "success"
- else
- log("debug", "failure")
- return "failure", "not-authorized"
- end
- end
- return object
+-- Credentials handler:
+-- Arguments: ("PLAIN", user, host, password)
+-- Returns: true (success) | false (fail) | nil (user unknown)
+local function new_plain(realm, credentials_handler)
+ local object = { mechanism = "PLAIN", realm = realm, credentials_handler = credentials_handler}
+ function object.feed(self, message)
+ if message == "" or message == nil then return "failure", "malformed-request" end
+ 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 authentication == nil or password == nil then return "failure", "malformed-request" end
+ self.username = authentication
+ local auth_success = self.credentials_handler("PLAIN", self.username, self.realm, password)
+
+ if auth_success then
+ return "success"
+ elseif auth_success == nil then
+ return "failure", "account-disabled"
+ else
+ return "failure", "not-authorized"
+ end
+ end
+ return object