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