2019-12-11 08:29:31 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-12-11 08:29:31 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Captcha.Native do
|
|
|
|
alias Pleroma.Captcha.Service
|
|
|
|
@behaviour Service
|
|
|
|
|
|
|
|
@impl Service
|
|
|
|
def new do
|
|
|
|
case Captcha.get() do
|
2020-02-25 07:34:56 -07:00
|
|
|
:error ->
|
2020-04-29 10:48:08 -06:00
|
|
|
%{error: :captcha_error}
|
2019-12-11 08:29:31 -07:00
|
|
|
|
|
|
|
{:ok, answer_data, img_binary} ->
|
|
|
|
%{
|
|
|
|
type: :native,
|
|
|
|
token: token(),
|
|
|
|
url: "data:image/png;base64," <> Base.encode64(img_binary),
|
2020-07-29 15:07:22 -06:00
|
|
|
answer_data: answer_data,
|
|
|
|
seconds_valid: Pleroma.Config.get([Pleroma.Captcha, :seconds_valid])
|
2019-12-11 08:29:31 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Service
|
|
|
|
def validate(_token, captcha, captcha) when not is_nil(captcha), do: :ok
|
2020-04-29 10:48:08 -06:00
|
|
|
def validate(_token, _captcha, _answer), do: {:error, :invalid}
|
2019-12-11 08:29:31 -07:00
|
|
|
|
|
|
|
defp token do
|
|
|
|
10
|
|
|
|
|> :crypto.strong_rand_bytes()
|
|
|
|
|> Base.url_encode64(padding: false)
|
|
|
|
end
|
|
|
|
end
|