plugins/mod_auth_internal_hashed.lua

Wed, 09 Jun 2010 17:54:34 +0200

author
Tobias Markmann <tm@ayena.de>
date
Wed, 09 Jun 2010 17:54:34 +0200
changeset 3211
d69e90ffbc09
parent 3210
5e51f8a7179b
child 3212
e416b9185c6b
permissions
-rw-r--r--

mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.

3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
1 -- Prosody IM
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
4 -- Copyright (C) 2010 Jeff Mitchell
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
5 --
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
8 --
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
9
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
10 local datamanager = require "util.datamanager";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
11 local log = require "util.logger".init("usermanager");
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
12 local type = type;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
13 local error = error;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
14 local ipairs = ipairs;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
15 local hashes = require "util.hashes";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
16 local jid_bare = require "util.jid".bare;
3205
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
17 local getAuthenticationDatabaseSHA1 = require "util.sasl.scram".getAuthenticationDatabaseSHA1;
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
18 local config = require "core.configmanager";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
19 local usermanager = require "core.usermanager";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
20 local generate_uuid = require "util.uuid".generate;
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
21 local new_sasl = require "util.sasl".new;
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
22 local nodeprep = require "util.encodings".stringprep.nodeprep;
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
23 local hosts = hosts;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
24
3208
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
25 -- TODO: remove these two lines in near future
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
26 local hmac_sha1 = require "util.hmac".sha1;
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
27 local sha1 = require "util.hashes".sha1;
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
28
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
29 local prosody = _G.prosody;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
30
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
31 local is_cyrus = usermanager.is_cyrus;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
32
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
33 -- Default; can be set per-user
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
34 local iteration_count = 4096;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
35
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
36 function new_hashpass_provider(host)
3180
99be525bcfb4 Rename mod_defaultauth -> mod_auth_internal, mod_hashpassauth -> mod_auth_internal_hashed, and the providers to internal and internal_hashed respectively. Also no longer auto-load defaultauth, but instead auto-load the plugin selected for each host at startup based on the provider name.
Matthew Wild <mwild1@gmail.com>
parents: 3162
diff changeset
37 local provider = { name = "internal_hashed" };
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
38 log("debug", "initializing hashpass authentication provider for host '%s'", host);
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
39
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
40 function provider.test_password(username, password)
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
41 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
42 local credentials = datamanager.load(username, host, "accounts") or {};
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
43
3161
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
44 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
45 if credentials.password ~= password then
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
46 return nil, "Auth failed. Provided password is incorrect.";
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
47 end
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
48
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
49 if provider.set_password(username, credentials.password) == nil then
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
50 return nil, "Auth failed. Could not set hashed password from plaintext.";
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
51 else
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
52 return true;
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
53 end
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
54 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
55
3162
546695e80e0a Correct out of order logic in mod_hashpassauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3161
diff changeset
56 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then
546695e80e0a Correct out of order logic in mod_hashpassauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3161
diff changeset
57 return nil, "Auth failed. Stored salt and iteration count information is not complete.";
546695e80e0a Correct out of order logic in mod_hashpassauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3161
diff changeset
58 end
3205
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
59
3208
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
60 local valid, stored_key, server_key
3207
b350d9753804 mod_auth_internal_hashed: Store stored_key and server_key when setting a password.
Tobias Markmann <tm@ayena.de>
parents: 3205
diff changeset
61
3210
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
62 -- convert hexpass to stored_key and server_key
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
63 -- TODO: remove this in near future
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
64 if credentials.hashpass then
3208
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
65 valid = true;
3210
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
66 local salted_password = credentials.hashpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
67 log("debug", "salted_password in bin: %s", tostring(salted_password));
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
68 credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key")):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
69 credentials.server_key = hmac_sha1(salted_password, "Server Key"):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
3208
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
70 end
4b660bf61048 mod_auth_internal_hashed: Coverting salted password to stored_key and server_key.
Tobias Markmann <tm@ayena.de>
parents: 3207
diff changeset
71
3210
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
72 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count);
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
73
3205
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
74 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
75 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
76
3210
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
77 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
78 return true;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
79 else
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
80 return nil, "Auth failed. Invalid username, password, or password hash information.";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
81 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
82 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
83
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
84 function provider.set_password(username, password)
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
85 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
86 local account = datamanager.load(username, host, "accounts");
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
87 if account then
3210
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
88 account.salt = account.salt or generate_uuid();
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
89 account.iteration_count = account.iteration_count or iteration_count;
5e51f8a7179b mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on PLAIN login.
Tobias Markmann <tm@ayena.de>
parents: 3208
diff changeset
90 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count);
3207
b350d9753804 mod_auth_internal_hashed: Store stored_key and server_key when setting a password.
Tobias Markmann <tm@ayena.de>
parents: 3205
diff changeset
91 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
b350d9753804 mod_auth_internal_hashed: Store stored_key and server_key when setting a password.
Tobias Markmann <tm@ayena.de>
parents: 3205
diff changeset
92 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
b350d9753804 mod_auth_internal_hashed: Store stored_key and server_key when setting a password.
Tobias Markmann <tm@ayena.de>
parents: 3205
diff changeset
93
b350d9753804 mod_auth_internal_hashed: Store stored_key and server_key when setting a password.
Tobias Markmann <tm@ayena.de>
parents: 3205
diff changeset
94 account.stored_key = stored_key_hex
b350d9753804 mod_auth_internal_hashed: Store stored_key and server_key when setting a password.
Tobias Markmann <tm@ayena.de>
parents: 3205
diff changeset
95 account.server_key = server_key_hex
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
96
3161
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
97 account.password = nil;
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
98 return datamanager.store(username, host, "accounts", account);
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
99 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
100 return nil, "Account not available.";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
101 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
102
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
103 function provider.user_exists(username)
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
104 if is_cyrus(host) then return true; end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
105 local account = datamanager.load(username, host, "accounts");
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
106 if not account then
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
107 log("debug", "account not found for username '%s' at host '%s'", username, module.host);
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
108 return nil, "Auth failed. Invalid username";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
109 end
3161
3c46cb94caed Add mechanism for upgrading to hashed passwords from default. Remove some extra debug.
Jeff Mitchell <jeff@jefferai.org>
parents: 3159
diff changeset
110 if (account.hashpass == nil or string.len(account.hashpass) == 0) and (account.password == nil or string.len(account.password) == 0) then
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
111 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host);
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
112 return nil, "Auth failed. Password invalid.";
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
113 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
114 return true;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
115 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
116
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
117 function provider.create_user(username, password)
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
118 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
119 local salt = generate_uuid();
3205
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
120 local valid, stored_key, server_key = saltedPasswordSHA1(password, salt, iteration_count);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
121 local stored_key_hex = stored_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
122 local server_key_hex = server_key:gsub(".", function (c) return ("%02x"):format(c:byte()); end);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
123 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count});
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
124 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
125
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
126 function provider.get_sasl_handler()
3191
a475fbce1990 mod_auth_internal, mod_auth_internal_hashed: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents: 3190
diff changeset
127 local realm = module:get_option("sasl_realm") or module.host;
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
128 local testpass_authentication_profile = {
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
129 plain_test = function(username, password, realm)
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
130 local prepped_username = nodeprep(username);
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
131 if not prepped_username then
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
132 log("debug", "NODEprep failed on username: %s", username);
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
133 return "", nil;
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
134 end
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
135 return usermanager.test_password(prepped_username, password, realm), true;
3193
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
136 end,
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
137 scram_sha_1 = function(username, realm)
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
138 local credentials = datamanager.load(username, host, "accounts") or {};
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
139 if credentials.password then
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
140 usermanager.set_password(username, credentials.password);
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
141 credentials = datamanager.load(username, host, "accounts") or {};
d686abb6b069 mod_auth_internal_hashed: Added SCRAM-SHA-1 support for SASL.
Waqas Hussain <waqas20@gmail.com>
parents: 3191
diff changeset
142 end
3211
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
143
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
144 -- convert hexpass to stored_key and server_key
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
145 -- TODO: remove this in near future
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
146 if credentials.hashpass then
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
147 local salted_password = credentials.hashpass:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
148 credentials.stored_key = sha1(hmac_sha1(salted_password, "Client Key")):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
149 credentials.server_key = hmac_sha1(salted_password, "Server Key"):gsub(".", function (c) return ("%02x"):format(c:byte()); end);
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
150 end
d69e90ffbc09 mod_auth_internal_hashed: Convert hashpass to server_key/stored_key on SCRAM-SHA-1 login.
Tobias Markmann <tm@ayena.de>
parents: 3210
diff changeset
151
3205
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
152 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt;
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
153 stored_key = stored_key and stored_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
154 server_key = server_key and server_key:gsub("..", function(x) return string.char(tonumber(x, 16)); end);
2dcd826bbbc6 mod_auth_internal_hashed: Store StoredKey and ServerKey instead of salted hashed password.
Tobias Markmann <tm@ayena.de>
parents: 3193
diff changeset
155 return stored_key, server_key, iteration_count, salt, true;
3190
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
156 end
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
157 };
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
158 return new_sasl(realm, testpass_authentication_profile);
3159
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
159 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
160
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
161 function provider.is_admin(jid)
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
162 local admins = config.get(host, "core", "admins");
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
163 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
164 jid = jid_bare(jid);
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
165 for _,admin in ipairs(admins) do
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
166 if admin == jid then return true; end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
167 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
168 elseif admins then
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
169 log("error", "Option 'admins' for host '%s' is not a table", host);
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
170 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
171 return is_admin(jid); -- Test whether it's a global admin instead
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
172 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
173 return provider;
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
174 end
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
175
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
176 module:add_item("auth-provider", new_hashpass_provider(module.host));
db9def53fe9c Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
177

mercurial