lua-cs-bouncer-mcaptcha/lib/plugins/crowdsec/recaptcha.lua

117 lines
2.7 KiB
Lua
Raw Normal View History

2022-01-26 16:59:32 +01:00
local http = require "resty.http"
2022-01-26 17:44:50 +01:00
local cjson = require "cjson"
2022-01-30 16:21:10 +01:00
local template = require "plugins.crowdsec.template"
2022-01-30 17:07:51 +01:00
local utils = require "plugins.crowdsec.utils"
2022-01-30 16:21:10 +01:00
2022-01-05 15:10:54 +01:00
2022-01-26 12:23:49 +01:00
local M = {_TYPE='module', _NAME='recaptcha.funcs', _VERSION='1.0-0'}
2022-01-26 17:01:47 +01:00
local recaptcha_verify_url = "https://www.google.com/recaptcha/api/siteverify"
2022-01-26 16:50:29 +01:00
M._VERIFY_STATE = "to_verify"
M._VALIDATED_STATE = "validated"
2022-01-26 12:23:49 +01:00
M.State = {}
2022-01-26 16:50:29 +01:00
M.State["1"] = M._VERIFY_STATE
M.State["2"] = M._VALIDATED_STATE
2022-01-26 12:23:49 +01:00
M.SecretKey = ""
M.SiteKey = ""
M.Template = ""
2022-01-26 16:50:29 +01:00
function M.GetStateID(state)
for k, v in pairs(M.State) do
if v == state then
return tonumber(k)
end
end
return nil
end
2022-01-30 17:07:51 +01:00
2022-01-26 18:15:09 +01:00
2022-01-26 12:41:45 +01:00
function M.New(siteKey, secretKey, TemplateFilePath)
2022-01-26 18:15:09 +01:00
if siteKey == nil or siteKey == "" then
return "no recaptcha site key provided, can't use recaptcha"
end
2022-01-26 12:23:49 +01:00
M.SiteKey = siteKey
2022-01-26 18:15:09 +01:00
if secretKey == nil or secretKey == "" then
return "no recaptcha secret key provided, can't use recaptcha"
end
M.SecretKey = secretKey
2022-02-08 17:50:28 +01:00
if TemplateFilePath == nil then
return "CAPTCHA_TEMPLATE_PATH variable is empty, will ban without template"
end
2022-01-30 17:07:51 +01:00
if utils.file_exist(TemplateFilePath) == false then
2022-01-26 18:15:09 +01:00
return "captcha template file doesn't exist, can't use recaptcha"
end
2022-01-30 17:07:51 +01:00
local captcha_template = utils.read_file(TemplateFilePath)
2022-01-26 12:35:51 +01:00
if captcha_template == nil then
2022-01-26 12:41:45 +01:00
return "Template file " .. TemplateFilePath .. "not found."
2022-01-26 12:35:51 +01:00
end
2022-01-30 16:21:10 +01:00
2022-02-01 11:51:10 +01:00
local template_data = {}
2022-01-30 16:25:58 +01:00
template_data["recaptcha_site_key"] = M.SiteKey
2022-01-30 16:21:10 +01:00
local view = template.compile(captcha_template, template_data)
M.Template = view
2022-01-26 12:35:51 +01:00
2022-01-26 12:41:45 +01:00
return nil
2022-01-26 12:23:49 +01:00
end
2022-01-26 12:32:09 +01:00
function M.GetTemplate()
return M.Template
end
2022-01-26 12:23:49 +01:00
function table_to_encoded_url(args)
local params = {}
for k, v in pairs(args) do table.insert(params, k .. '=' .. v) end
return table.concat(params, "&")
2022-02-01 11:51:10 +01:00
end
2022-01-26 12:23:49 +01:00
function M.Validate(g_captcha_res, remote_ip)
2022-02-01 11:51:10 +01:00
local body = {
2022-01-26 12:28:49 +01:00
secret = M.SecretKey,
2022-01-26 12:23:49 +01:00
response = g_captcha_res,
remoteip = remote_ip
2022-02-01 11:51:10 +01:00
}
local data = table_to_encoded_url(body)
local httpc = http.new()
2022-02-09 15:37:06 +01:00
httpc:set_timeout(2000)
2022-02-01 11:51:10 +01:00
local res, err = httpc:request_uri(recaptcha_verify_url, {
method = "POST",
body = data,
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
},
})
2022-02-09 15:37:06 +01:00
httpc:close()
2022-02-01 11:51:10 +01:00
if err ~= nil then
return true, err
end
local result = cjson.decode(res.body)
if result.success == false then
for k, v in pairs(result["error-codes"]) do
if v == "invalid-input-secret" then
ngx.log(ngx.ERR, "reCaptcha secret key is invalid")
return true, nil
end
end
end
return result.success, nil
2022-01-26 12:23:49 +01:00
end
2022-01-05 15:10:54 +01:00
2022-01-26 12:26:45 +01:00
return M