comparison mod_register_web/mod_register_web.lua @ 1778:32604bf33a4c

mod_register_web: Switch to the new reCAPTCHA API, including support for "nocaptcha" when users are already signed in to Google.
author Thijs Alkemade <me@thijsalkema.de>
date Wed, 04 Mar 2015 12:57:24 +0100
parents c56baec031e8
children c8161146c698
comparison
equal deleted inserted replaced
1777:c353acd1d366 1778:32604bf33a4c
1 local captcha_options = module:get_option("captcha_options", {}); 1 local captcha_options = module:get_option("captcha_options", {});
2 local nodeprep = require "util.encodings".stringprep.nodeprep; 2 local nodeprep = require "util.encodings".stringprep.nodeprep;
3 local usermanager = require "core.usermanager"; 3 local usermanager = require "core.usermanager";
4 local http = require "net.http"; 4 local http = require "net.http";
5 local path_sep = package.config:sub(1,1); 5 local path_sep = package.config:sub(1,1);
6 local json = require "util.json".decode;
7 local t_concat = table.concat;
6 8
7 module:depends"http"; 9 module:depends"http";
8 10
9 local extra_fields = { 11 local extra_fields = {
10 nick = true; name = true; first = true; last = true; email = true; 12 nick = true; name = true; first = true; last = true; email = true;
45 module:log("error", "Missing parameter from captcha_options: %s", k); 47 module:log("error", "Missing parameter from captcha_options: %s", k);
46 end 48 end
47 })); 49 }));
48 end 50 end
49 function verify_captcha(request, form, callback) 51 function verify_captcha(request, form, callback)
50 http.request("https://www.google.com/recaptcha/api/verify", { 52 http.request("https://www.google.com/recaptcha/api/siteverify", {
51 body = http.formencode { 53 body = http.formencode {
52 privatekey = captcha_options.recaptcha_private_key; 54 secret = captcha_options.recaptcha_private_key;
53 remoteip = request.conn:ip(); 55 remoteip = request.conn:ip();
54 challenge = form.recaptcha_challenge_field; 56 response = form["g-recaptcha-response"];
55 response = form.recaptcha_response_field;
56 }; 57 };
57 }, function (verify_result, code) 58 }, function (verify_result, code)
58 local verify_ok, verify_err = verify_result:match("^([^\n]+)\n([^\n]+)"); 59 local result = json(verify_result);
59 if verify_ok == "true" then 60 if result.success == true then
60 callback(true); 61 callback(true);
61 else 62 else
62 callback(false, verify_err) 63 callback(false, t_concat(result["error-codes"]));
63 end 64 end
64 end); 65 end);
65 end 66 end
66 else 67 else
67 module:log("debug", "No Recaptcha options set, using fallback captcha") 68 module:log("debug", "No Recaptcha options set, using fallback captcha")