comparison mod_auth_phpbb3/mod_auth_phpbb3.lua @ 419:2a2b70e1a998

mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 10 Sep 2011 22:35:59 +0500
parents 145fa870321c
children eaafb38daa5e
comparison
equal deleted inserted replaced
418:e840b4ce538d 419:2a2b70e1a998
4 -- 4 --
5 5
6 local log = require "util.logger".init("auth_sql"); 6 local log = require "util.logger".init("auth_sql");
7 local new_sasl = require "util.sasl".new; 7 local new_sasl = require "util.sasl".new;
8 local nodeprep = require "util.encodings".stringprep.nodeprep; 8 local nodeprep = require "util.encodings".stringprep.nodeprep;
9 local saslprep = require "util.encodings".stringprep.saslprep;
9 local DBI = require "DBI" 10 local DBI = require "DBI"
10 local md5 = require "util.hashes".md5; 11 local md5 = require "util.hashes".md5;
11 local uuid_gen = require "util.uuid".generate; 12 local uuid_gen = require "util.uuid".generate;
12 13
13 local connection; 14 local connection;
77 if not stmt then return stmt, err; end 78 if not stmt then return stmt, err; end
78 return stmt:affected(); 79 return stmt:affected();
79 end 80 end
80 81
81 local function get_password(username) 82 local function get_password(username)
82 local stmt, err = getsql("SELECT `user_password` FROM `phpbb_users` WHERE `username`=?", username); 83 local stmt, err = getsql("SELECT `user_password` FROM `phpbb_users` WHERE `username_clean`=?", username);
83 if stmt then 84 if stmt then
84 for row in stmt:rows(true) do 85 for row in stmt:rows(true) do
85 return row.user_password; 86 return row.user_password;
86 end 87 end
87 end 88 end
194 end 195 end
195 function provider.create_user(username, password) 196 function provider.create_user(username, password)
196 return nil, "Account creation/modification not supported."; 197 return nil, "Account creation/modification not supported.";
197 end 198 end
198 199
200 local escapes = {
201 [" "] = "\\20";
202 ['"'] = "\\22";
203 ["&"] = "\\26";
204 ["'"] = "\\27";
205 ["/"] = "\\2f";
206 [":"] = "\\3a";
207 ["<"] = "\\3c";
208 [">"] = "\\3e";
209 ["@"] = "\\40";
210 ["\\"] = "\\5c";
211 };
212 local unescapes = {};
213 for k,v in pairs(escapes) do unescapes[v] = k; end
214 local function jid_escape(s) return s and (s:gsub(".", escapes)); end
215 local function jid_unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end
216
199 function provider.get_sasl_handler() 217 function provider.get_sasl_handler()
200 local profile = { 218 local sasl = {};
201 plain_test = function(sasl, username, password, realm) 219 function sasl:clean_clone() return provider.get_sasl_handler(); end
202 -- TODO stringprep 220 function sasl:mechanisms() return { PLAIN = true; }; end
203 return provider.test_password(username, password), true; 221 function sasl:select(mechanism)
204 end; 222 if not self.selected and mechanism == "PLAIN" then
205 }; 223 self.selected = mechanism;
206 return new_sasl(module.host, profile); 224 return true;
225 end
226 end
227 function sasl:process(message)
228 if not message then return "failure", "malformed-request"; end
229 local authorization, authentication, password = message:match("^([^%z]*)%z([^%z]+)%z([^%z]+)");
230 if not authorization then return "failure", "malformed-request"; end
231 authentication = saslprep(authentication);
232 password = saslprep(password);
233 if (not password) or (password == "") or (not authentication) or (authentication == "") then
234 return "failure", "malformed-request", "Invalid username or password.";
235 end
236 local function test(authentication)
237 local prepped = nodeprep(authentication);
238 local normalized = jid_unescape(prepped);
239 return normalized and provider.test_password(normalized, password) and prepped;
240 end
241 local username = test(authentication) or test(jid_escape(authentication));
242 if username then
243 self.username = username;
244 return "success";
245 end
246 return "failure", "not-authorized", "Unable to authorize you with the authentication credentials you've sent.";
247 end
248 return sasl;
207 end 249 end
208 250
209 module:add_item("auth-provider", provider); 251 module:add_item("auth-provider", provider);
210 252