Annotate

plugins/mod_register_ibr.lua @ 9307:feaef6215bb8

util.stanza: Don't automatically generate ids for iq stanzas Users of this API should provide their own id attribute that meets their uniqueness requirements. The current implementation leaks information (i.e. how many iq stanzas have been sent by the server to other JIDs). Providing any strong guarantees of randomness here would need to pull in additional dependencies that we don't want in this simple library.
author Matthew Wild <mwild1@gmail.com>
date Thu, 13 Sep 2018 16:35:48 +0100
parent 8737:6d71845bf56f
child 9453:b6cdadb1175d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1189
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2448
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2448
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5763
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 691
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 691
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
9
60
44800be871f5 User registration, etc (jabber:iq:register)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local st = require "util.stanza";
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
11 local dataform_new = require "util.dataforms".new;
60
44800be871f5 User registration, etc (jabber:iq:register)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local usermanager_user_exists = require "core.usermanager".user_exists;
44800be871f5 User registration, etc (jabber:iq:register)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local usermanager_create_user = require "core.usermanager".create_user;
3996
7f35b292531b mod_register: Change to use new delete_user auth provider method
Matthew Wild <mwild1@gmail.com>
parents: 3995
diff changeset
14 local usermanager_delete_user = require "core.usermanager".delete_user;
927
cc180d25dbeb Fixed: mod_register: Node prepping was not being applied to usernames (part of issue #57)
Waqas Hussain <waqas20@gmail.com>
parents: 926
diff changeset
15 local nodeprep = require "util.encodings".stringprep.nodeprep;
3995
e504b06492c6 mod_register: Add registration_compat config option to allow account remove requests addressed to='host' (defaults to true)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
16
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
17 local additional_fields = module:get_option("additional_registration_fields", {});
8484
f591855f060d mod_register: Split into mod_register_ibr and mod_user_account_management (#723)
Kim Alvefur <zash@zash.se>
parents: 8464
diff changeset
18 local require_encryption = module:get_option_boolean("c2s_require_encryption",
f591855f060d mod_register: Split into mod_register_ibr and mod_user_account_management (#723)
Kim Alvefur <zash@zash.se>
parents: 8464
diff changeset
19 module:get_option_boolean("require_encryption", false));
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
20
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5371
diff changeset
21 local account_details = module:open_store("account_details");
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5371
diff changeset
22
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
23 local field_map = {
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
24 username = { name = "username", type = "text-single", label = "Username", required = true };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
25 password = { name = "password", type = "text-private", label = "Password", required = true };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
26 nick = { name = "nick", type = "text-single", label = "Nickname" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
27 name = { name = "name", type = "text-single", label = "Full Name" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
28 first = { name = "first", type = "text-single", label = "Given Name" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
29 last = { name = "last", type = "text-single", label = "Family Name" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
30 email = { name = "email", type = "text-single", label = "Email" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
31 address = { name = "address", type = "text-single", label = "Street" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
32 city = { name = "city", type = "text-single", label = "City" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
33 state = { name = "state", type = "text-single", label = "State" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
34 zip = { name = "zip", type = "text-single", label = "Postal code" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
35 phone = { name = "phone", type = "text-single", label = "Telephone number" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
36 url = { name = "url", type = "text-single", label = "Webpage" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
37 date = { name = "date", type = "text-single", label = "Birth date" };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
38 };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
39
7812
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
40 local title = module:get_option_string("registration_title",
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
41 "Creating a new account");
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
42 local instructions = module:get_option_string("registration_instructions",
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
43 "Choose a username and password for use with this service.");
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
44
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
45 local registration_form = dataform_new{
7812
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
46 title = title;
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
47 instructions = instructions;
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
48
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
49 field_map.username;
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
50 field_map.password;
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
51 };
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
52
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
53 local registration_query = st.stanza("query", {xmlns = "jabber:iq:register"})
7812
2bc339352dcd mod_register: Allow 'title' and 'instructions' fields to be customized
Kim Alvefur <zash@zash.se>
parents: 7754
diff changeset
54 :tag("instructions"):text(instructions):up()
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
55 :tag("username"):up()
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
56 :tag("password"):up();
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
57
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
58 for _, field in ipairs(additional_fields) do
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
59 if type(field) == "table" then
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
60 registration_form[#registration_form + 1] = field;
7816
2624f4ee34a2 mod_register: Fix syntax errors
Kim Alvefur <zash@zash.se>
parents: 7815
diff changeset
61 elseif field_map[field] or field_map[field:sub(1, -2)] then
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
62 if field:match("%+$") then
7814
2120d71b0d56 mod_register: Strip '+' char from field names without using length
Kim Alvefur <zash@zash.se>
parents: 7812
diff changeset
63 field = field:sub(1, -2);
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
64 field_map[field].required = true;
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
65 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
66
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
67 registration_form[#registration_form + 1] = field_map[field];
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
68 registration_query:tag(field):up();
7815
f8d25a2e80ea mod_register: Verify that fields are known to prevent traceback
Kim Alvefur <zash@zash.se>
parents: 7814
diff changeset
69 else
f8d25a2e80ea mod_register: Verify that fields are known to prevent traceback
Kim Alvefur <zash@zash.se>
parents: 7814
diff changeset
70 module:log("error", "Unknown field %q", field);
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
71 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
72 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
73 registration_query:add_child(registration_form:form());
60
44800be871f5 User registration, etc (jabber:iq:register)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74
4268
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
75 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up();
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
76 module:hook("stream-features", function(event)
5707
36a289e9244c mod_register: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 5637
diff changeset
77 local session, features = event.origin, event.features;
4268
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
78
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
79 -- Advertise registration to unauthorized clients only.
8484
f591855f060d mod_register: Split into mod_register_ibr and mod_user_account_management (#723)
Kim Alvefur <zash@zash.se>
parents: 8464
diff changeset
80 if session.type ~= "c2s_unauthed" or (require_encryption and not session.secure) then
4268
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
81 return
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
82 end
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
83
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
84 features:add_child(register_stream_feature);
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
85 end);
c249f10eb9bb Advertise in-band registration support.
Glenn Maynard <glenn@zewt.org>
parents: 3997
diff changeset
86
4398
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
87 local function parse_response(query)
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
88 local form = query:get_child("x", "jabber:x:data");
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
89 if form then
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
90 return registration_form:data(form);
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
91 else
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
92 local data = {};
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
93 local errors = {};
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
94 for _, field in ipairs(registration_form) do
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
95 local name, required = field.name, field.required;
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
96 if field_map[name] then
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
97 data[name] = query:get_child_text(name);
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
98 if (not data[name] or #data[name] == 0) and required then
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
99 errors[name] = "Required value missing";
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
100 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
101 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
102 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
103 if next(errors) then
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
104 return data, errors;
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
105 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
106 return data;
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
107 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
108 end
acc37e221940 mod_register: Add support for additional registration fields
Florian Zeitz <florob@babelmonkeys.de>
parents: 4270
diff changeset
109
8194
ba9cd8447578 mod_register: Add comments saying which section handles password change, account deletion and which is in-band registration
Kim Alvefur <zash@zash.se>
parents: 8192
diff changeset
110 -- In-band registration
3529
3f9cc12308aa mod_register: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3394
diff changeset
111 module:hook("stanza/iq/jabber:iq:register:query", function(event)
3f9cc12308aa mod_register: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3394
diff changeset
112 local session, stanza = event.origin, event.stanza;
7017
ff734a602886 mod_register: Use session log instance to ease indentification
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
113 local log = session.log or module._log;
3529
3f9cc12308aa mod_register: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3394
diff changeset
114
8484
f591855f060d mod_register: Split into mod_register_ibr and mod_user_account_management (#723)
Kim Alvefur <zash@zash.se>
parents: 8464
diff changeset
115 if session.type ~= "c2s_unauthed" then
7709
0af1783d1592 mod_register: Additional logging for various registration failure cases
Kim Alvefur <zash@zash.se>
parents: 7570
diff changeset
116 log("debug", "Attempted registration when disabled or already authenticated");
665
09e0e9c722a3 Add allow_registration option to disable account registration
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
117 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
8736
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
118 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
119 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
120
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
121 if require_encryption and not session.secure then
7916
72b6d5ab4137 mod_register: Require encryption before registration if c2s_require_encryption is set (fixes #595)
Kim Alvefur <zash@zash.se>
parents: 5637
diff changeset
122 session.send(st.error_reply(stanza, "modify", "policy-violation", "Encryption is required"));
8736
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
123 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
124 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
125
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
126 local query = stanza.tags[1];
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
127 if stanza.attr.type == "get" then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
128 local reply = st.reply(stanza);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
129 reply:add_child(registration_query);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
130 session.send(reply);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
131 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
132 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
133
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
134 -- stanza.attr.type == "set"
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
135 if query.tags[1] and query.tags[1].name == "remove" then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
136 session.send(st.error_reply(stanza, "auth", "registration-required"));
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
137 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
138 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
139
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
140 local data, errors = parse_response(query);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
141 if errors then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
142 log("debug", "Error parsing registration form:");
8737
6d71845bf56f mod_register_ibr: Return a textual error to the user for problems with parsing form data
Kim Alvefur <zash@zash.se>
parents: 8736
diff changeset
143 local textual_errors = {};
8736
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
144 for field, err in pairs(errors) do
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
145 log("debug", "Field %q: %s", field, err);
8737
6d71845bf56f mod_register_ibr: Return a textual error to the user for problems with parsing form data
Kim Alvefur <zash@zash.se>
parents: 8736
diff changeset
146 table.insert(textual_errors, ("%s: %s"):format(field:gsub("^%a", string.upper), err));
8736
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
147 end
8737
6d71845bf56f mod_register_ibr: Return a textual error to the user for problems with parsing form data
Kim Alvefur <zash@zash.se>
parents: 8736
diff changeset
148 session.send(st.error_reply(stanza, "modify", "not-acceptable", table.concat(textual_errors, "\n")));
8736
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
149 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
150 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
151
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
152 local username, password = nodeprep(data.username), data.password;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
153 data.username, data.password = nil, nil;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
154 local host = module.host;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
155 if not username or username == "" then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
156 log("debug", "The requested username is invalid.");
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
157 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
158 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
159 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
160
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
161 local user = { username = username , host = host, additional = data, ip = session.ip, session = session, allowed = true }
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
162 module:fire_event("user-registering", user);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
163 if not user.allowed then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
164 log("debug", "Registration disallowed by module: %s", user.reason or "no reason given");
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
165 session.send(st.error_reply(stanza, "modify", "not-acceptable", user.reason));
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
166 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
167 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
168
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
169 if usermanager_user_exists(username, host) then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
170 log("debug", "Attempt to register with existing username");
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
171 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
172 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
173 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
174
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
175 -- TODO unable to write file, file may be locked, etc, what's the correct error?
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
176 local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
177 if usermanager_create_user(username, password, host) then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
178 data.registered = os.time();
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
179 if not account_details:set(username, data) then
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
180 log("debug", "Could not store extra details");
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
181 usermanager_delete_user(username, host);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
182 session.send(error_reply);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
183 return true;
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
184 end
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
185 session.send(st.reply(stanza)); -- user created!
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
186 log("info", "User account created: %s@%s", username, host);
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
187 module:fire_event("user-registered", {
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
188 username = username, host = host, source = "mod_register",
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
189 session = session });
3529
3f9cc12308aa mod_register: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3394
diff changeset
190 else
8736
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
191 log("debug", "Could not create user");
a071c203a1a0 mod_register_ibr: Reshape the code using early returns to reduce needless indentation
Kim Alvefur <zash@zash.se>
parents: 8583
diff changeset
192 session.send(error_reply);
3529
3f9cc12308aa mod_register: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3394
diff changeset
193 end
3f9cc12308aa mod_register: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3394
diff changeset
194 return true;
60
44800be871f5 User registration, etc (jabber:iq:register)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 end);