2019-12-11 15:29:31 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-12-11 15:29:31 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Captcha.Native do
|
|
|
|
import Pleroma.Web.Gettext
|
|
|
|
alias Pleroma.Captcha.Service
|
|
|
|
@behaviour Service
|
|
|
|
|
|
|
|
@impl Service
|
|
|
|
def new do
|
|
|
|
case Captcha.get() do
|
2020-02-25 14:34:56 +00:00
|
|
|
:error ->
|
|
|
|
%{error: dgettext("errors", "Captcha error")}
|
2019-12-11 15:29:31 +00:00
|
|
|
|
|
|
|
{:ok, answer_data, img_binary} ->
|
|
|
|
%{
|
|
|
|
type: :native,
|
|
|
|
token: token(),
|
|
|
|
url: "data:image/png;base64," <> Base.encode64(img_binary),
|
|
|
|
answer_data: answer_data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Service
|
|
|
|
def validate(_token, captcha, captcha) when not is_nil(captcha), do: :ok
|
|
|
|
def validate(_token, _captcha, _answer), do: {:error, dgettext("errors", "Invalid CAPTCHA")}
|
|
|
|
|
|
|
|
defp token do
|
|
|
|
10
|
|
|
|
|> :crypto.strong_rand_bytes()
|
|
|
|
|> Base.url_encode64(padding: false)
|
|
|
|
end
|
|
|
|
end
|