Changeset

399:4e0d36941ba1

mod_auth_sql: More cleanup.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 10 Aug 2011 05:30:08 +0500
parents 398:fdd4f5ab029a
children 400:f42fe4229f8a
files mod_auth_sql/mod_auth_sql.lua
diffstat 1 files changed, 14 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/mod_auth_sql/mod_auth_sql.lua	Wed Aug 10 04:42:50 2011 +0500
+++ b/mod_auth_sql/mod_auth_sql.lua	Wed Aug 10 05:30:08 2011 +0500
@@ -1,5 +1,6 @@
 -- Simple SQL Authentication module for Prosody IM
 -- Copyright (C) 2011 Tomasz Sterna <tomek@xiaoka.com>
+-- Copyright (C) 2011 Waqas Hussain <waqas20@gmail.com>
 --
 
 local log = require "util.logger".init("auth_sql");
@@ -70,50 +71,33 @@
 	return stmt;
 end
 
+local function get_password(username)
+	local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?", username, module.host);
+	if stmt then
+		for row in stmt:rows(true) do
+			return row.password;
+		end
+	end
+end
+
 
 provider = { name = "sql" };
 
 function provider.test_password(username, password)
-	local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `password`=? AND `realm`=?",
-		username, password, module.host);
-
-	if not stmt then return nil, err; end
-
-	for row in stmt:rows(true) do
-		return true;
-	end
+	return password and get_password(username) == password;
 end
-
 function provider.get_password(username)
-	local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?",
-		username, module.host);
-
-	if not stmt then return nil, err; end
-
-	for row in stmt:rows(true) do
-		return row.password;
-	end
+	return get_password(username);
 end
-
 function provider.set_password(username, password)
 	return nil, "Setting password is not supported.";
 end
-
 function provider.user_exists(username)
-	local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `realm`=?",
-		username, module.host);
-
-	if not stmt then return nil, err; end
-
-	for row in stmt:rows(true) do
-		return true;
-	end
+	return get_password(username) and true;
 end
-
 function provider.create_user(username, password)
 	return nil, "Account creation/modification not supported.";
 end
-
 function provider.get_sasl_handler()
 	local profile = {
 		plain = function(sasl, username, realm)
@@ -122,7 +106,7 @@
 				module:log("debug", "NODEprep failed on username: %s", username);
 				return "", nil;
 			end
-			local password = provider.get_password(prepped_username);
+			local password = get_password(prepped_username);
 			if not password then return "", nil; end
 			return password, true;
 		end