Mercurial > prosody-modules
comparison mod_register_web/mod_register_web.lua @ 4440:95262bd1bcb2
mod_register_web: Add hCaptcha provider
| author | Michel Le Bihan <michel@lebihan.pl> |
|---|---|
| date | Mon, 15 Feb 2021 21:04:19 +0100 |
| parents | cf3247ec5e01 |
| children |
comparison
equal
deleted
inserted
replaced
| 4439:6ae1c7b9c58b | 4440:95262bd1bcb2 |
|---|---|
| 40 local success_tpl = get_template "success"; | 40 local success_tpl = get_template "success"; |
| 41 | 41 |
| 42 -- COMPAT `or request.conn:ip()` | 42 -- COMPAT `or request.conn:ip()` |
| 43 | 43 |
| 44 if next(captcha_options) ~= nil then | 44 if next(captcha_options) ~= nil then |
| 45 local recaptcha_tpl = get_template "recaptcha"; | 45 local provider = captcha_options.provider; |
| 46 | 46 if provider == nil or provider == "recaptcha" then |
| 47 function generate_captcha(display_options) | 47 local recaptcha_tpl = get_template "recaptcha"; |
| 48 return recaptcha_tpl.apply(setmetatable({ | 48 |
| 49 recaptcha_display_error = display_options and display_options.recaptcha_error | 49 function generate_captcha(display_options) |
| 50 and ("&error="..display_options.recaptcha_error) or ""; | 50 return recaptcha_tpl.apply(setmetatable({ |
| 51 }, { | 51 recaptcha_display_error = display_options and display_options.recaptcha_error |
| 52 __index = function (_, k) | 52 and ("&error="..display_options.recaptcha_error) or ""; |
| 53 if captcha_options[k] then return captcha_options[k]; end | 53 }, { |
| 54 module:log("error", "Missing parameter from captcha_options: %s", k); | 54 __index = function (_, k) |
| 55 end | 55 if captcha_options[k] then return captcha_options[k]; end |
| 56 })); | 56 module:log("error", "Missing parameter from captcha_options: %s", k); |
| 57 end | 57 end |
| 58 function verify_captcha(request, form, callback) | 58 })); |
| 59 http.request("https://www.google.com/recaptcha/api/siteverify", { | 59 end |
| 60 body = http.formencode { | 60 function verify_captcha(request, form, callback) |
| 61 secret = captcha_options.recaptcha_private_key; | 61 http.request("https://www.google.com/recaptcha/api/siteverify", { |
| 62 remoteip = request.ip or request.conn:ip(); | 62 body = http.formencode { |
| 63 response = form["g-recaptcha-response"]; | 63 secret = captcha_options.recaptcha_private_key; |
| 64 }; | 64 remoteip = request.ip or request.conn:ip(); |
| 65 }, function (verify_result, code) | 65 response = form["g-recaptcha-response"]; |
| 66 local result = json(verify_result); | 66 }; |
| 67 if not result then | 67 }, function (verify_result, code) |
| 68 module:log("warn", "Unable to decode response from recaptcha: [%d] %s", code, verify_result); | 68 local result = json(verify_result); |
| 69 callback(false, "Captcha API error"); | 69 if not result then |
| 70 elseif result.success == true then | 70 module:log("warn", "Unable to decode response from recaptcha: [%d] %s", code, verify_result); |
| 71 callback(true); | 71 callback(false, "Captcha API error"); |
| 72 else | 72 elseif result.success == true then |
| 73 callback(false, t_concat(result["error-codes"])); | 73 callback(true); |
| 74 end | 74 else |
| 75 end); | 75 callback(false, t_concat(result["error-codes"])); |
| 76 end | |
| 77 end); | |
| 78 end | |
| 79 elseif provider == "hcaptcha" then | |
| 80 local captcha_tpl = get_template "hcaptcha"; | |
| 81 | |
| 82 function generate_captcha(display_options) | |
| 83 return captcha_tpl.apply(setmetatable({ | |
| 84 captcha_display_error = display_options and display_options.captcha_error | |
| 85 and ("&error="..display_options.captcha_error) or ""; | |
| 86 }, { | |
| 87 __index = function (_, k) | |
| 88 if captcha_options[k] then return captcha_options[k]; end | |
| 89 module:log("error", "Missing parameter from captcha_options: %s", k); | |
| 90 end | |
| 91 })); | |
| 92 end | |
| 93 function verify_captcha(request, form, callback) | |
| 94 http.request("https://hcaptcha.com/siteverify", { | |
| 95 body = http.formencode { | |
| 96 secret = captcha_options.captcha_private_key; | |
| 97 remoteip = request.ip or request.conn:ip(); | |
| 98 response = form["h-captcha-response"]; | |
| 99 }; | |
| 100 }, function (verify_result, code) | |
| 101 local result = json(verify_result); | |
| 102 if not result then | |
| 103 module:log("warn", "Unable to decode response from hcaptcha: [%d] %s", code, verify_result); | |
| 104 callback(false, "Captcha API error"); | |
| 105 elseif result.success == true then | |
| 106 callback(true); | |
| 107 else | |
| 108 callback(false, t_concat(result["error-codes"])); | |
| 109 end | |
| 110 end); | |
| 111 end | |
| 76 end | 112 end |
| 77 else | 113 else |
| 78 module:log("debug", "No Recaptcha options set, using fallback captcha") | 114 module:log("debug", "No captcha options set, using fallback captcha") |
| 79 local random = math.random; | 115 local random = math.random; |
| 80 local hmac_sha1 = require "util.hashes".hmac_sha1; | 116 local hmac_sha1 = require "util.hashes".hmac_sha1; |
| 81 local secret = require "util.uuid".generate() | 117 local secret = require "util.uuid".generate() |
| 82 local ops = { '+', '-' }; | 118 local ops = { '+', '-' }; |
| 83 local captcha_tpl = get_template "simplecaptcha"; | 119 local captcha_tpl = get_template "simplecaptcha"; |
