Merge with sasl branch.
[prosody.git] / core / usermanager.lua
1 -- Prosody IM
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 local datamanager = require "util.datamanager";
10 local log = require "util.logger".init("usermanager");
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
18 module "usermanager"
19
20 function validate_credentials(host, username, password, method)
21         log("debug", "User '%s' is being validated", username);
22         local credentials = datamanager.load(username, host, "accounts") or {};
23
24         if method == nil then method = "PLAIN"; end
25         if method == "PLAIN" and credentials.password then -- PLAIN, do directly
26                 if password == credentials.password then
27                         return true;
28                 else
29                         return nil, "Auth failed. Invalid username or password.";
30                 end
31   end
32         -- must do md5
33         -- make credentials md5
34         local pwd = credentials.password;
35         if not pwd then pwd = credentials.md5; else pwd = hashes.md5(pwd, true); end
36         -- make password md5
37         if method == "PLAIN" then
38                 password = hashes.md5(password or "", true);
39         elseif method ~= "DIGEST-MD5" then
40                 return nil, "Unsupported auth method";
41         end
42         -- compare
43         if password == pwd then
44                 return true;
45         else
46                 return nil, "Auth failed. Invalid username or password.";
47         end
48 end
49
50 function get_password(username, host)
51   return (datamanager.load(username, host, "accounts") or {}).password
52 end
53
54 function user_exists(username, host)
55         return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials
56 end
57
58 function create_user(username, password, host)
59         return datamanager.store(username, host, "accounts", {password = password});
60 end
61
62 function get_supported_methods(host)
63         return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config
64 end
65
66 function is_admin(jid, host)
67         host = host or "*";
68         local admins = config.get(host, "core", "admins");
69         if host ~= "*" and admins == config.get("*", "core", "admins") then
70                 return nil;
71         end
72         if type(admins) == "table" then
73                 jid = jid_bare(jid);
74                 for _,admin in ipairs(admins) do
75                         if admin == jid then return true; end
76                 end
77         elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end
78         return nil;
79 end
80
81 return _M;