Tue, 03 Aug 2010 10:58:15 +0100
sessionmanager: Remove dependency on eventmanager
3157 | 1 | -- Prosody IM |
2 | -- Copyright (C) 2008-2010 Matthew Wild | |
3 | -- Copyright (C) 2008-2010 Waqas Hussain | |
4 | -- | |
5 | -- This project is MIT/X11 licensed. Please see the | |
6 | -- COPYING file in the source package for more information. | |
7 | -- | |
8 | ||
9 | local datamanager = require "util.datamanager"; | |
3336
3a8ce659edfc
mod_auth_internal, usermanager: Rename to mod_auth_internal_plain, and update usermanager to still use it as the default
Matthew Wild <mwild1@gmail.com>
parents:
3335
diff
changeset
|
10 | local log = require "util.logger".init("auth_internal_plain"); |
3157 | 11 | local type = type; |
12 | local error = error; | |
13 | local ipairs = ipairs; | |
14 | local hashes = require "util.hashes"; | |
15 | local jid_bare = require "util.jid".bare; | |
16 | local config = require "core.configmanager"; | |
3158 | 17 | local usermanager = require "core.usermanager"; |
3190
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
18 | 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
|
19 | local nodeprep = require "util.encodings".stringprep.nodeprep; |
3157 | 20 | local hosts = hosts; |
21 | ||
22 | local prosody = _G.prosody; | |
23 | ||
3158 | 24 | local is_cyrus = usermanager.is_cyrus; |
25 | ||
3157 | 26 | function new_default_provider(host) |
3336
3a8ce659edfc
mod_auth_internal, usermanager: Rename to mod_auth_internal_plain, and update usermanager to still use it as the default
Matthew Wild <mwild1@gmail.com>
parents:
3335
diff
changeset
|
27 | local provider = { name = "internal_plain" }; |
3158 | 28 | log("debug", "initializing default authentication provider for host '%s'", host); |
29 | ||
3157 | 30 | function provider.test_password(username, password) |
3158 | 31 | log("debug", "test password '%s' for user %s at host %s", password, username, module.host); |
3157 | 32 | if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end |
33 | local credentials = datamanager.load(username, host, "accounts") or {}; | |
34 | ||
35 | if password == credentials.password then | |
36 | return true; | |
37 | else | |
38 | return nil, "Auth failed. Invalid username or password."; | |
39 | end | |
40 | end | |
41 | ||
42 | function provider.get_password(username) | |
3158 | 43 | log("debug", "get_password for username '%s' at host '%s'", username, module.host); |
3157 | 44 | if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
45 | return (datamanager.load(username, host, "accounts") or {}).password; | |
46 | end | |
47 | ||
48 | function provider.set_password(username, password) | |
49 | if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end | |
50 | local account = datamanager.load(username, host, "accounts"); | |
51 | if account then | |
52 | account.password = password; | |
53 | return datamanager.store(username, host, "accounts", account); | |
54 | end | |
55 | return nil, "Account not available."; | |
56 | end | |
57 | ||
58 | function provider.user_exists(username) | |
59 | if is_cyrus(host) then return true; end | |
60 | local account = datamanager.load(username, host, "accounts"); | |
61 | if not account then | |
3158 | 62 | log("debug", "account not found for username '%s' at host '%s'", username, module.host); |
3157 | 63 | return nil, "Auth failed. Invalid username"; |
64 | end | |
65 | return true; | |
66 | end | |
67 | ||
68 | function provider.create_user(username, password) | |
69 | if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end | |
70 | return datamanager.store(username, host, "accounts", {password = password}); | |
71 | end | |
72 | ||
3190
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
73 | 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
|
74 | 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
|
75 | local getpass_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
|
76 | plain = function(username, realm) |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
77 | 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
|
78 | 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
|
79 | 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
|
80 | 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
|
81 | end |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
82 | local password = usermanager.get_password(prepped_username, realm); |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
83 | if not password then |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
84 | 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
|
85 | end |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
86 | return password, true; |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
87 | end |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
88 | }; |
b5f261123013
mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents:
3180
diff
changeset
|
89 | return new_sasl(realm, getpass_authentication_profile); |
3157 | 90 | end |
3287
e425e27c12be
mod_auth_internal, mod_auth_internal_hashed: Remove is_admin method from providers
Matthew Wild <mwild1@gmail.com>
parents:
3272
diff
changeset
|
91 | |
3157 | 92 | return provider; |
93 | end | |
94 | ||
95 | module:add_item("auth-provider", new_default_provider(module.host)); | |
96 |