2020-08-28 15:00:16 +03:00
|
|
|
defmodule PlausibleWeb.Captcha do
|
|
|
|
@verify_endpoint "https://hcaptcha.com/siteverify"
|
|
|
|
|
|
|
|
def enabled? do
|
|
|
|
!!sitekey()
|
|
|
|
end
|
|
|
|
|
|
|
|
def sitekey() do
|
|
|
|
Application.get_env(:plausible, :hcaptcha, [])
|
|
|
|
|> Keyword.fetch!(:sitekey)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify(token) do
|
|
|
|
if enabled?() do
|
2020-11-03 12:20:11 +03:00
|
|
|
res =
|
|
|
|
HTTPoison.post!(@verify_endpoint, {:form, [{"response", token}, {"secret", secret()}]})
|
|
|
|
|
2020-08-28 15:00:16 +03:00
|
|
|
json = Jason.decode!(res.body)
|
|
|
|
json["success"]
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp secret() do
|
|
|
|
Application.get_env(:plausible, :hcaptcha, [])
|
|
|
|
|> Keyword.fetch!(:secret)
|
|
|
|
end
|
|
|
|
end
|