cd0eb6f2141d0360c69f710ad7a5ed9bd2649d27
[akkoma] / lib / pleroma / captcha / kocaptcha.ex
1 defmodule Pleroma.Captcha.Kocaptcha do
2 alias Pleroma.Captcha.Service
3 @behaviour Service
4
5 @impl Service
6 def new() do
7 endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
8
9 case Tesla.get(endpoint <> "/new") do
10 {:error, _} ->
11 %{error: "Kocaptcha service unavailable"}
12
13 {:ok, res} ->
14 json_resp = Poison.decode!(res.body)
15
16 %{
17 type: :kocaptcha,
18 token: json_resp["token"],
19 url: endpoint <> json_resp["url"],
20 answer_data: json_resp["md5"]
21 }
22 end
23 end
24
25 @impl Service
26 def validate(_token, captcha, answer_data) do
27 # Here the token is unsed, because the unencrypted captcha answer is just passed to method
28 if not is_nil(captcha) and
29 :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(answer_data),
30 do: :ok,
31 else: {:error, "Invalid CAPTCHA"}
32 end
33 end