Changeset

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 1225:a3766d3baacb
children 1227:6015434f0e05
files mod_register_web/mod_register_web.lua
diffstat 1 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mod_register_web/mod_register_web.lua	Sat Nov 30 18:22:58 2013 +0100
+++ b/mod_register_web/mod_register_web.lua	Sat Nov 30 18:25:32 2013 +0100
@@ -26,6 +26,23 @@
   		end })
   	));
 end
+function verify_captcha(form, callback)
+	http.request("https://www.google.com/recaptcha/api/verify", {
+		body = http.formencode {
+			privatekey = captcha_options.recaptcha_private_key;
+			remoteip = request.conn:ip();
+			challenge = form.recaptcha_challenge_field;
+			response = form.recaptcha_response_field;
+		};
+	}, function (verify_result, code)
+		local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
+		if verify_ok == "true" then
+			callback(true);
+		else
+			callback(false, verify_err)
+		end
+	end);
+end
 
 function generate_page(event, display_options)
 	local request = event.request;
@@ -79,20 +96,12 @@
 function handle_form(event)
 	local request, response = event.request, event.response;
 	local form = http.formdecode(request.body);
-	http.request("https://www.google.com/recaptcha/api/verify", {
-		body = http.formencode {
-			privatekey = captcha_options.recaptcha_private_key;
-			remoteip = request.conn:ip();
-			challenge = form.recaptcha_challenge_field;
-			response = form.recaptcha_response_field;
-		};
-	}, function (verify_result, code)
-		local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)");
-		if verify_ok == "true" then
+	verify_captcha(form, function (ok, err)
+		if ok then
 			local register_ok, register_err = register_user(form);
 			response:send(generate_register_response(event, form, register_ok, register_err));
 		else
-			response:send(generate_page(event, { register_error = verify_err }));
+			response:send(generate_page(event, { register_error = err }));
 		end
 	end);
 	return true; -- Leave connection open until we respond above