89dd7f1bcdfe4c80c03ac1b518747c54c069bfd1
[prosody.git] / plugins / mod_auth_internal_plain.lua
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 --
8
9 local datamanager = require "util.datamanager";
10 local usermanager = require "core.usermanager";
11 local new_sasl = require "util.sasl".new;
12 local nodeprep = require "util.encodings".stringprep.nodeprep;
13
14 local log = module._log;
15
16 function new_default_provider(host)
17         local provider = { name = "internal_plain" };
18         log("debug", "initializing internal_plain authentication provider for host '%s'", host);
19
20         function provider.test_password(username, password)
21                 log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
22                 local credentials = datamanager.load(username, host, "accounts") or {};
23         
24                 if password == credentials.password then
25                         return true;
26                 else
27                         return nil, "Auth failed. Invalid username or password.";
28                 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
82
83 module:add_item("auth-provider", new_default_provider(module.host));
84