a3128decb4daa8ef43acaf5aeef8aaf5a89c6b8e
[akkoma] / lib / pleroma / captcha / kocaptcha.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
9 @behaviour Service
10 @default_endpoint "https://captcha.kotobank.ch"
11
12 @impl Service
13 def new do
14 endpoint = Pleroma.Config.get([__MODULE__, :endpoint], @default_endpoint)
15
16 case Tesla.get(endpoint <> "/new") do
17 {:error, _} ->
18 %{error: dgettext("errors", "Kocaptcha service unavailable")}
19
20 {:ok, res} ->
21 json_resp = Jason.decode!(res.body)
22
23 %{
24 type: :kocaptcha,
25 token: json_resp["token"],
26 url: endpoint <> json_resp["url"],
27 answer_data: json_resp["md5"]
28 }
29 end
30 end
31
32 @impl Service
33 def validate(_token, captcha, answer_data) do
34 # Here the token is unsed, because the unencrypted captcha answer is just passed to method
35 if not is_nil(captcha) and
36 :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(answer_data),
37 do: :ok,
38 else: {:error, dgettext("errors", "Invalid CAPTCHA")}
39 end
40 end