Software / code / prosody
Comparison
plugins/mod_auth_internal_hashed.lua @ 5116:5f9066db1b4d
mod_auth_internal_hashed: Get rid of useless wrapper function new_hashpass_provider.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Wed, 12 Sep 2012 21:40:00 +0500 |
| parent | 4764:0df5b2d5dff3 |
| child | 5117:2c7e1ce8f482 |
comparison
equal
deleted
inserted
replaced
| 5115:3939960b3c07 | 5116:5f9066db1b4d |
|---|---|
| 37 | 37 |
| 38 | 38 |
| 39 -- Default; can be set per-user | 39 -- Default; can be set per-user |
| 40 local iteration_count = 4096; | 40 local iteration_count = 4096; |
| 41 | 41 |
| 42 function new_hashpass_provider(host) | 42 local host = module.host; |
| 43 local provider = { name = "internal_hashed" }; | 43 -- define auth provider |
| 44 log("debug", "initializing internal_hashed authentication provider for host '%s'", host); | 44 local provider = { name = "internal_hashed" }; |
| 45 log("debug", "initializing internal_hashed authentication provider for host '%s'", host); | |
| 45 | 46 |
| 46 function provider.test_password(username, password) | 47 function provider.test_password(username, password) |
| 47 local credentials = datamanager.load(username, host, "accounts") or {}; | 48 local credentials = datamanager.load(username, host, "accounts") or {}; |
| 48 | |
| 49 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then | |
| 50 if credentials.password ~= password then | |
| 51 return nil, "Auth failed. Provided password is incorrect."; | |
| 52 end | |
| 53 | 49 |
| 54 if provider.set_password(username, credentials.password) == nil then | 50 if credentials.password ~= nil and string.len(credentials.password) ~= 0 then |
| 55 return nil, "Auth failed. Could not set hashed password from plaintext."; | 51 if credentials.password ~= password then |
| 56 else | 52 return nil, "Auth failed. Provided password is incorrect."; |
| 57 return true; | |
| 58 end | |
| 59 end | 53 end |
| 60 | 54 |
| 61 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then | 55 if provider.set_password(username, credentials.password) == nil then |
| 62 return nil, "Auth failed. Stored salt and iteration count information is not complete."; | 56 return nil, "Auth failed. Could not set hashed password from plaintext."; |
| 57 else | |
| 58 return true; | |
| 63 end | 59 end |
| 64 | 60 end |
| 65 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count); | 61 |
| 66 | 62 if credentials.iteration_count == nil or credentials.salt == nil or string.len(credentials.salt) == 0 then |
| 63 return nil, "Auth failed. Stored salt and iteration count information is not complete."; | |
| 64 end | |
| 65 | |
| 66 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, credentials.salt, credentials.iteration_count); | |
| 67 | |
| 68 local stored_key_hex = to_hex(stored_key); | |
| 69 local server_key_hex = to_hex(server_key); | |
| 70 | |
| 71 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then | |
| 72 return true; | |
| 73 else | |
| 74 return nil, "Auth failed. Invalid username, password, or password hash information."; | |
| 75 end | |
| 76 end | |
| 77 | |
| 78 function provider.set_password(username, password) | |
| 79 local account = datamanager.load(username, host, "accounts"); | |
| 80 if account then | |
| 81 account.salt = account.salt or generate_uuid(); | |
| 82 account.iteration_count = account.iteration_count or iteration_count; | |
| 83 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count); | |
| 67 local stored_key_hex = to_hex(stored_key); | 84 local stored_key_hex = to_hex(stored_key); |
| 68 local server_key_hex = to_hex(server_key); | 85 local server_key_hex = to_hex(server_key); |
| 69 | 86 |
| 70 if valid and stored_key_hex == credentials.stored_key and server_key_hex == credentials.server_key then | 87 account.stored_key = stored_key_hex |
| 71 return true; | 88 account.server_key = server_key_hex |
| 72 else | 89 |
| 73 return nil, "Auth failed. Invalid username, password, or password hash information."; | 90 account.password = nil; |
| 74 end | 91 return datamanager.store(username, host, "accounts", account); |
| 75 end | 92 end |
| 76 | 93 return nil, "Account not available."; |
| 77 function provider.set_password(username, password) | |
| 78 local account = datamanager.load(username, host, "accounts"); | |
| 79 if account then | |
| 80 account.salt = account.salt or generate_uuid(); | |
| 81 account.iteration_count = account.iteration_count or iteration_count; | |
| 82 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, account.salt, account.iteration_count); | |
| 83 local stored_key_hex = to_hex(stored_key); | |
| 84 local server_key_hex = to_hex(server_key); | |
| 85 | |
| 86 account.stored_key = stored_key_hex | |
| 87 account.server_key = server_key_hex | |
| 88 | |
| 89 account.password = nil; | |
| 90 return datamanager.store(username, host, "accounts", account); | |
| 91 end | |
| 92 return nil, "Account not available."; | |
| 93 end | |
| 94 | |
| 95 function provider.user_exists(username) | |
| 96 local account = datamanager.load(username, host, "accounts"); | |
| 97 if not account then | |
| 98 log("debug", "account not found for username '%s' at host '%s'", username, module.host); | |
| 99 return nil, "Auth failed. Invalid username"; | |
| 100 end | |
| 101 return true; | |
| 102 end | |
| 103 | |
| 104 function provider.create_user(username, password) | |
| 105 if password == nil then | |
| 106 return datamanager.store(username, host, "accounts", {}); | |
| 107 end | |
| 108 local salt = generate_uuid(); | |
| 109 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count); | |
| 110 local stored_key_hex = to_hex(stored_key); | |
| 111 local server_key_hex = to_hex(server_key); | |
| 112 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count}); | |
| 113 end | |
| 114 | |
| 115 function provider.delete_user(username) | |
| 116 return datamanager.store(username, host, "accounts", nil); | |
| 117 end | |
| 118 | |
| 119 function provider.get_sasl_handler() | |
| 120 local testpass_authentication_profile = { | |
| 121 plain_test = function(sasl, username, password, realm) | |
| 122 local prepped_username = nodeprep(username); | |
| 123 if not prepped_username then | |
| 124 log("debug", "NODEprep failed on username: %s", username); | |
| 125 return "", nil; | |
| 126 end | |
| 127 return usermanager.test_password(prepped_username, realm, password), true; | |
| 128 end, | |
| 129 scram_sha_1 = function(sasl, username, realm) | |
| 130 local credentials = datamanager.load(username, host, "accounts"); | |
| 131 if not credentials then return; end | |
| 132 if credentials.password then | |
| 133 usermanager.set_password(username, credentials.password, host); | |
| 134 credentials = datamanager.load(username, host, "accounts"); | |
| 135 if not credentials then return; end | |
| 136 end | |
| 137 | |
| 138 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt; | |
| 139 stored_key = stored_key and from_hex(stored_key); | |
| 140 server_key = server_key and from_hex(server_key); | |
| 141 return stored_key, server_key, iteration_count, salt, true; | |
| 142 end | |
| 143 }; | |
| 144 return new_sasl(module.host, testpass_authentication_profile); | |
| 145 end | |
| 146 | |
| 147 return provider; | |
| 148 end | 94 end |
| 149 | 95 |
| 150 module:add_item("auth-provider", new_hashpass_provider(module.host)); | 96 function provider.user_exists(username) |
| 97 local account = datamanager.load(username, host, "accounts"); | |
| 98 if not account then | |
| 99 log("debug", "account not found for username '%s' at host '%s'", username, host); | |
| 100 return nil, "Auth failed. Invalid username"; | |
| 101 end | |
| 102 return true; | |
| 103 end | |
| 151 | 104 |
| 105 function provider.create_user(username, password) | |
| 106 if password == nil then | |
| 107 return datamanager.store(username, host, "accounts", {}); | |
| 108 end | |
| 109 local salt = generate_uuid(); | |
| 110 local valid, stored_key, server_key = getAuthenticationDatabaseSHA1(password, salt, iteration_count); | |
| 111 local stored_key_hex = to_hex(stored_key); | |
| 112 local server_key_hex = to_hex(server_key); | |
| 113 return datamanager.store(username, host, "accounts", {stored_key = stored_key_hex, server_key = server_key_hex, salt = salt, iteration_count = iteration_count}); | |
| 114 end | |
| 115 | |
| 116 function provider.delete_user(username) | |
| 117 return datamanager.store(username, host, "accounts", nil); | |
| 118 end | |
| 119 | |
| 120 function provider.get_sasl_handler() | |
| 121 local testpass_authentication_profile = { | |
| 122 plain_test = function(sasl, username, password, realm) | |
| 123 local prepped_username = nodeprep(username); | |
| 124 if not prepped_username then | |
| 125 log("debug", "NODEprep failed on username: %s", username); | |
| 126 return "", nil; | |
| 127 end | |
| 128 return usermanager.test_password(prepped_username, realm, password), true; | |
| 129 end, | |
| 130 scram_sha_1 = function(sasl, username, realm) | |
| 131 local credentials = datamanager.load(username, host, "accounts"); | |
| 132 if not credentials then return; end | |
| 133 if credentials.password then | |
| 134 usermanager.set_password(username, credentials.password, host); | |
| 135 credentials = datamanager.load(username, host, "accounts"); | |
| 136 if not credentials then return; end | |
| 137 end | |
| 138 | |
| 139 local stored_key, server_key, iteration_count, salt = credentials.stored_key, credentials.server_key, credentials.iteration_count, credentials.salt; | |
| 140 stored_key = stored_key and from_hex(stored_key); | |
| 141 server_key = server_key and from_hex(server_key); | |
| 142 return stored_key, server_key, iteration_count, salt, true; | |
| 143 end | |
| 144 }; | |
| 145 return new_sasl(host, testpass_authentication_profile); | |
| 146 end | |
| 147 | |
| 148 module:add_item("auth-provider", provider); | |
| 149 |