2018-12-24 05:04:54 +09:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 15:49:20 +09:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-24 05:04:54 +09:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-16 04:06:44 +09:00
|
|
|
defmodule Pleroma.Captcha.Kocaptcha do
|
|
|
|
alias Pleroma.Captcha.Service
|
|
|
|
@behaviour Service
|
|
|
|
|
|
|
|
@impl Service
|
2019-03-05 12:18:43 +09:00
|
|
|
def new do
|
2018-12-16 04:06:44 +09:00
|
|
|
endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
|
2018-12-16 04:08:26 +09:00
|
|
|
|
2020-11-01 18:05:39 +09:00
|
|
|
case Pleroma.HTTP.get(endpoint <> "/new") do
|
2018-12-16 04:06:44 +09:00
|
|
|
{:error, _} ->
|
2020-04-30 01:48:08 +09:00
|
|
|
%{error: :kocaptcha_service_unavailable}
|
2018-12-16 04:08:26 +09:00
|
|
|
|
2018-12-16 04:06:44 +09:00
|
|
|
{:ok, res} ->
|
2019-05-14 05:37:38 +09:00
|
|
|
json_resp = Jason.decode!(res.body)
|
2018-12-16 04:06:44 +09:00
|
|
|
|
2018-12-21 06:32:37 +09:00
|
|
|
%{
|
|
|
|
type: :kocaptcha,
|
2018-12-23 04:39:08 +09:00
|
|
|
token: json_resp["token"],
|
2018-12-21 06:32:37 +09:00
|
|
|
url: endpoint <> json_resp["url"],
|
2020-07-30 06:07:22 +09:00
|
|
|
answer_data: json_resp["md5"],
|
|
|
|
seconds_valid: Pleroma.Config.get([Pleroma.Captcha, :seconds_valid])
|
2018-12-21 06:32:37 +09:00
|
|
|
}
|
2018-12-16 04:06:44 +09:00
|
|
|
end
|
|
|
|
end
|
2018-12-17 04:04:43 +09:00
|
|
|
|
|
|
|
@impl Service
|
2018-12-23 04:39:08 +09: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,
|
2020-04-30 01:48:08 +09:00
|
|
|
else: {:error, :invalid}
|
2018-12-17 04:04:43 +09:00
|
|
|
end
|
2018-12-16 04:06:44 +09:00
|
|
|
end
|