This patch adds configuration options `password_sql_query` and
`usernames_sql_query` which can be used for
setting arbitrary SQL query to retrieve users list or password instead
of the hard coded values.
The new configuration options are expected to be string with an SQL
query. The options support
special sequences %h and %u which are substituted with host name and
user name respectively
in the query before execution.

-- 
You received this message because you are subscribed to the Google Groups 
"prosody-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/prosody-dev.
For more options, visit https://groups.google.com/d/optout.
# HG changeset patch
# User [email protected]
# Date 1517297191 -7200
#      Tue Jan 30 09:26:31 2018 +0200
# Node ID b946687faa5ed91a87cb95b3241e9f4b924f6c94
# Parent  6f3859233515cbc48eb04cb152a17ef5e77f4ae4
mod_auth_sql: Support for setting SQL queries from the configuration file

This patch adds configuration options `password_sql_query` and `usernames_sql_query` which can be used for
setting arbitrary SQL query to retrieve users list or password instead of the hard coded values.
The new configuration options are expected to be string with an SQL query. The options support
special sequences %h and %u which are substituted with host name and user name respectively
in the query before execution.

diff -r 6f3859233515 -r b946687faa5e mod_auth_sql/mod_auth_sql.lua
--- a/mod_auth_sql/mod_auth_sql.lua	Fri Jan 26 19:00:22 2018 +0100
+++ b/mod_auth_sql/mod_auth_sql.lua	Tue Jan 30 09:26:31 2018 +0200
@@ -70,8 +70,25 @@
 	return stmt;
 end
 
+local function parse_sql_query_config(query, username)
+	local arguments = {};
+	local pattern = "%%[hu]";
+	for mark in query:gmatch(pattern) do
+		if mark == "%h" then
+			table.insert(arguments, module.host);
+		elseif mark == "%u" then
+			table.insert(arguments, username);
+		end
+		query = query:gsub(pattern, "?");
+	end
+
+	return query, arguments
+end
+
 local function get_password(username)
-	local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?", username, module.host);
+	local query = params.password_sql_query or "SELECT `password` FROM `authreg` WHERE `username`=%u AND `realm`=%h";
+	query, arguments = parse_sql_query_config(query, username);
+	local stmt, err = getsql(query, unpack(arguments));
 	if stmt then
 		for row in stmt:rows(true) do
 			return row.password;
@@ -109,7 +126,9 @@
 end
 
 function provider.users()
-	local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `realm`=?", module.host);
+	local query = params.usernames_sql_query or "SELECT `username` FROM `authreg` WHERE `realm`=%h";
+	query, arguments = parse_sql_query_config(query, "");
+	local stmt, err = getsql(query, unpack(arguments));
 	if stmt then
 		local next, state = stmt:rows(true)
 		return function()

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to