comparison mod_auth_sql/mod_auth_sql.lua @ 398:fdd4f5ab029a

mod_auth_sql: Cleanup.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 10 Aug 2011 04:42:50 +0500
parents c416db434e5b
children 4e0d36941ba1
comparison
equal deleted inserted replaced
397:7331e2669f24 398:fdd4f5ab029a
3 -- 3 --
4 4
5 local log = require "util.logger".init("auth_sql"); 5 local log = require "util.logger".init("auth_sql");
6 local new_sasl = require "util.sasl".new; 6 local new_sasl = require "util.sasl".new;
7 local nodeprep = require "util.encodings".stringprep.nodeprep; 7 local nodeprep = require "util.encodings".stringprep.nodeprep;
8 local DBI = require "DBI"
8 9
9 local DBI;
10 local connection; 10 local connection;
11 local params = module:get_option("sql"); 11 local params = module:get_option("sql");
12 12
13 local resolve_relative_path = require "core.configmanager".resolve_relative_path; 13 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
14 14
40 return connection; 40 return connection;
41 end 41 end
42 end 42 end
43 43
44 do -- process options to get a db connection 44 do -- process options to get a db connection
45 DBI = require "DBI";
46
47 params = params or { driver = "SQLite3" }; 45 params = params or { driver = "SQLite3" };
48 46
49 if params.driver == "SQLite3" then 47 if params.driver == "SQLite3" then
50 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); 48 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
51 end 49 end
70 if not ok then return nil, err; end 68 if not ok then return nil, err; end
71 69
72 return stmt; 70 return stmt;
73 end 71 end
74 72
75 function new_default_provider(host)
76 local provider = { name = "sql" };
77 module:log("debug", "initializing default authentication provider for host '%s'", host);
78 73
79 function provider.test_password(username, password) 74 provider = { name = "sql" };
80 module:log("debug", "test_password '%s' for user %s at host %s", password, username, host);
81 75
82 local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `password`=? AND `realm`=?", 76 function provider.test_password(username, password)
83 username, password, host); 77 local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `password`=? AND `realm`=?",
78 username, password, module.host);
84 79
85 if stmt ~= nil then 80 if not stmt then return nil, err; end
86 local count = 0;
87 for row in stmt:rows(true) do
88 count = count + 1;
89 end
90 if count > 0 then
91 return true;
92 end
93 else
94 module:log("error", "QUERY ERROR: %s %s", err, debug.traceback());
95 return nil, err;
96 end
97 81
98 return false; 82 for row in stmt:rows(true) do
83 return true;
99 end 84 end
100
101 function provider.get_password(username)
102 module:log("debug", "get_password for username '%s' at host '%s'", username, host);
103
104 local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?",
105 username, host);
106
107 local password = nil;
108 if stmt ~= nil then
109 for row in stmt:rows(true) do
110 password = row.password;
111 end
112 else
113 module:log("error", "QUERY ERROR: %s %s", err, debug.traceback());
114 return nil;
115 end
116
117 return password;
118 end
119
120 function provider.set_password(username, password)
121 return nil, "Setting password is not supported.";
122 end
123
124 function provider.user_exists(username)
125 module:log("debug", "test user %s existence at host %s", username, host);
126
127 local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `realm`=?",
128 username, host);
129
130 if stmt ~= nil then
131 local count = 0;
132 for row in stmt:rows(true) do
133 count = count + 1;
134 end
135 if count > 0 then
136 return true;
137 end
138 else
139 module:log("error", "QUERY ERROR: %s %s", err, debug.traceback());
140 return nil, err;
141 end
142
143 return false;
144 end
145
146 function provider.create_user(username, password)
147 return nil, "Account creation/modification not supported.";
148 end
149
150 function provider.get_sasl_handler()
151 local realm = module:get_option("sasl_realm") or host;
152 local getpass_authentication_profile = {
153 plain = function(sasl, username, realm)
154 local prepped_username = nodeprep(username);
155 if not prepped_username then
156 module:log("debug", "NODEprep failed on username: %s", username);
157 return "", nil;
158 end
159 local password = usermanager.get_password(prepped_username, realm);
160 if not password then
161 return "", nil;
162 end
163 return password, true;
164 end
165 };
166 return new_sasl(realm, getpass_authentication_profile);
167 end
168
169 return provider;
170 end 85 end
171 86
172 module:add_item("auth-provider", new_default_provider(module.host)); 87 function provider.get_password(username)
88 local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?",
89 username, module.host);
173 90
91 if not stmt then return nil, err; end
92
93 for row in stmt:rows(true) do
94 return row.password;
95 end
96 end
97
98 function provider.set_password(username, password)
99 return nil, "Setting password is not supported.";
100 end
101
102 function provider.user_exists(username)
103 local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `realm`=?",
104 username, module.host);
105
106 if not stmt then return nil, err; end
107
108 for row in stmt:rows(true) do
109 return true;
110 end
111 end
112
113 function provider.create_user(username, password)
114 return nil, "Account creation/modification not supported.";
115 end
116
117 function provider.get_sasl_handler()
118 local profile = {
119 plain = function(sasl, username, realm)
120 local prepped_username = nodeprep(username);
121 if not prepped_username then
122 module:log("debug", "NODEprep failed on username: %s", username);
123 return "", nil;
124 end
125 local password = provider.get_password(prepped_username);
126 if not password then return "", nil; end
127 return password, true;
128 end
129 };
130 return new_sasl(module.host, profile);
131 end
132
133 module:add_item("auth-provider", provider);