util.encodings: Fix small typo introduced in 7f789266b741
[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 log = require "util.logger".init("auth_internal_plain");
11 local type = type;
12 local error = error;
13 local ipairs = ipairs;
14 local hashes = require "util.hashes";
15 local jid_bare = require "util.jid".bare;
16 local config = require "core.configmanager";
17 local usermanager = require "core.usermanager";
18 local new_sasl = require "util.sasl".new;
19 local nodeprep = require "util.encodings".stringprep.nodeprep;
20 local hosts = hosts;
21
22 local prosody = _G.prosody;
23
24 function new_default_provider(host)
25         local provider = { name = "internal_plain" };
26         log("debug", "initializing default authentication provider for host '%s'", host);
27
28         function provider.test_password(username, password)
29                 log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
30                 local credentials = datamanager.load(username, host, "accounts") or {};
31         
32                 if password == credentials.password then
33                         return true;
34                 else
35                         return nil, "Auth failed. Invalid username or password.";
36                 end
37         end
38
39         function provider.get_password(username)
40                 log("debug", "get_password for username '%s' at host '%s'", username, module.host);
41                 return (datamanager.load(username, host, "accounts") or {}).password;
42         end
43         
44         function provider.set_password(username, password)
45                 local account = datamanager.load(username, host, "accounts");
46                 if account then
47                         account.password = password;
48                         return datamanager.store(username, host, "accounts", account);
49                 end
50                 return nil, "Account not available.";
51         end
52
53         function provider.user_exists(username)
54                 local account = datamanager.load(username, host, "accounts");
55                 if not account then
56                         log("debug", "account not found for username '%s' at host '%s'", username, module.host);
57                         return nil, "Auth failed. Invalid username";
58                 end
59                 return true;
60         end
61
62         function provider.create_user(username, password)
63                 return datamanager.store(username, host, "accounts", {password = password});
64         end
65         
66         function provider.delete_user(username)
67                 return datamanager.store(username, host, "accounts", nil);
68         end
69
70         function provider.get_sasl_handler()
71                 local getpass_authentication_profile = {
72                         plain = function(sasl, username, realm)
73                                 local prepped_username = nodeprep(username);
74                                 if not prepped_username then
75                                         log("debug", "NODEprep failed on username: %s", username);
76                                         return "", nil;
77                                 end
78                                 local password = usermanager.get_password(prepped_username, realm);
79                                 if not password then
80                                         return "", nil;
81                                 end
82                                 return password, true;
83                         end
84                 };
85                 return new_sasl(module.host, getpass_authentication_profile);
86         end
87         
88         return provider;
89 end
90
91 module:add_item("auth-provider", new_default_provider(module.host));
92