Created
May 4, 2010 21:16
-
-
Save zeen/390010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Prosody IM | |
-- Copyright (C) 2008-2009 Matthew Wild | |
-- Copyright (C) 2008-2009 Waqas Hussain | |
-- | |
-- This project is MIT/X11 licensed. Please see the | |
-- COPYING file in the source package for more information. | |
-- | |
local datamanager = require "util.datamanager"; | |
local log = require "util.logger".init("usermanager"); | |
local type = type; | |
local error = error; | |
local ipairs = ipairs; | |
local hashes = require "util.hashes"; | |
local jid_bare = require "util.jid".bare; | |
local config = require "core.configmanager"; | |
local hosts = hosts; | |
local default_handler = {}; | |
function default_handler:create_user(username, host, password) | |
return datamanager.store(username, host, "accounts", {password = password}); | |
end | |
function default_handler:user_exists(username, host) | |
return datamanager.load(username, host, "accounts") ~= nil; -- FIXME also check for empty credentials | |
end | |
function default_handler:get_password(username, host) | |
return (datamanager.load(username, host, "accounts") or {}).password; | |
end | |
function default_handler:set_password(username, host, password) | |
local user = datamanager.load(username, host, "accounts"); | |
if user then | |
user.password = password; | |
return datamanager.store(username, host, "accounts", user); | |
end | |
return nil, "Can't set password for non-existent user"; | |
end | |
function default_handler:test_password(username, host, password) | |
local credentials = datamanager.load(username, host, "accounts") or {}; | |
if password == credentials.password then return true; end | |
return nil, "Auth failed. Invalid username or password."; | |
end | |
function default_handler:create_sasl_handler(host) | |
-- TODO | |
end | |
local global_handler = default_handler; | |
module "usermanager" | |
function get_default_auth_handler() return default_handler; end | |
function set_auth_handler(host, handler) | |
if type(host) == "function" then host, handler = nil, host; end | |
if not handler then handler = default_handler; end | |
if not host then | |
global_handler = handler; | |
elseif hosts[host] then | |
hosts[host].auth_handler = handler; | |
else | |
error("Setting auth handler for non-existent host"); | |
end | |
end | |
function get_auth_handler(host) | |
return hosts[host] and (hosts[host].auth_handler or global_handler) or (host and {} or global_handler); | |
end | |
function validate_credentials(host, username, password) | |
log("debug", "User '%s' is being validated", username); | |
local handler = get_auth_handler(host); | |
if handler.test_password then return handler:test_password(username, host, password); end | |
return nil, "Auth not supported for host"; | |
end | |
function get_password(username, host) | |
local handler = get_auth_handler(host); | |
if handler.get_password then return handler:get_password(username, host); end | |
return nil, "Getting password not supported"; | |
end | |
function user_exists(username, host) | |
local handler = get_auth_handler(host); | |
if handler.user_exists then return handler:user_exists(username, host); end | |
return true; -- always return true when unsupported | |
end | |
function create_user(username, password, host) | |
local handler = get_auth_handler(host); | |
if handler.create_user then return handler:create_user(username, host); end | |
return nil, "User creation not supported"; | |
end | |
function is_admin(jid, host) | |
host = host or "*"; | |
local admins = config.get(host, "core", "admins"); | |
if host ~= "*" and admins == config.get("*", "core", "admins") then | |
return nil; | |
end | |
if type(admins) == "table" then | |
jid = jid_bare(jid); | |
for _,admin in ipairs(admins) do | |
if admin == jid then return true; end | |
end | |
elseif admins then log("warn", "Option 'admins' for host '%s' is not a table", host); end | |
return nil; | |
end | |
return _M; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment