comparison mod_register_web/mod_register_web.lua @ 1234:da39ba4047a7

mod_register_web: Pass request to captcha handler
author Kim Alvefur <zash@zash.se>
date Sat, 30 Nov 2013 22:10:41 +0100
parents f7b1992e4ff6
children 9277e0a3922f
comparison
equal deleted inserted replaced
1233:f7b1992e4ff6 1234:da39ba4047a7
34 if captcha_options[k] then return captcha_options[k]; end 34 if captcha_options[k] then return captcha_options[k]; end
35 module:log("error", "Missing parameter from captcha_options: %s", k); 35 module:log("error", "Missing parameter from captcha_options: %s", k);
36 end 36 end
37 })); 37 }));
38 end 38 end
39 function verify_captcha(form, callback) 39 function verify_captcha(request, form, callback)
40 http.request("https://www.google.com/recaptcha/api/verify", { 40 http.request("https://www.google.com/recaptcha/api/verify", {
41 body = http.formencode { 41 body = http.formencode {
42 privatekey = captcha_options.recaptcha_private_key; 42 privatekey = captcha_options.recaptcha_private_key;
43 remoteip = request.conn:ip(); 43 remoteip = request.conn:ip();
44 challenge = form.recaptcha_challenge_field; 44 challenge = form.recaptcha_challenge_field;
79 local challenge = hmac_sha1(secret, answer, true); 79 local challenge = hmac_sha1(secret, answer, true);
80 return captcha_tpl.apply { 80 return captcha_tpl.apply {
81 op = op, x = x, y = y, challenge = challenge; 81 op = op, x = x, y = y, challenge = challenge;
82 }; 82 };
83 end 83 end
84 function verify_captcha(form, callback) 84 function verify_captcha(request, form, callback)
85 if hmac_sha1(secret, form.captcha_reply, true) == form.captcha_challenge then 85 if hmac_sha1(secret, form.captcha_reply, true) == form.captcha_challenge then
86 callback(true); 86 callback(true);
87 else 87 else
88 callback(false, "Captcha verification failed"); 88 callback(false, "Captcha verification failed");
89 end 89 end
133 end 133 end
134 134
135 function handle_form(event) 135 function handle_form(event)
136 local request, response = event.request, event.response; 136 local request, response = event.request, event.response;
137 local form = http.formdecode(request.body); 137 local form = http.formdecode(request.body);
138 verify_captcha(form, function (ok, err) 138 verify_captcha(request, form, function (ok, err)
139 if ok then 139 if ok then
140 local register_ok, register_err = register_user(form); 140 local register_ok, register_err = register_user(form);
141 response:send(generate_register_response(event, form, register_ok, register_err)); 141 response:send(generate_register_response(event, form, register_ok, register_err));
142 else 142 else
143 response:send(generate_page(event, { register_error = err })); 143 response:send(generate_page(event, { register_error = err }));