mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_auth_internal_plain.lua
blob276efb641663b58abbe10a1737c07c426cb53d7b
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
9 local usermanager = require "core.usermanager";
10 local new_sasl = require "util.sasl".new;
12 local log = module._log;
13 local host = module.host;
15 local accounts = module:open_store("accounts");
17 -- define auth provider
18 local provider = {};
20 function provider.test_password(username, password)
21 log("debug", "test password for user '%s'", username);
22 local credentials = accounts:get(username) or {};
24 if password == credentials.password then
25 return true;
26 else
27 return nil, "Auth failed. Invalid username or password.";
28 end
29 end
31 function provider.get_password(username)
32 log("debug", "get_password for username '%s'", username);
33 return (accounts:get(username) or {}).password;
34 end
36 function provider.set_password(username, password)
37 log("debug", "set_password for username '%s'", username);
38 local account = accounts:get(username);
39 if account then
40 account.password = password;
41 return accounts:set(username, account);
42 end
43 return nil, "Account not available.";
44 end
46 function provider.user_exists(username)
47 local account = accounts:get(username);
48 if not account then
49 log("debug", "account not found for username '%s'", username);
50 return nil, "Auth failed. Invalid username";
51 end
52 return true;
53 end
55 function provider.users()
56 return accounts:users();
57 end
59 function provider.create_user(username, password)
60 return accounts:set(username, {password = password});
61 end
63 function provider.delete_user(username)
64 return accounts:set(username, nil);
65 end
67 function provider.get_sasl_handler()
68 local getpass_authentication_profile = {
69 plain = function(_, username, realm)
70 local password = usermanager.get_password(username, realm);
71 if not password then
72 return "", nil;
73 end
74 return password, true;
75 end
77 return new_sasl(host, getpass_authentication_profile);
78 end
80 module:provides("auth", provider);