comparison mod_auth_sql/mod_auth_sql.lua @ 399:4e0d36941ba1

mod_auth_sql: More cleanup.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 10 Aug 2011 05:30:08 +0500
parents fdd4f5ab029a
children 52f2188ec47d
comparison
equal deleted inserted replaced
398:fdd4f5ab029a 399:4e0d36941ba1
1 -- Simple SQL Authentication module for Prosody IM 1 -- Simple SQL Authentication module for Prosody IM
2 -- Copyright (C) 2011 Tomasz Sterna <tomek@xiaoka.com> 2 -- Copyright (C) 2011 Tomasz Sterna <tomek@xiaoka.com>
3 -- Copyright (C) 2011 Waqas Hussain <waqas20@gmail.com>
3 -- 4 --
4 5
5 local log = require "util.logger".init("auth_sql"); 6 local log = require "util.logger".init("auth_sql");
6 local new_sasl = require "util.sasl".new; 7 local new_sasl = require "util.sasl".new;
7 local nodeprep = require "util.encodings".stringprep.nodeprep; 8 local nodeprep = require "util.encodings".stringprep.nodeprep;
68 if not ok then return nil, err; end 69 if not ok then return nil, err; end
69 70
70 return stmt; 71 return stmt;
71 end 72 end
72 73
74 local function get_password(username)
75 local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?", username, module.host);
76 if stmt then
77 for row in stmt:rows(true) do
78 return row.password;
79 end
80 end
81 end
82
73 83
74 provider = { name = "sql" }; 84 provider = { name = "sql" };
75 85
76 function provider.test_password(username, password) 86 function provider.test_password(username, password)
77 local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `password`=? AND `realm`=?", 87 return password and get_password(username) == password;
78 username, password, module.host);
79
80 if not stmt then return nil, err; end
81
82 for row in stmt:rows(true) do
83 return true;
84 end
85 end 88 end
86
87 function provider.get_password(username) 89 function provider.get_password(username)
88 local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?", 90 return get_password(username);
89 username, module.host);
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 91 end
97
98 function provider.set_password(username, password) 92 function provider.set_password(username, password)
99 return nil, "Setting password is not supported."; 93 return nil, "Setting password is not supported.";
100 end 94 end
101
102 function provider.user_exists(username) 95 function provider.user_exists(username)
103 local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `realm`=?", 96 return get_password(username) and true;
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 97 end
112
113 function provider.create_user(username, password) 98 function provider.create_user(username, password)
114 return nil, "Account creation/modification not supported."; 99 return nil, "Account creation/modification not supported.";
115 end 100 end
116
117 function provider.get_sasl_handler() 101 function provider.get_sasl_handler()
118 local profile = { 102 local profile = {
119 plain = function(sasl, username, realm) 103 plain = function(sasl, username, realm)
120 local prepped_username = nodeprep(username); 104 local prepped_username = nodeprep(username);
121 if not prepped_username then 105 if not prepped_username then
122 module:log("debug", "NODEprep failed on username: %s", username); 106 module:log("debug", "NODEprep failed on username: %s", username);
123 return "", nil; 107 return "", nil;
124 end 108 end
125 local password = provider.get_password(prepped_username); 109 local password = get_password(prepped_username);
126 if not password then return "", nil; end 110 if not password then return "", nil; end
127 return password, true; 111 return password, true;
128 end 112 end
129 }; 113 };
130 return new_sasl(module.host, profile); 114 return new_sasl(module.host, profile);