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