net.http.server: Properly handle persistent connections
[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 local host = module.host;
16
17 -- define auth provider
18 local provider = {};
19 log("debug", "initializing internal_plain authentication provider for host '%s'", host);
20
21 function provider.test_password(username, password)
22         log("debug", "test password '%s' for user %s at host %s", password, username, host);
23         local credentials = datamanager.load(username, host, "accounts") or {};
24
25         if password == credentials.password then
26                 return true;
27         else
28                 return nil, "Auth failed. Invalid username or password.";
29         end
30 end
31
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
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.users()
56         return datamanager.users(host, "accounts");
57 end
58
59 function provider.create_user(username, password)
60         return datamanager.store(username, host, "accounts", {password = password});
61 end
62
63 function provider.delete_user(username)
64         return datamanager.store(username, host, "accounts", nil);
65 end
66
67 function provider.get_sasl_handler()
68         local getpass_authentication_profile = {
69                 plain = function(sasl, username, realm)
70                         local prepped_username = nodeprep(username);
71                         if not prepped_username then
72                                 log("debug", "NODEprep failed on username: %s", username);
73                                 return "", nil;
74                         end
75                         local password = usermanager.get_password(prepped_username, realm);
76                         if not password then
77                                 return "", nil;
78                         end
79                         return password, true;
80                 end
81         };
82         return new_sasl(host, getpass_authentication_profile);
83 end
84         
85 module:provides("auth", provider);
86