4 -- |
4 -- |
5 -- This project is MIT/X11 licensed. Please see the |
5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. |
6 -- COPYING file in the source package for more information. |
7 -- |
7 -- |
8 |
8 |
9 local datamanager = require "util.datamanager"; |
|
10 local usermanager = require "core.usermanager"; |
9 local usermanager = require "core.usermanager"; |
11 local new_sasl = require "util.sasl".new; |
10 local new_sasl = require "util.sasl".new; |
12 |
11 |
13 local log = module._log; |
12 local log = module._log; |
14 local host = module.host; |
13 local host = module.host; |
|
14 |
|
15 local accounts = module:open_store("accounts"); |
15 |
16 |
16 -- define auth provider |
17 -- define auth provider |
17 local provider = {}; |
18 local provider = {}; |
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, 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 = accounts:get(username) 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 end |
30 |
31 |
31 function provider.get_password(username) |
32 function provider.get_password(username) |
32 log("debug", "get_password for username '%s' at host '%s'", username, host); |
33 log("debug", "get_password for username '%s' at host '%s'", username, host); |
33 return (datamanager.load(username, host, "accounts") or {}).password; |
34 return (accounts:get(username) or {}).password; |
34 end |
35 end |
35 |
36 |
36 function provider.set_password(username, password) |
37 function provider.set_password(username, password) |
37 local account = datamanager.load(username, host, "accounts"); |
38 local account = accounts:get(username); |
38 if account then |
39 if account then |
39 account.password = password; |
40 account.password = password; |
40 return datamanager.store(username, host, "accounts", account); |
41 return accounts:set(username, account); |
41 end |
42 end |
42 return nil, "Account not available."; |
43 return nil, "Account not available."; |
43 end |
44 end |
44 |
45 |
45 function provider.user_exists(username) |
46 function provider.user_exists(username) |
46 local account = datamanager.load(username, host, "accounts"); |
47 local account = accounts:get(username); |
47 if not account then |
48 if not account then |
48 log("debug", "account not found for username '%s' at host '%s'", username, host); |
49 log("debug", "account not found for username '%s' at host '%s'", username, host); |
49 return nil, "Auth failed. Invalid username"; |
50 return nil, "Auth failed. Invalid username"; |
50 end |
51 end |
51 return true; |
52 return true; |
52 end |
53 end |
53 |
54 |
54 function provider.users() |
55 function provider.users() |
55 return datamanager.users(host, "accounts"); |
56 return accounts:users(); |
56 end |
57 end |
57 |
58 |
58 function provider.create_user(username, password) |
59 function provider.create_user(username, password) |
59 return datamanager.store(username, host, "accounts", {password = password}); |
60 return accounts:set(username, {password = password}); |
60 end |
61 end |
61 |
62 |
62 function provider.delete_user(username) |
63 function provider.delete_user(username) |
63 return datamanager.store(username, host, "accounts", nil); |
64 return accounts:set(username, nil); |
64 end |
65 end |
65 |
66 |
66 function provider.get_sasl_handler() |
67 function provider.get_sasl_handler() |
67 local getpass_authentication_profile = { |
68 local getpass_authentication_profile = { |
68 plain = function(sasl, username, realm) |
69 plain = function(sasl, username, realm) |