3162
|
1 |
-- Prosody IM |
|
2 |
-- Copyright (C) 2008-2010 Matthew Wild |
|
3 |
-- Copyright (C) 2008-2010 Waqas Hussain |
|
4 |
-- Copyright (C) 2010 Jeff Mitchell |
|
5 |
-- |
|
6 |
-- This project is MIT/X11 licensed. Please see the |
|
7 |
-- COPYING file in the source package for more information. |
|
8 |
-- |
|
9 |
|
|
10 |
local datamanager = require "util.datamanager"; |
|
11 |
local log = require "util.logger".init("usermanager"); |
|
12 |
local type = type; |
|
13 |
local error = error; |
|
14 |
local ipairs = ipairs; |
|
15 |
local hashes = require "util.hashes"; |
|
16 |
local jid_bare = require "util.jid".bare; |
|
17 |
local config = require "core.configmanager"; |
|
18 |
local hosts = hosts; |
|
19 |
|
|
20 |
local prosody = _G.prosody; |
|
21 |
|
|
22 |
function new_default_provider(host) |
|
23 |
local provider = { name = "default" }; |
|
24 |
|
|
25 |
function provider.test_password(username, password) |
|
26 |
log("debug", "test password for user %s at host %s", username, host); |
|
27 |
if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end |
|
28 |
local credentials = datamanager.load(username, host, "accounts") or {}; |
|
29 |
|
|
30 |
if password == credentials.password then |
|
31 |
return true; |
|
32 |
else |
|
33 |
return nil, "Auth failed. Invalid username or password."; |
|
34 |
end |
|
35 |
end |
|
36 |
|
|
37 |
function provider.get_password(username) |
|
38 |
log("debug", "get password for user %s at host %s", username, host); |
|
39 |
if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
|
40 |
return (datamanager.load(username, host, "accounts") or {}).password; |
|
41 |
end |
|
42 |
|
|
43 |
function provider.set_password(username, password) |
|
44 |
if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
|
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 |
if is_cyrus(host) then return true; end |
|
55 |
local account = datamanager.load(username, host, "accounts"); |
|
56 |
if not account then |
|
57 |
log("debug", "account not found for username '%s' at host '%s'", username, host); |
|
58 |
return nil, "Auth failed. Invalid username"; |
|
59 |
end |
|
60 |
if account.password == nil or string.len(account.password) == 0 then |
|
61 |
log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, host); |
|
62 |
return nil, "Auth failed. Password invalid."; |
|
63 |
end |
|
64 |
return true; |
|
65 |
end |
|
66 |
|
|
67 |
function provider.create_user(username, password) |
|
68 |
if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end |
|
69 |
return datamanager.store(username, host, "accounts", {password = password}); |
|
70 |
end |
|
71 |
|
|
72 |
function provider.get_supported_methods() |
|
73 |
return {["PLAIN"] = true, ["DIGEST-MD5"] = true}; -- TODO this should be taken from the config |
|
74 |
end |
|
75 |
|
|
76 |
function provider.is_admin(jid) |
|
77 |
local admins = config.get(host, "core", "admins"); |
|
78 |
if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then |
|
79 |
jid = jid_bare(jid); |
|
80 |
for _,admin in ipairs(admins) do |
|
81 |
if admin == jid then return true; end |
|
82 |
end |
|
83 |
elseif admins then |
|
84 |
log("error", "Option 'admins' for host '%s' is not a table", host); |
|
85 |
end |
|
86 |
return is_admin(jid); -- Test whether it's a global admin instead |
|
87 |
end |
|
88 |
return provider; |
|
89 |
end |
|
90 |
|
|
91 |
module:add_item("auth-provider", new_default_provider(module.host)); |
|
92 |
|