annotate mod_register_web/mod_register_web.lua @ 1459:742f3dc601b5

mod_register_web: Use path separator from package.config
author Kim Alvefur <zash@zash.se>
date Fri, 27 Jun 2014 20:57:51 +0200
parents e670d4cc5027
children 5e1f7af23cf0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local captcha_options = module:get_option("captcha_options", {});
746
03595194075a mod_register_web: nodeprep username before creating user (thanks IRON)
Matthew Wild <mwild1@gmail.com>
parents: 653
diff changeset
2 local nodeprep = require "util.encodings".stringprep.nodeprep;
1225
a3766d3baacb mod_register_web: Import usermanager and util.http into locals
Kim Alvefur <zash@zash.se>
parents: 1223
diff changeset
3 local usermanager = require "core.usermanager";
1247
34fbe58d19da mod_register_web: Use net.http instead of util.http, as we need to make requests (thanks dustin)
Matthew Wild <mwild1@gmail.com>
parents: 1239
diff changeset
4 local http = require "net.http";
1459
742f3dc601b5 mod_register_web: Use path separator from package.config
Kim Alvefur <zash@zash.se>
parents: 1320
diff changeset
5 local path_sep = package.config:sub(1,1);
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
1235
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
7 local extra_fields = {
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
8 nick = true; name = true; first = true; last = true; email = true;
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
9 address = true; city = true; state = true; zip = true;
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
10 phone = true; url = true; date = true;
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
11 }
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
12
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
13 function template(data)
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
14 -- Like util.template, but deals with plain text
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
15 return { apply = function(values) return (data:gsub("{([^}]+)}", values)); end }
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
16 end
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
17
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
18 local function get_template(name)
1459
742f3dc601b5 mod_register_web: Use path separator from package.config
Kim Alvefur <zash@zash.se>
parents: 1320
diff changeset
19 local fh = assert(module:load_resource("templates"..path_sep..name..".html"));
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
20 local data = assert(fh:read("*a"));
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
21 fh:close();
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
22 return template(data);
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
23 end
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
24
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
25 local function render(template, data)
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
26 return tostring(template.apply(data));
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
27 end
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
28
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
29 local register_tpl = get_template "register";
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
30 local success_tpl = get_template "success";
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
31
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
32 if next(captcha_options) ~= nil then
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
33 local recaptcha_tpl = get_template "recaptcha";
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
34
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
35 function generate_captcha(display_options)
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
36 return recaptcha_tpl.apply(setmetatable({
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
37 recaptcha_display_error = display_options and display_options.recaptcha_error
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
38 and ("&error="..display_options.recaptcha_error) or "";
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
39 }, {
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
40 __index = function (t, k)
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
41 if captcha_options[k] then return captcha_options[k]; end
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
42 module:log("error", "Missing parameter from captcha_options: %s", k);
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
43 end
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
44 }));
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
45 end
1234
da39ba4047a7 mod_register_web: Pass request to captcha handler
Kim Alvefur <zash@zash.se>
parents: 1233
diff changeset
46 function verify_captcha(request, form, callback)
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
47 http.request("https://www.google.com/recaptcha/api/verify", {
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
48 body = http.formencode {
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
49 privatekey = captcha_options.recaptcha_private_key;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
50 remoteip = request.conn:ip();
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
51 challenge = form.recaptcha_challenge_field;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
52 response = form.recaptcha_response_field;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
53 };
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
54 }, function (verify_result, code)
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
55 local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
56 if verify_ok == "true" then
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
57 callback(true);
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
58 else
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
59 callback(false, verify_err)
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
60 end
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
61 end);
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
62 end
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
63 else
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
64 module:log("debug", "No Recaptcha options set, using fallback captcha")
1231
502ce9672eae mod_register_web: Use local
Kim Alvefur <zash@zash.se>
parents: 1230
diff changeset
65 local random = math.random;
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
66 local hmac_sha1 = require "util.hashes".hmac_sha1;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
67 local secret = require "util.uuid".generate()
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
68 local ops = { '+', '-' };
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
69 local captcha_tpl = get_template "simplecaptcha";
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
70 function generate_captcha()
1231
502ce9672eae mod_register_web: Use local
Kim Alvefur <zash@zash.se>
parents: 1230
diff changeset
71 local op = ops[random(1, #ops)];
502ce9672eae mod_register_web: Use local
Kim Alvefur <zash@zash.se>
parents: 1230
diff changeset
72 local x, y = random(1, 9)
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
73 repeat
1231
502ce9672eae mod_register_web: Use local
Kim Alvefur <zash@zash.se>
parents: 1230
diff changeset
74 y = random(1, 9);
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
75 until x ~= y;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
76 local answer;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
77 if op == '+' then
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
78 answer = x + y;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
79 elseif op == '-' then
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
80 if x < y then
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
81 -- Avoid negative numbers
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
82 x, y = y, x;
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
83 end
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
84 answer = x - y;
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
85 end
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
86 local challenge = hmac_sha1(secret, answer, true);
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
87 return captcha_tpl.apply {
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
88 op = op, x = x, y = y, challenge = challenge;
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
89 };
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
90 end
1234
da39ba4047a7 mod_register_web: Pass request to captcha handler
Kim Alvefur <zash@zash.se>
parents: 1233
diff changeset
91 function verify_captcha(request, form, callback)
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
92 if hmac_sha1(secret, form.captcha_reply, true) == form.captcha_challenge then
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
93 callback(true);
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
94 else
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
95 callback(false, "Captcha verification failed");
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
96 end
1228
db85ff22ae97 mod_register_web: Add a simple fallback captcha
Kim Alvefur <zash@zash.se>
parents: 1227
diff changeset
97 end
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
98 end
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 function generate_page(event, display_options)
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local request = event.request;
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
102
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
103 return render(register_tpl, {
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
104 path = request.path; hostname = module.host;
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
105 notice = display_options and display_options.register_error or "";
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
106 captcha = generate_captcha(display_options);
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
107 })
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
1320
e670d4cc5027 mod_register_web: Pass request around so IP address can be reported
Kim Alvefur <zash@zash.se>
parents: 1247
diff changeset
110 function register_user(form, origin)
1229
12e3bc0fd6ed mod_register_web: Indentation fix
Kim Alvefur <zash@zash.se>
parents: 1228
diff changeset
111 local prepped_username = nodeprep(form.username);
1230
f7c561fbd5a6 mod_register_web: Friendly error messages if the username is taken or failed nodeprep
Kim Alvefur <zash@zash.se>
parents: 1229
diff changeset
112 if not prepped_username then
f7c561fbd5a6 mod_register_web: Friendly error messages if the username is taken or failed nodeprep
Kim Alvefur <zash@zash.se>
parents: 1229
diff changeset
113 return nil, "Username contains forbidden characters";
f7c561fbd5a6 mod_register_web: Friendly error messages if the username is taken or failed nodeprep
Kim Alvefur <zash@zash.se>
parents: 1229
diff changeset
114 end
1236
59332e0bfbdc mod_register_web: Check for empty username
Kim Alvefur <zash@zash.se>
parents: 1235
diff changeset
115 if #prepped_username == 0 then
59332e0bfbdc mod_register_web: Check for empty username
Kim Alvefur <zash@zash.se>
parents: 1235
diff changeset
116 return nil, "The username field was empty";
59332e0bfbdc mod_register_web: Check for empty username
Kim Alvefur <zash@zash.se>
parents: 1235
diff changeset
117 end
1229
12e3bc0fd6ed mod_register_web: Indentation fix
Kim Alvefur <zash@zash.se>
parents: 1228
diff changeset
118 if usermanager.user_exists(prepped_username, module.host) then
1230
f7c561fbd5a6 mod_register_web: Friendly error messages if the username is taken or failed nodeprep
Kim Alvefur <zash@zash.se>
parents: 1229
diff changeset
119 return nil, "Username already taken";
1229
12e3bc0fd6ed mod_register_web: Indentation fix
Kim Alvefur <zash@zash.se>
parents: 1228
diff changeset
120 end
1238
752285859607 mod_register_web: Use correct variables
Kim Alvefur <zash@zash.se>
parents: 1237
diff changeset
121 local registering = { username = prepped_username , host = module.host, allowed = true }
1237
c669cb78b293 mod_register_web: Fire user-registering event like mod_register
Kim Alvefur <zash@zash.se>
parents: 1236
diff changeset
122 module:fire_event("user-registering", registering);
c669cb78b293 mod_register_web: Fire user-registering event like mod_register
Kim Alvefur <zash@zash.se>
parents: 1236
diff changeset
123 if not registering.allowed then
c669cb78b293 mod_register_web: Fire user-registering event like mod_register
Kim Alvefur <zash@zash.se>
parents: 1236
diff changeset
124 return nil, "Registration not allowed";
c669cb78b293 mod_register_web: Fire user-registering event like mod_register
Kim Alvefur <zash@zash.se>
parents: 1236
diff changeset
125 end
1233
f7b1992e4ff6 mod_register_web: Add missing =
Kim Alvefur <zash@zash.se>
parents: 1232
diff changeset
126 local ok, err = usermanager.create_user(prepped_username, form.password, module.host);
1232
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
127 if ok then
1235
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
128 local extra_data = {};
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
129 for field in pairs(extra_fields) do
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
130 local field_value = form[field];
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
131 if field_value and #field_value > 0 then
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
132 extra_data[field] = field_value;
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
133 end
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
134 end
1238
752285859607 mod_register_web: Use correct variables
Kim Alvefur <zash@zash.se>
parents: 1237
diff changeset
135 if next(extra_data) ~= nil then
1235
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
136 datamanager.store(prepped_username, module.host, "account_details", extra_data);
9277e0a3922f mod_register_web: Support additional registration fields
Kim Alvefur <zash@zash.se>
parents: 1234
diff changeset
137 end
1232
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
138 module:fire_event("user-registered", {
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
139 username = prepped_username,
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
140 host = module.host,
1320
e670d4cc5027 mod_register_web: Pass request around so IP address can be reported
Kim Alvefur <zash@zash.se>
parents: 1247
diff changeset
141 source = module.name,
e670d4cc5027 mod_register_web: Pass request around so IP address can be reported
Kim Alvefur <zash@zash.se>
parents: 1247
diff changeset
142 ip = origin.conn:ip(),
1232
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
143 });
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
144 end
12f59489ef6e mod_register_web: Fire user-registered event
Kim Alvefur <zash@zash.se>
parents: 1231
diff changeset
145 return ok, err;
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 function generate_success(event, form)
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
149 return render(success_tpl, { jid = nodeprep(form.username).."@"..module.host });
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 function generate_register_response(event, form, ok, err)
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 local message;
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if ok then
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 return generate_success(event, form);
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 else
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 return generate_page(event, { register_error = err });
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 function handle_form(event)
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 local request, response = event.request, event.response;
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 local form = http.formdecode(request.body);
1234
da39ba4047a7 mod_register_web: Pass request to captcha handler
Kim Alvefur <zash@zash.se>
parents: 1233
diff changeset
164 verify_captcha(request, form, function (ok, err)
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
165 if ok then
1320
e670d4cc5027 mod_register_web: Pass request around so IP address can be reported
Kim Alvefur <zash@zash.se>
parents: 1247
diff changeset
166 local register_ok, register_err = register_user(form, request);
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 response:send(generate_register_response(event, form, register_ok, register_err));
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 else
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
169 response:send(generate_page(event, { register_error = err }));
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 end);
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 return true; -- Leave connection open until we respond above
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 module:provides("http", {
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 route = {
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 GET = generate_page;
1239
cc5cbeeb9fc7 mod_register_web: Handle URLs with a trailing slash
Kim Alvefur <zash@zash.se>
parents: 1238
diff changeset
178 ["GET /"] = generate_page;
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 POST = handle_form;
1239
cc5cbeeb9fc7 mod_register_web: Handle URLs with a trailing slash
Kim Alvefur <zash@zash.se>
parents: 1238
diff changeset
180 ["POST /"] = handle_form;
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 };
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 });