Support for mcaptcha (dirty)

This commit is contained in:
Fijxu 2024-09-08 02:58:50 -03:00
parent 5249a8c4e8
commit e41d14d2f9
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
4 changed files with 265 additions and 192 deletions

View file

@ -138,7 +138,7 @@ end
function csmod.validateCaptcha(captcha_res, remote_ip)
return captcha.Validate(captcha_res, remote_ip)
return captcha.ValidateMCaptcha(captcha_res, remote_ip)
end

View file

@ -9,16 +9,19 @@ local captcha_backend_url = {}
captcha_backend_url["recaptcha"] = "https://www.recaptcha.net/recaptcha/api/siteverify"
captcha_backend_url["hcaptcha"] = "https://hcaptcha.com/siteverify"
captcha_backend_url["turnstile"] = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
captcha_backend_url["mcaptcha"] = "https://mcaptcha.nadeko.net/api/v1/pow/siteverify"
local captcha_frontend_js = {}
captcha_frontend_js["recaptcha"] = "https://www.recaptcha.net/recaptcha/api.js"
captcha_frontend_js["hcaptcha"] = "https://js.hcaptcha.com/1/api.js"
captcha_frontend_js["turnstile"] = "https://challenges.cloudflare.com/turnstile/v0/api.js"
captcha_frontend_js["mcaptcha"] = "https://unpkg.com/@mcaptcha/vanilla-glue@0.1.0-rc2/dist/index.js"
local captcha_frontend_key = {}
captcha_frontend_key["recaptcha"] = "g-recaptcha"
captcha_frontend_key["hcaptcha"] = "h-captcha"
captcha_frontend_key["turnstile"] = "cf-turnstile"
captcha_frontend_key["mcaptcha"] = "m-captcha"
M.SecretKey = ""
M.SiteKey = ""
@ -112,5 +115,39 @@ function M.Validate(captcha_res, remote_ip)
return result.success, nil
end
function M.ValidateMCaptcha(captcha_res, remote_ip)
local body = {
token = captcha_res,
key = M.SiteKey,
secret = M.SecretKey
}
local data = cjson.encode(body)
local httpc = http.new()
httpc:set_timeout(2000)
local res, err = httpc:request_uri(captcha_backend_url[M.CaptchaProvider], {
method = "POST",
body = data,
headers = {
["Content-Type"] = "application/json",
},
})
httpc:close()
if err ~= nil then
return true, err
end
local result = cjson.decode(res.body)
if result.error and result.error == "Account not found" then
ngx.log(ngx.ERR, "siteKey is not valid")
return true, nil
elseif result.error and result.error == "Wrong password" then
ngx.log(ngx.ERR, "secretKey is not valid")
return true, nil
end
return result.valid, nil
end
return M

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long