annotate mod_captcha_registration/modules/mod_register.lua @ 5405:c7a5caad28ef

mod_http_oauth2: Enforce response type encoded in client_id The client promises to only use this response type, so we should hold them to that. This makes it fail earlier if the response type is disabled or the client is trying to use one that it promised not to use. Better than failing after login and consent.
author Kim Alvefur <zash@zash.se>
date Tue, 02 May 2023 16:31:25 +0200
parents 11fdfd73a527
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1373
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
1 -- Prosody IM
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
4 -- Modifications copyright (C) 2014 mrDoctorWho
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
5 --
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
8 --
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
9
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
10
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
11 local st = require "util.stanza";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
12 local dataform_new = require "util.dataforms".new;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
13 local usermanager_user_exists = require "core.usermanager".user_exists;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
14 local usermanager_create_user = require "core.usermanager".create_user;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
15 local usermanager_set_password = require "core.usermanager".set_password;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
16 local usermanager_delete_user = require "core.usermanager".delete_user;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
17 local os_time = os.time;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
18 local nodeprep = require "util.encodings".stringprep.nodeprep;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
19 local jid_bare = require "util.jid".bare;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
20 local timer = require "util.timer";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
21
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
22
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
23 local math = require "math";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
24 local captcha = require "captcha";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
25
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
26
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
27
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
28 local base64 = require "util.encodings".base64.encode;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
29 local sha1 = require "util.hashes".sha1;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
30
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
31
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
32 local captcha_ids = {};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
33
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
34 local config = module:get_option("captcha_config") or {};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
35
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
36
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
37 local compat = module:get_option_boolean("registration_compat", true);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
38 local allow_registration = module:get_option_boolean("allow_registration", false);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
39 local additional_fields = module:get_option("additional_registration_fields", {});
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
40
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
41 local account_details = module:open_store("account_details");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
42
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
43 local field_map = {
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
44 username = { name = "username", type = "text-single", label = "Username", required = true };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
45 password = { name = "password", type = "text-private", label = "Password", required = true };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
46 nick = { name = "nick", type = "text-single", label = "Nickname" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
47 name = { name = "name", type = "text-single", label = "Full Name" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
48 first = { name = "first", type = "text-single", label = "Given Name" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
49 last = { name = "last", type = "text-single", label = "Family Name" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
50 email = { name = "email", type = "text-single", label = "Email" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
51 address = { name = "address", type = "text-single", label = "Street" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
52 city = { name = "city", type = "text-single", label = "City" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
53 state = { name = "state", type = "text-single", label = "State" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
54 zip = { name = "zip", type = "text-single", label = "Postal code" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
55 phone = { name = "phone", type = "text-single", label = "Telephone number" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
56 url = { name = "url", type = "text-single", label = "Webpage" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
57 date = { name = "date", type = "text-single", label = "Birth date" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
58
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
59 -- something new
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
60 formtype = { name = "FORM_TYPE", type = "hidden"};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
61 captcha_text = { name = "captcha_text", type = "fixed", label = "Warning: "};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
62 captcha_psi = { name = "captchahidden", type = "hidden" }; -- Don't know exactly why, but it exists in ejabberd register form
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
63 captcha_url = { name = "url", type = "text-single", label = "Captcha url"};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
64 from = { name = "from", type = "hidden" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
65 captcha_challenge = { name = "challenge", type = "hidden" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
66 sid = { name = "sid", type = "hidden" };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
67 ocr = { name = "ocr", label = "Enter shown text", required = true, type = "media" }
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
68 };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
69
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
70 local registration_form = dataform_new{
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
71
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
72 field_map.formtype;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
73 field_map.username;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
74 field_map.password;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
75 field_map.captcha_text;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
76 -- field_map.captcha_psi; -- Maybe later, i really have no idea why it used in ejabberd reg form
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
77 field_map.captcha_url;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
78 field_map.from;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
79 field_map.captcha_challenge;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
80 field_map.sid;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
81 field_map.ocr;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
82 };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
83
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
84
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
85 function delete_captcha(cid)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
86 os.remove(string.format("%s/%s.png", config.dir, cid))
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
87 captcha_ids[cid] = nil;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
88 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
89
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
90 for _, field in ipairs(additional_fields) do
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
91 if type(field) == "table" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
92 registration_form[#registration_form + 1] = field;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
93 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
94 if field:match("%+$") then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
95 field = field:sub(1, #field - 1);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
96 field_map[field].required = true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
97 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
98
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
99 registration_form[#registration_form + 1] = field_map[field];
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
100 registration_query:tag(field):up();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
101 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
102 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
103
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
104 module:add_feature("jabber:iq:register");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
105
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
106 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
107 module:hook("stream-features", function(event)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
108 local session, features = event.origin, event.features;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
109
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
110 -- Advertise registration to unauthorized clients only.
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
111 if not(allow_registration) or session.type ~= "c2s_unauthed" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
112 return
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
113 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
114
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
115 features:add_child(register_stream_feature);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
116 end);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
117
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
118 local function handle_registration_stanza(event)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
119 local session, stanza = event.origin, event.stanza;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
120
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
121 local query = stanza.tags[1];
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
122 if stanza.attr.type == "get" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
123 local reply = st.reply(stanza);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
124 reply:tag("query", {xmlns = "jabber:iq:register"})
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
125 :tag("registered"):up()
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
126 :tag("username"):text(session.username):up()
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
127 :tag("password"):up();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
128 session.send(reply);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
129 else -- stanza.attr.type == "set"
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
130 if query.tags[1] and query.tags[1].name == "remove" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
131 local username, host = session.username, session.host;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
132
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
133 local old_session_close = session.close;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
134 session.close = function(session, ...)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
135 session.send(st.reply(stanza));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
136 return old_session_close(session, ...);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
137 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
138
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
139 local ok, err = usermanager_delete_user(username, host);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
140
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
141 if not ok then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
142 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
143 session.close = old_session_close;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
144 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
145 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
146 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
147
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
148 module:log("info", "User removed their account: %s@%s", username, host);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
149 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
150 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
151 local username = nodeprep(query:get_child("username"):get_text());
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
152 local password = query:get_child("password"):get_text();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
153 if username and password then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
154 if username == session.username then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
155 if usermanager_set_password(username, password, session.host) then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
156 session.send(st.reply(stanza));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
157 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
158 -- TODO unable to write file, file may be locked, etc, what's the correct error?
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
159 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
160 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
161 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
162 session.send(st.error_reply(stanza, "modify", "bad-request"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
163 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
164 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
165 session.send(st.error_reply(stanza, "modify", "bad-request"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
166 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
167 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
168 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
169 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
170 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
171
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
172 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
173 if compat then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
174 module:hook("iq/host/jabber:iq:register:query", function (event)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
175 local session, stanza = event.origin, event.stanza;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
176 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
177 return handle_registration_stanza(event);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
178 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
179 end);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
180 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
181
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
182 local function parse_response(query)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
183 local form = query:get_child("x", "jabber:x:data");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
184 if form then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
185 return registration_form:data(form);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
186 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
187 local data = {};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
188 local errors = {};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
189 for _, field in ipairs(registration_form) do
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
190 local name, required = field.name, field.required;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
191 if field_map[name] then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
192 data[name] = query:get_child_text(name);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
193 if (not data[name] or #data[name] == 0) and required then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
194 errors[name] = "Required value missing";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
195 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
196 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
197 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
198 if next(errors) then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
199 return data, errors;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
200 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
201 return data;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
202 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
203 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
204
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
205 local recent_ips = {};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
206 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
207 local whitelist_only = module:get_option("whitelist_registration_only");
2728
11fdfd73a527 mod_captcha_registration: Add ::1 to the default registration_whitelist.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1373
diff changeset
208 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1", "::1" };
1373
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
209 local blacklisted_ips = module:get_option("registration_blacklist") or {};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
210
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
211 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
212 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
213
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
214
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
215 local function get_file(name)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
216 local file = io.open(name, "rb")
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
217 local data = file:read("*all")
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
218 file:close()
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
219 return data
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
220 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
221
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
222
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
223 local function get_captcha()
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
224 local cap = captcha.new();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
225 math.randomseed(os_time());
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
226 local cid = tostring(math.random(1000, 90000)); -- random cid used for cap name
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
227 cap:font(config.font);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
228 cap:scribble();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
229 captcha_ids[cid] = cap:write(string.format("%s/%s.png", config.dir, cid)):lower();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
230 timer.add_task(config.timeout, function() delete_captcha(cid) end); -- Add new function to use arguments. Is there any other way in lua? Or it even works?
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
231 return cid
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
232 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
233
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
234
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
235
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
236 module:hook("stanza/iq/jabber:iq:register:query", function(event)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
237 local session, stanza = event.origin, event.stanza;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
238
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
239 if not(allow_registration) or session.type ~= "c2s_unauthed" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
240 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
241 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
242 local query = stanza.tags[1];
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
243 if stanza.attr.type == "get" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
244 local reply = st.reply(stanza):query("jabber:iq:register");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
245 -- TODO: Move this in standalone function
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
246 local challenge = get_captcha()
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
247 local captcha_data = get_file(config.dir.."/"..challenge..".png")
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
248 local captcha_sha = sha1(captcha_data, true) -- omg
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
249 local captcha_base64 = base64(captcha_data) -- lol wut
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
250 xml = registration_form:form(({FORM_TYPE = "urn:xmpp:captcha",
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
251 from = session.host,
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
252 ocr = {{
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
253 type = "image/png",
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
254 uri = string.format("cid:sha1+%s@bob.xmpp.org", captcha_sha)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
255 }};
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
256 url = string.format("http://%s:5280/%s/%s", session.host, config.web_path, challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
257 captcha_text = "If you can't see an image, follow link below";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
258 challenge = challenge;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
259 sid = "1";
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
260 }));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
261
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
262 data = st.stanza("data",
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
263 {xmlns = "urn:xmpp:bob",
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
264 cid = string.format("sha1+%s@bob.xmpp.org", captcha_sha),
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
265 type = "image/png",
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
266 ["max-age"] = config.timeout})
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
267 :text(captcha_base64);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
268
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
269 reply = reply:add_child(xml);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
270 reply = reply:add_child(data);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
271 session.send(reply);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
272
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
273 elseif stanza.attr.type == "set" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
274 if query.tags[1] and query.tags[1].name == "remove" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
275 session.send(st.error_reply(stanza, "auth", "registration-required"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
276 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
277 local data, errors = parse_response(query);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
278 if errors then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
279 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
280 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
281 -- Check that the user is not blacklisted or registering too often
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
282 if not session.ip then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
283 module:log("debug", "User's IP not known; can't apply blacklist/whitelist");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
284 elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
285 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
286 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
287 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
288 if not recent_ips[session.ip] then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
289 recent_ips[session.ip] = { time = os_time(), count = 1 };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
290 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
291 local ip = recent_ips[session.ip];
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
292 ip.count = ip.count + 1;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
293
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
294 if os_time() - ip.time < min_seconds_between_registrations then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
295 ip.time = os_time();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
296 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
297 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
298 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
299 ip.time = os_time();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
300 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
301 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
302 local host = module.host;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
303 local ocr = data.ocr:lower();
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
304 local challenge = data.challenge;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
305 local username, password = nodeprep(data.username), data.password;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
306 data.username, data.password = nil, nil;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
307
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
308 if challenge == nil or captcha_ids[challenge] == nil then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
309 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Captcha id is invalid or it has expired"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
310 delete_captcha(challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
311 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
312 elseif ocr ~= captcha_ids[challenge] then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
313 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Invalid captcha text"));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
314 delete_captcha(challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
315 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
316 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
317 if not username or username == "" then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
318 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
319 delete_captcha(challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
320 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
321 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
322 local user = { username = username , host = host, allowed = true }
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
323 module:fire_event("user-registering", user);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
324 if not user.allowed then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
325 delete_captcha(challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
326 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is forbidden."));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
327 elseif usermanager_user_exists(username, host) then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
328 delete_captcha(challenge)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
329 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
330 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
331 -- TODO unable to write file, file may be locked, etc, what's the correct error?
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
332 local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
333 if usermanager_create_user(username, password, host) then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
334 if next(data) and not account_details:set(username, data) then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
335 delete_captcha(challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
336 usermanager_delete_user(username, host);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
337 session.send(error_reply);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
338 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
339 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
340 session.send(st.reply(stanza)); -- user created!
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
341 module:log("info", "User account created: %s@%s", username, host);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
342 module:fire_event("user-registered", {
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
343 username = username, host = host, source = "mod_register",
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
344 session = session });
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
345 else
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
346 delete_captcha(challenge);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
347 session.send(error_reply);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
348 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
349 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
350 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
351 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
352 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
353 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
354 return true;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
355 end);
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
356
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
357 function string:split(sep)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
358 local sep, fields = sep or ":", {}
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
359 local pattern = string.format("([^%s]+)", sep)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
360 self:gsub(pattern, function(c) fields[#fields+1] = c end)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
361 return fields
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
362 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
363
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
364
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
365 function handle_http_request(event)
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
366 local request = event.request;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
367 local path = request.path;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
368 local cid = path:split("/")[2];
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
369 if cid == nil or captcha_ids[cid] == nil then
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
370 return nil;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
371 end
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
372 request.response = {
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
373 status_code = 200;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
374 headers = {
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
375 content_type = "image/png"
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
376 };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
377 body = get_file(string.format("%s/%s.png", config.dir, cid));
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
378 };
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
379 return request.response;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
380
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
381
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
382 end;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
383
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
384 module:provides("http", {
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
385 default_path = "/"..config.web_path;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
386 route = {
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
387 ["GET /*"] = handle_http_request;
985bfc6e8cad mod_captcha_registration: initial commit
mrDoctorWho <mrdoctorwho@gmail.com>
parents:
diff changeset
388 };
2728
11fdfd73a527 mod_captcha_registration: Add ::1 to the default registration_whitelist.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1373
diff changeset
389 });