Mercurial > prosody-hg
comparison plugins/mod_hashpassauth.lua @ 3164:db9def53fe9c
Check in mod_hashpassauth -- works!
| author | Jeff Mitchell <jeff@jefferai.org> |
|---|---|
| date | Wed, 26 May 2010 18:16:58 -0400 |
| parents | |
| children | 3c46cb94caed |
comparison
equal
deleted
inserted
replaced
| 3163:a23168cc4af5 | 3164:db9def53fe9c |
|---|---|
| 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 saltedPasswordSHA1 = require "util.sasl.scram".saltedPasswordSHA1; | |
| 18 local config = require "core.configmanager"; | |
| 19 local usermanager = require "core.usermanager"; | |
| 20 local generate_uuid = require "util.uuid".generate; | |
| 21 local hosts = hosts; | |
| 22 | |
| 23 local prosody = _G.prosody; | |
| 24 | |
| 25 local is_cyrus = usermanager.is_cyrus; | |
| 26 | |
| 27 -- Default; can be set per-user | |
| 28 local iteration_count = 4096; | |
| 29 | |
| 30 function new_hashpass_provider(host) | |
| 31 local provider = { name = "hashpass" }; | |
| 32 log("debug", "initializing hashpass authentication provider for host '%s'", host); | |
| 33 | |
| 34 function provider.test_password(username, password) | |
| 35 log("debug", "test password for user %s at host %s", username, module.host); | |
| 36 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end | |
| 37 local credentials = datamanager.load(username, host, "accounts") or {}; | |
| 38 | |
| 39 if credentials.hashpass == nil or credentials.iteration_count == nil or credentials.salt == nil then | |
| 40 return nil, "Auth failed. Stored credential information is not complete."; | |
| 41 end | |
| 42 | |
| 43 local valid, binpass = saltedPasswordSHA1(password, credentials.salt, credentials.iteration_count); | |
| 44 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end); | |
| 45 if valid then | |
| 46 log("debug", "salted password returned valid"); | |
| 47 else | |
| 48 log("debug", "salted password returned not valid"); | |
| 49 end | |
| 50 log("debug", "hexpass is '%s', stored pass is '%s'", hexpass, credentials.hashpass); | |
| 51 if valid and hexpass == credentials.hashpass then | |
| 52 return true; | |
| 53 else | |
| 54 return nil, "Auth failed. Invalid username, password, or password hash information."; | |
| 55 end | |
| 56 end | |
| 57 | |
| 58 function provider.get_password(username) | |
| 59 log("debug", "get_password for username '%s' at host '%s'", username, module.host); | |
| 60 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end | |
| 61 return (datamanager.load(username, host, "accounts") or {}).hashpass; | |
| 62 end | |
| 63 | |
| 64 function provider.set_password(username, password) | |
| 65 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end | |
| 66 local account = datamanager.load(username, host, "accounts"); | |
| 67 if account then | |
| 68 if account.iteration_count == nil then | |
| 69 account.iteration_count = iteration_count; | |
| 70 end | |
| 71 | |
| 72 if account.salt == nil then | |
| 73 account.salt = generate_uuid(); | |
| 74 end | |
| 75 | |
| 76 local valid, binpass = saltedPasswordSHA1(password, account.salt, account.iteration_count); | |
| 77 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end); | |
| 78 account.hashpass = hexpass; | |
| 79 | |
| 80 return datamanager.store(username, host, "accounts", account); | |
| 81 end | |
| 82 return nil, "Account not available."; | |
| 83 end | |
| 84 | |
| 85 function provider.user_exists(username) | |
| 86 if is_cyrus(host) then return true; end | |
| 87 local account = datamanager.load(username, host, "accounts"); | |
| 88 if not account then | |
| 89 log("debug", "account not found for username '%s' at host '%s'", username, module.host); | |
| 90 return nil, "Auth failed. Invalid username"; | |
| 91 end | |
| 92 if account.hashpass == nil or string.len(account.hashpass) == 0 then | |
| 93 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host); | |
| 94 return nil, "Auth failed. Password invalid."; | |
| 95 end | |
| 96 return true; | |
| 97 end | |
| 98 | |
| 99 function provider.create_user(username, password) | |
| 100 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end | |
| 101 local salt = generate_uuid(); | |
| 102 local valid, binpass = saltedPasswordSHA1(password, salt, iteration_count); | |
| 103 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end); | |
| 104 return datamanager.store(username, host, "accounts", {hashpass = hexpass, salt = salt, iteration_count = iteration_count}); | |
| 105 end | |
| 106 | |
| 107 function provider.get_supported_methods() | |
| 108 return {["PLAIN"] = true}; -- TODO this should be taken from the config | |
| 109 end | |
| 110 | |
| 111 function provider.is_admin(jid) | |
| 112 local admins = config.get(host, "core", "admins"); | |
| 113 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then | |
| 114 jid = jid_bare(jid); | |
| 115 for _,admin in ipairs(admins) do | |
| 116 if admin == jid then return true; end | |
| 117 end | |
| 118 elseif admins then | |
| 119 log("error", "Option 'admins' for host '%s' is not a table", host); | |
| 120 end | |
| 121 return is_admin(jid); -- Test whether it's a global admin instead | |
| 122 end | |
| 123 return provider; | |
| 124 end | |
| 125 | |
| 126 module:add_item("auth-provider", new_hashpass_provider(module.host)); | |
| 127 |
