annotate mod_register_web/mod_register_web.lua @ 1227:6015434f0e05

mod_register_web: Move HTML into separate template files
author Kim Alvefur <zash@zash.se>
date Sat, 30 Nov 2013 18:57:54 +0100
parents 0667624637da
children db85ff22ae97
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";
a3766d3baacb mod_register_web: Import usermanager and util.http into locals
Kim Alvefur <zash@zash.se>
parents: 1223
diff changeset
4 local http = require "util.http";
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
6 function template(data)
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
7 -- 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
8 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
9 end
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
10
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
11 local function get_template(name)
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
12 local fh = assert(module:load_resource("templates/"..name..".html"));
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
13 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
14 fh:close();
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
15 return template(data);
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 render(template, data)
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
19 return tostring(template.apply(data));
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
20 end
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
21
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
22 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
23 local success_tpl = get_template "success";
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
24 local recaptcha_tpl = get_template "recaptcha";
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
25
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 function generate_captcha(display_options)
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
27 return recaptcha_tpl.apply(setmetatable({
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 recaptcha_display_error = display_options and display_options.recaptcha_error
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 and ("&error="..display_options.recaptcha_error) or "";
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 }, {
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 __index = function (t, k)
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if captcha_options[k] then return captcha_options[k]; end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 module:log("error", "Missing parameter from captcha_options: %s", k);
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
34 end
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
35 }));
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
37 function verify_captcha(form, callback)
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
38 http.request("https://www.google.com/recaptcha/api/verify", {
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
39 body = http.formencode {
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
40 privatekey = captcha_options.recaptcha_private_key;
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
41 remoteip = request.conn:ip();
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
42 challenge = form.recaptcha_challenge_field;
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
43 response = form.recaptcha_response_field;
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
44 };
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
45 }, function (verify_result, code)
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
46 local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
47 if verify_ok == "true" then
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
48 callback(true);
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
49 else
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
50 callback(false, verify_err)
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
51 end
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
52 end);
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
53 end
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 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
56 local request = event.request;
1227
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
57
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
58 return render(register_tpl, {
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
59 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
60 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
61 captcha = generate_captcha(display_options);
6015434f0e05 mod_register_web: Move HTML into separate template files
Kim Alvefur <zash@zash.se>
parents: 1226
diff changeset
62 })
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 function register_user(form)
746
03595194075a mod_register_web: nodeprep username before creating user (thanks IRON)
Matthew Wild <mwild1@gmail.com>
parents: 653
diff changeset
66 local prepped_username = nodeprep(form.username);
03595194075a mod_register_web: nodeprep username before creating user (thanks IRON)
Matthew Wild <mwild1@gmail.com>
parents: 653
diff changeset
67 if usermanager.user_exists(prepped_username, module.host) then
03595194075a mod_register_web: nodeprep username before creating user (thanks IRON)
Matthew Wild <mwild1@gmail.com>
parents: 653
diff changeset
68 return nil, "user-exists";
03595194075a mod_register_web: nodeprep username before creating user (thanks IRON)
Matthew Wild <mwild1@gmail.com>
parents: 653
diff changeset
69 end
03595194075a mod_register_web: nodeprep username before creating user (thanks IRON)
Matthew Wild <mwild1@gmail.com>
parents: 653
diff changeset
70 return usermanager.create_user(prepped_username, form.password, module.host);
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 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
74 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
75 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 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
78 local message;
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if ok then
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 return generate_success(event, form);
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 else
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 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
83 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 function handle_form(event)
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 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
88 local form = http.formdecode(request.body);
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
89 verify_captcha(form, function (ok, err)
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
90 if ok then
653
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local register_ok, register_err = register_user(form);
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 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
93 else
1226
0667624637da mod_register_web: Split out recaptcha verification to a separate function
Kim Alvefur <zash@zash.se>
parents: 1225
diff changeset
94 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
95 end
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end);
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 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
98 end
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 module:provides("http", {
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 route = {
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 GET = generate_page;
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 POST = handle_form;
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 };
c08b0e4b7b38 mod_register_web: Extremely rough web registration page, with captcha
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 });