comparison mod_register_web/mod_register_web.lua @ 1226:0667624637da

mod_register_web: Split out recaptcha verification to a separate function
author Kim Alvefur <zash@zash.se>
date Sat, 30 Nov 2013 18:25:32 +0100
parents a3766d3baacb
children 6015434f0e05
comparison
equal deleted inserted replaced
1225:a3766d3baacb 1226:0667624637da
23 __index = function (t, k) 23 __index = function (t, k)
24 if captcha_options[k] then return captcha_options[k]; end 24 if captcha_options[k] then return captcha_options[k]; end
25 module:log("error", "Missing parameter from captcha_options: %s", k); 25 module:log("error", "Missing parameter from captcha_options: %s", k);
26 end }) 26 end })
27 )); 27 ));
28 end
29 function verify_captcha(form, callback)
30 http.request("https://www.google.com/recaptcha/api/verify", {
31 body = http.formencode {
32 privatekey = captcha_options.recaptcha_private_key;
33 remoteip = request.conn:ip();
34 challenge = form.recaptcha_challenge_field;
35 response = form.recaptcha_response_field;
36 };
37 }, function (verify_result, code)
38 local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
39 if verify_ok == "true" then
40 callback(true);
41 else
42 callback(false, verify_err)
43 end
44 end);
28 end 45 end
29 46
30 function generate_page(event, display_options) 47 function generate_page(event, display_options)
31 local request = event.request; 48 local request = event.request;
32 return [[<!DOCTYPE html> 49 return [[<!DOCTYPE html>
77 end 94 end
78 95
79 function handle_form(event) 96 function handle_form(event)
80 local request, response = event.request, event.response; 97 local request, response = event.request, event.response;
81 local form = http.formdecode(request.body); 98 local form = http.formdecode(request.body);
82 http.request("https://www.google.com/recaptcha/api/verify", { 99 verify_captcha(form, function (ok, err)
83 body = http.formencode { 100 if ok then
84 privatekey = captcha_options.recaptcha_private_key;
85 remoteip = request.conn:ip();
86 challenge = form.recaptcha_challenge_field;
87 response = form.recaptcha_response_field;
88 };
89 }, function (verify_result, code)
90 local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
91 if verify_ok == "true" then
92 local register_ok, register_err = register_user(form); 101 local register_ok, register_err = register_user(form);
93 response:send(generate_register_response(event, form, register_ok, register_err)); 102 response:send(generate_register_response(event, form, register_ok, register_err));
94 else 103 else
95 response:send(generate_page(event, { register_error = verify_err })); 104 response:send(generate_page(event, { register_error = err }));
96 end 105 end
97 end); 106 end);
98 return true; -- Leave connection open until we respond above 107 return true; -- Leave connection open until we respond above
99 end 108 end
100 109