Merge branch 'fix/signup-without-email' into 'develop'
[akkoma] / lib / pleroma / captcha / kocaptcha.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Captcha.Kocaptcha do
6 import Pleroma.Web.Gettext
7 alias Pleroma.Captcha.Service
8 @behaviour Service
9
10 @impl Service
11 def new do
12 endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
13
14 case Tesla.get(endpoint <> "/new") do
15 {:error, _} ->
16 %{error: dgettext("errors", "Kocaptcha service unavailable")}
17
18 {:ok, res} ->
19 json_resp = Jason.decode!(res.body)
20
21 %{
22 type: :kocaptcha,
23 token: json_resp["token"],
24 url: endpoint <> json_resp["url"],
25 answer_data: json_resp["md5"]
26 }
27 end
28 end
29
30 @impl Service
31 def validate(_token, captcha, answer_data) do
32 # Here the token is unsed, because the unencrypted captcha answer is just passed to method
33 if not is_nil(captcha) and
34 :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(answer_data),
35 do: :ok,
36 else: {:error, dgettext("errors", "Invalid CAPTCHA")}
37 end
38 end