Comparison

plugins/mod_auth_internal_plain.lua @ 5115:3939960b3c07

mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 12 Sep 2012 21:32:12 +0500
parent 4762:943f9f860ab4
child 5117:2c7e1ce8f482
comparison
equal deleted inserted replaced
5113:3393cab2dd6b 5115:3939960b3c07
10 local usermanager = require "core.usermanager"; 10 local usermanager = require "core.usermanager";
11 local new_sasl = require "util.sasl".new; 11 local new_sasl = require "util.sasl".new;
12 local nodeprep = require "util.encodings".stringprep.nodeprep; 12 local nodeprep = require "util.encodings".stringprep.nodeprep;
13 13
14 local log = module._log; 14 local log = module._log;
15 local host = module.host;
15 16
16 function new_default_provider(host) 17 -- define auth provider
17 local provider = { name = "internal_plain" }; 18 local provider = { name = "internal_plain" };
18 log("debug", "initializing internal_plain authentication provider for host '%s'", host); 19 log("debug", "initializing internal_plain authentication provider for host '%s'", host);
19 20
20 function provider.test_password(username, password) 21 function provider.test_password(username, password)
21 log("debug", "test password '%s' for user %s at host %s", password, username, module.host); 22 log("debug", "test password '%s' for user %s at host %s", password, username, host);
22 local credentials = datamanager.load(username, host, "accounts") or {}; 23 local credentials = datamanager.load(username, host, "accounts") or {};
23 24
24 if password == credentials.password then 25 if password == credentials.password then
25 return true; 26 return true;
26 else 27 else
27 return nil, "Auth failed. Invalid username or password."; 28 return nil, "Auth failed. Invalid username or password.";
28 end
29 end 29 end
30
31 function provider.get_password(username)
32 log("debug", "get_password for username '%s' at host '%s'", username, module.host);
33 return (datamanager.load(username, host, "accounts") or {}).password;
34 end
35
36 function provider.set_password(username, password)
37 local account = datamanager.load(username, host, "accounts");
38 if account then
39 account.password = password;
40 return datamanager.store(username, host, "accounts", account);
41 end
42 return nil, "Account not available.";
43 end
44
45 function provider.user_exists(username)
46 local account = datamanager.load(username, host, "accounts");
47 if not account then
48 log("debug", "account not found for username '%s' at host '%s'", username, module.host);
49 return nil, "Auth failed. Invalid username";
50 end
51 return true;
52 end
53
54 function provider.create_user(username, password)
55 return datamanager.store(username, host, "accounts", {password = password});
56 end
57
58 function provider.delete_user(username)
59 return datamanager.store(username, host, "accounts", nil);
60 end
61
62 function provider.get_sasl_handler()
63 local getpass_authentication_profile = {
64 plain = function(sasl, username, realm)
65 local prepped_username = nodeprep(username);
66 if not prepped_username then
67 log("debug", "NODEprep failed on username: %s", username);
68 return "", nil;
69 end
70 local password = usermanager.get_password(prepped_username, realm);
71 if not password then
72 return "", nil;
73 end
74 return password, true;
75 end
76 };
77 return new_sasl(module.host, getpass_authentication_profile);
78 end
79
80 return provider;
81 end 30 end
82 31
83 module:add_item("auth-provider", new_default_provider(module.host)); 32 function provider.get_password(username)
33 log("debug", "get_password for username '%s' at host '%s'", username, host);
34 return (datamanager.load(username, host, "accounts") or {}).password;
35 end
84 36
37 function provider.set_password(username, password)
38 local account = datamanager.load(username, host, "accounts");
39 if account then
40 account.password = password;
41 return datamanager.store(username, host, "accounts", account);
42 end
43 return nil, "Account not available.";
44 end
45
46 function provider.user_exists(username)
47 local account = datamanager.load(username, host, "accounts");
48 if not account then
49 log("debug", "account not found for username '%s' at host '%s'", username, host);
50 return nil, "Auth failed. Invalid username";
51 end
52 return true;
53 end
54
55 function provider.create_user(username, password)
56 return datamanager.store(username, host, "accounts", {password = password});
57 end
58
59 function provider.delete_user(username)
60 return datamanager.store(username, host, "accounts", nil);
61 end
62
63 function provider.get_sasl_handler()
64 local getpass_authentication_profile = {
65 plain = function(sasl, username, realm)
66 local prepped_username = nodeprep(username);
67 if not prepped_username then
68 log("debug", "NODEprep failed on username: %s", username);
69 return "", nil;
70 end
71 local password = usermanager.get_password(prepped_username, realm);
72 if not password then
73 return "", nil;
74 end
75 return password, true;
76 end
77 };
78 return new_sasl(host, getpass_authentication_profile);
79 end
80
81 module:add_item("auth-provider", provider);
82