Stanza router: Message to bare JID fixes
[prosody.git] / core / usermanager.lua
1 -- Prosody IM v0.3
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10
11 require "util.datamanager"
12 local datamanager = datamanager;
13 local log = require "util.logger".init("usermanager");
14 local error = error;
15 local hashes = require "util.hashes";
16
17 module "usermanager"
18
19 function validate_credentials(host, username, password, method)
20         log("debug", "User '%s' is being validated", username);
21         local credentials = datamanager.load(username, host, "accounts") or {};
22         if method == nil then method = "PLAIN"; end
23         if method == "PLAIN" and credentials.password then -- PLAIN, do directly
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         -- must do md5
31         -- make credentials md5
32         local pwd = credentials.password;
33         if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
34         -- make password md5
35         if method == "PLAIN" then
36                 password = hashes.md5(password or "", true);
37         elseif method ~= "DIGEST-MD5" then
38                 return nil, "Unsupported auth method";
39         end
40         -- compare
41         if password == pwd then
42                 return true;
43         else
44                 return nil, "Auth failed. Invalid username or password.";
45         end
46 end
47
48 function user_exists(username, host)
49         return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
50 end
51
52 function create_user(username, password, host)
53         return datamanager.store(username, host, "accounts", {password = password});
54 end
55
56 function get_supported_methods(host)
57         local methods = {["PLAIN"] = true}; -- TODO this should be taken from the config
58         methods["DIGEST-MD5"] = true;
59         return methods;
60 end
61
62 return _M;