analytics/lib/plausible_web/captcha.ex

42 lines
810 B
Elixir
Raw Normal View History

defmodule PlausibleWeb.Captcha do
alias Plausible.HTTPClient
@verify_endpoint "https://hcaptcha.com/siteverify"
def enabled? do
is_binary(sitekey())
end
def sitekey() do
Application.get_env(:plausible, :hcaptcha, [])[:sitekey]
end
def verify(token) do
if enabled?() do
2020-11-03 12:20:11 +03:00
res =
HTTPClient.impl().post(
@verify_endpoint,
[{"content-type", "application/x-www-form-urlencoded"}],
%{
response: token,
secret: secret()
}
)
2020-11-03 12:20:11 +03:00
2022-08-17 14:01:33 +03:00
case res do
{:ok, %Finch.Response{status: 200, body: %{"success" => success}}} ->
success
2022-08-17 14:01:33 +03:00
_ ->
false
end
else
true
end
end
defp secret() do
Application.get_env(:plausible, :hcaptcha, [])[:secret]
end
end