2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 15:44:49 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-15 12:06:44 -07:00
|
|
|
defmodule Pleroma.Captcha.Kocaptcha do
|
2019-07-10 03:25:58 -06:00
|
|
|
import Pleroma.Web.Gettext
|
2018-12-15 12:06:44 -07:00
|
|
|
alias Pleroma.Captcha.Service
|
|
|
|
@behaviour Service
|
|
|
|
|
|
|
|
@impl Service
|
2019-03-04 20:18:43 -07:00
|
|
|
def new do
|
2018-12-15 12:06:44 -07:00
|
|
|
endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
|
2018-12-15 12:08:26 -07:00
|
|
|
|
2018-12-15 12:43:28 -07:00
|
|
|
case Tesla.get(endpoint <> "/new") do
|
2018-12-15 12:06:44 -07:00
|
|
|
{:error, _} ->
|
2019-07-10 03:25:58 -06:00
|
|
|
%{error: dgettext("errors", "Kocaptcha service unavailable")}
|
2018-12-15 12:08:26 -07:00
|
|
|
|
2018-12-15 12:06:44 -07:00
|
|
|
{:ok, res} ->
|
2019-05-13 14:37:38 -06:00
|
|
|
json_resp = Jason.decode!(res.body)
|
2018-12-15 12:06:44 -07:00
|
|
|
|
2018-12-20 14:32:37 -07:00
|
|
|
%{
|
|
|
|
type: :kocaptcha,
|
2018-12-22 12:39:08 -07:00
|
|
|
token: json_resp["token"],
|
2018-12-20 14:32:37 -07:00
|
|
|
url: endpoint <> json_resp["url"],
|
2018-12-22 12:39:08 -07:00
|
|
|
answer_data: json_resp["md5"]
|
2018-12-20 14:32:37 -07:00
|
|
|
}
|
2018-12-15 12:06:44 -07:00
|
|
|
end
|
|
|
|
end
|
2018-12-16 12:04:43 -07:00
|
|
|
|
|
|
|
@impl Service
|
2018-12-22 12:39:08 -07: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,
|
2019-07-10 03:25:58 -06:00
|
|
|
else: {:error, dgettext("errors", "Invalid CAPTCHA")}
|
2018-12-16 12:04:43 -07:00
|
|
|
end
|
2018-12-15 12:06:44 -07:00
|
|
|
end
|