95 lines
No EOL
2 KiB
Lua
95 lines
No EOL
2 KiB
Lua
local template = require "resty.template.safe"
|
|
local http = require "resty.http"
|
|
|
|
local M = {_TYPE='module', _NAME='recaptcha.funcs', _VERSION='1.0-0'}
|
|
|
|
local recaptcha_verify_url = "https://www.google.com/recaptcha/api/siteverify"
|
|
|
|
M._VERIFY_STATE = "to_verify"
|
|
M._VALIDATED_STATE = "validated"
|
|
|
|
|
|
M.State = {}
|
|
M.State["1"] = M._VERIFY_STATE
|
|
M.State["2"] = M._VALIDATED_STATE
|
|
|
|
M.SecretKey = ""
|
|
M.SiteKey = ""
|
|
M.Template = ""
|
|
|
|
|
|
function M.GetStateID(state)
|
|
for k, v in pairs(M.State) do
|
|
if v == state then
|
|
return tonumber(k)
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function read_file(path)
|
|
local file = io.open(path, "r") -- r read mode and b binary mode
|
|
if not file then return nil end
|
|
io.input(file)
|
|
content = io.read("*a")
|
|
io.close(file)
|
|
return content
|
|
end
|
|
|
|
function M.New(siteKey, secretKey, TemplateFilePath)
|
|
M.SecretKey = secretKey
|
|
M.SiteKey = siteKey
|
|
|
|
local captcha_template = read_file(TemplateFilePath)
|
|
if captcha_template == nil then
|
|
return "Template file " .. TemplateFilePath .. "not found."
|
|
end
|
|
local view = template.new(captcha_template)
|
|
|
|
view.recaptcha_site_key = siteKey
|
|
M.Template = tostring(view)
|
|
|
|
return nil
|
|
end
|
|
|
|
|
|
function M.GetTemplate()
|
|
return M.Template
|
|
end
|
|
|
|
|
|
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, "&")
|
|
end
|
|
|
|
function M.Validate(g_captcha_res, remote_ip)
|
|
body = {
|
|
secret = M.SecretKey,
|
|
response = g_captcha_res,
|
|
remoteip = remote_ip
|
|
}
|
|
|
|
local httpc = http.new()
|
|
|
|
httpc:set_timeout(1)
|
|
|
|
local res, err = httpc:request_uri(recaptcha_verify_url, {
|
|
method = "POST",
|
|
body = body,
|
|
headers = {
|
|
["Content-Type"] = "application/x-www-form-urlencoded",
|
|
},
|
|
})
|
|
if err ~= nil then
|
|
return true, err
|
|
end
|
|
|
|
result = cjson.decode(res.body)
|
|
|
|
return result.success, nil
|
|
end
|
|
|
|
|
|
return M |