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 d57ef3c4e3ca2a97d05a0af961453b0185ba8044 # 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 d57ef3c4e3ca 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])"; + query = query:gsub(pattern, function(mark) + if mark == "%h" then + table.insert(arguments, module.host); + elseif mark == "%u" then + table.insert(arguments, username); + end + return "?"; + 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()
signature.asc
Description: OpenPGP digital signature
