19 local nodeprep = require "util.encodings".stringprep.nodeprep; |
19 local nodeprep = require "util.encodings".stringprep.nodeprep; |
20 local hosts = hosts; |
20 local hosts = hosts; |
21 |
21 |
22 local prosody = _G.prosody; |
22 local prosody = _G.prosody; |
23 |
23 |
24 local is_cyrus = usermanager.is_cyrus; |
|
25 |
|
26 function new_default_provider(host) |
24 function new_default_provider(host) |
27 local provider = { name = "internal_plain" }; |
25 local provider = { name = "internal_plain" }; |
28 log("debug", "initializing default authentication provider for host '%s'", host); |
26 log("debug", "initializing default authentication provider for host '%s'", host); |
29 |
27 |
30 function provider.test_password(username, password) |
28 function provider.test_password(username, password) |
31 log("debug", "test password '%s' for user %s at host %s", password, username, module.host); |
29 log("debug", "test password '%s' for user %s at host %s", password, username, module.host); |
32 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end |
|
33 local credentials = datamanager.load(username, host, "accounts") or {}; |
30 local credentials = datamanager.load(username, host, "accounts") or {}; |
34 |
31 |
35 if password == credentials.password then |
32 if password == credentials.password then |
36 return true; |
33 return true; |
37 else |
34 else |
39 end |
36 end |
40 end |
37 end |
41 |
38 |
42 function provider.get_password(username) |
39 function provider.get_password(username) |
43 log("debug", "get_password for username '%s' at host '%s'", username, module.host); |
40 log("debug", "get_password for username '%s' at host '%s'", username, module.host); |
44 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
|
45 return (datamanager.load(username, host, "accounts") or {}).password; |
41 return (datamanager.load(username, host, "accounts") or {}).password; |
46 end |
42 end |
47 |
43 |
48 function provider.set_password(username, password) |
44 function provider.set_password(username, password) |
49 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
|
50 local account = datamanager.load(username, host, "accounts"); |
45 local account = datamanager.load(username, host, "accounts"); |
51 if account then |
46 if account then |
52 account.password = password; |
47 account.password = password; |
53 return datamanager.store(username, host, "accounts", account); |
48 return datamanager.store(username, host, "accounts", account); |
54 end |
49 end |
55 return nil, "Account not available."; |
50 return nil, "Account not available."; |
56 end |
51 end |
57 |
52 |
58 function provider.user_exists(username) |
53 function provider.user_exists(username) |
59 if is_cyrus(host) then return true; end |
|
60 local account = datamanager.load(username, host, "accounts"); |
54 local account = datamanager.load(username, host, "accounts"); |
61 if not account then |
55 if not account then |
62 log("debug", "account not found for username '%s' at host '%s'", username, module.host); |
56 log("debug", "account not found for username '%s' at host '%s'", username, module.host); |
63 return nil, "Auth failed. Invalid username"; |
57 return nil, "Auth failed. Invalid username"; |
64 end |
58 end |
65 return true; |
59 return true; |
66 end |
60 end |
67 |
61 |
68 function provider.create_user(username, password) |
62 function provider.create_user(username, password) |
69 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end |
|
70 return datamanager.store(username, host, "accounts", {password = password}); |
63 return datamanager.store(username, host, "accounts", {password = password}); |
71 end |
64 end |
72 |
65 |
73 function provider.get_sasl_handler() |
66 function provider.get_sasl_handler() |
74 local realm = module:get_option("sasl_realm") or module.host; |
67 local realm = module:get_option("sasl_realm") or module.host; |