Annotate

plugins/mod_auth_internal_plain.lua @ 5115:3939960b3c07

mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 12 Sep 2012 21:32:12 +0500
parent 4762:943f9f860ab4
child 5117:2c7e1ce8f482
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
1 -- Prosody IM
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
4 --
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
7 --
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
8
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
9 local datamanager = require "util.datamanager";
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
10 local usermanager = require "core.usermanager";
3186
b5f261123013 mod_auth_internal, mod_auth_internal_hashed: Updated to provide get_sasl_handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3180
diff changeset
11 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
12 local nodeprep = require "util.encodings".stringprep.nodeprep;
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
13
4762
943f9f860ab4 mod_auth_internal_plain: Remove unused imports
Matthew Wild <mwild1@gmail.com>
parents: 4603
diff changeset
14 local log = module._log;
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
15 local host = module.host;
3163
a23168cc4af5 Working defaultauth
Jeff Mitchell <jeff@jefferai.org>
parents: 3162
diff changeset
16
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
17 -- define auth provider
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
18 local provider = { name = "internal_plain" };
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
19 log("debug", "initializing internal_plain authentication provider for host '%s'", host);
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
20
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
21 function provider.test_password(username, password)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
22 log("debug", "test password '%s' for user %s at host %s", password, username, host);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
23 local credentials = datamanager.load(username, host, "accounts") or {};
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
24
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
25 if password == credentials.password then
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
26 return true;
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
27 else
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
28 return nil, "Auth failed. Invalid username or password.";
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
29 end
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
30 end
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
31
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
32 function provider.get_password(username)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
33 log("debug", "get_password for username '%s' at host '%s'", username, host);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
34 return (datamanager.load(username, host, "accounts") or {}).password;
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
35 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
36
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
37 function provider.set_password(username, password)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
38 local account = datamanager.load(username, host, "accounts");
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
39 if account then
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
40 account.password = password;
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
41 return datamanager.store(username, host, "accounts", account);
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
42 end
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
43 return nil, "Account not available.";
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
44 end
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
45
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
46 function provider.user_exists(username)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
47 local account = datamanager.load(username, host, "accounts");
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
48 if not account then
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
49 log("debug", "account not found for username '%s' at host '%s'", username, host);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
50 return nil, "Auth failed. Invalid username";
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
51 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
52 return true;
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
53 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
54
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
55 function provider.create_user(username, password)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
56 return datamanager.store(username, host, "accounts", {password = password});
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
57 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
58
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
59 function provider.delete_user(username)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
60 return datamanager.store(username, host, "accounts", nil);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
61 end
3162
f246719abcd2 Added mod_auth_default
Jeff Mitchell <jeff@jefferai.org>
parents:
diff changeset
62
5115
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
63 function provider.get_sasl_handler()
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
64 local getpass_authentication_profile = {
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
65 plain = function(sasl, username, realm)
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
66 local prepped_username = nodeprep(username);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
67 if not prepped_username then
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
68 log("debug", "NODEprep failed on username: %s", username);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
69 return "", nil;
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
70 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
71 local password = usermanager.get_password(prepped_username, realm);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
72 if not password then
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
73 return "", nil;
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
74 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
75 return password, true;
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
76 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
77 };
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
78 return new_sasl(host, getpass_authentication_profile);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
79 end
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
80
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
81 module:add_item("auth-provider", provider);
3939960b3c07 mod_auth_{internal_plain,cyrus,anonymous}: Get rid of useless wrapper function new_default_provider.
Waqas Hussain <waqas20@gmail.com>
parents: 4762
diff changeset
82