2018-12-15 19:06:44 +00:00
|
|
|
defmodule Pleroma.Captcha.Kocaptcha do
|
|
|
|
alias Pleroma.Captcha.Service
|
|
|
|
@behaviour Service
|
|
|
|
|
|
|
|
@impl Service
|
|
|
|
def new() do
|
|
|
|
endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
|
2018-12-15 19:08:26 +00:00
|
|
|
|
2018-12-15 19:43:28 +00:00
|
|
|
case Tesla.get(endpoint <> "/new") do
|
2018-12-15 19:06:44 +00:00
|
|
|
{:error, _} ->
|
|
|
|
%{error: "Kocaptcha service unavailable"}
|
2018-12-15 19:08:26 +00:00
|
|
|
|
2018-12-15 19:06:44 +00:00
|
|
|
{:ok, res} ->
|
|
|
|
json_resp = Poison.decode!(res.body)
|
|
|
|
|
2018-12-20 21:32:37 +00:00
|
|
|
%{
|
|
|
|
type: :kocaptcha,
|
2018-12-22 19:39:08 +00:00
|
|
|
token: json_resp["token"],
|
2018-12-20 21:32:37 +00:00
|
|
|
url: endpoint <> json_resp["url"],
|
2018-12-22 19:39:08 +00:00
|
|
|
answer_data: json_resp["md5"]
|
2018-12-20 21:32:37 +00:00
|
|
|
}
|
2018-12-15 19:06:44 +00:00
|
|
|
end
|
|
|
|
end
|
2018-12-16 19:04:43 +00:00
|
|
|
|
|
|
|
@impl Service
|
2018-12-22 19:39:08 +00:00
|
|
|
def validate(_token, captcha, answer_data) do
|
|
|
|
# Here the token is unsed, because the unencrypted captcha answer is just passed to method
|
|
|
|
if not is_nil(captcha) and
|
|
|
|
:crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(answer_data),
|
|
|
|
do: :ok,
|
|
|
|
else: {:error, "Invalid CAPTCHA"}
|
2018-12-16 19:04:43 +00:00
|
|
|
end
|
2018-12-15 19:06:44 +00:00
|
|
|
end
|