logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

kocaptcha.ex (1129B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Captcha.Kocaptcha do
  5. alias Pleroma.Captcha.Service
  6. @behaviour Service
  7. @impl Service
  8. def new do
  9. endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
  10. case Pleroma.HTTP.get(endpoint <> "/new") do
  11. {:error, _} ->
  12. %{error: :kocaptcha_service_unavailable}
  13. {:ok, res} ->
  14. json_resp = Jason.decode!(res.body)
  15. %{
  16. type: :kocaptcha,
  17. token: json_resp["token"],
  18. url: endpoint <> json_resp["url"],
  19. answer_data: json_resp["md5"],
  20. seconds_valid: Pleroma.Config.get([Pleroma.Captcha, :seconds_valid])
  21. }
  22. end
  23. end
  24. @impl Service
  25. def validate(_token, captcha, answer_data) do
  26. # Here the token is unused, because the unencrypted captcha answer is just passed to method
  27. if not is_nil(captcha) and
  28. :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(answer_data),
  29. do: :ok,
  30. else: {:error, :invalid}
  31. end
  32. end