logo

pleroma

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

native.ex (933B)


  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.Native do
  5. alias Pleroma.Captcha.Service
  6. @behaviour Service
  7. @impl Service
  8. def new do
  9. case Captcha.get() do
  10. :error ->
  11. %{error: :captcha_error}
  12. {:ok, answer_data, img_binary} ->
  13. %{
  14. type: :native,
  15. token: token(),
  16. url: "data:image/png;base64," <> Base.encode64(img_binary),
  17. answer_data: answer_data,
  18. seconds_valid: Pleroma.Config.get([Pleroma.Captcha, :seconds_valid])
  19. }
  20. end
  21. end
  22. @impl Service
  23. def validate(_token, captcha, captcha) when not is_nil(captcha), do: :ok
  24. def validate(_token, _captcha, _answer), do: {:error, :invalid}
  25. defp token do
  26. 10
  27. |> :crypto.strong_rand_bytes()
  28. |> Base.url_encode64(padding: false)
  29. end
  30. end