Change Pleroma.CaptchaTest to be a regular module instead of GenServer

This commit is contained in:
Egor Kislitsyn 2020-04-29 21:01:16 +04:00
parent 7b0c8f0fde
commit 39a78998d0
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
2 changed files with 14 additions and 45 deletions

View file

@ -73,7 +73,6 @@ defmodule Pleroma.Application do
Pleroma.Repo, Pleroma.Repo,
Config.TransferTask, Config.TransferTask,
Pleroma.Emoji, Pleroma.Emoji,
Pleroma.Captcha,
Pleroma.Plugs.RateLimiter.Supervisor Pleroma.Plugs.RateLimiter.Supervisor
] ++ ] ++
cachex_children() ++ cachex_children() ++

View file

@ -7,36 +7,12 @@ defmodule Pleroma.Captcha do
alias Plug.Crypto.KeyGenerator alias Plug.Crypto.KeyGenerator
alias Plug.Crypto.MessageEncryptor alias Plug.Crypto.MessageEncryptor
use GenServer
@doc false
def start_link(_) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
@doc false
def init(_) do
{:ok, nil}
end
@doc """ @doc """
Ask the configured captcha service for a new captcha Ask the configured captcha service for a new captcha
""" """
def new do def new do
GenServer.call(__MODULE__, :new)
end
@doc """
Ask the configured captcha service to validate the captcha
"""
def validate(token, captcha, answer_data) do
GenServer.call(__MODULE__, {:validate, token, captcha, answer_data})
end
@doc false
def handle_call(:new, _from, state) do
if not enabled?() do if not enabled?() do
{:reply, %{type: :none}, state} %{type: :none}
else else
new_captcha = method().new() new_captcha = method().new()
@ -53,18 +29,22 @@ defmodule Pleroma.Captcha do
|> :erlang.term_to_binary() |> :erlang.term_to_binary()
|> MessageEncryptor.encrypt(secret, sign_secret) |> MessageEncryptor.encrypt(secret, sign_secret)
{ # Replace the answer with the encrypted answer
:reply, %{new_captcha | answer_data: encrypted_captcha_answer}
# Replace the answer with the encrypted answer
%{new_captcha | answer_data: encrypted_captcha_answer},
state
}
end end
end end
@doc false @doc """
def handle_call({:validate, token, captcha, answer_data}, _from, state) do Ask the configured captcha service to validate the captcha
{:reply, do_validate(token, captcha, answer_data), state} """
def validate(token, captcha, answer_data) do
with {:ok, %{at: at, answer_data: answer_md5}} <- validate_answer_data(token, answer_data),
:ok <- validate_expiration(at),
:ok <- validate_usage(token),
:ok <- method().validate(token, captcha, answer_md5),
{:ok, _} <- mark_captcha_as_used(token) do
:ok
end
end end
def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled], false) def enabled?, do: Pleroma.Config.get([__MODULE__, :enabled], false)
@ -79,16 +59,6 @@ defmodule Pleroma.Captcha do
{secret, sign_secret} {secret, sign_secret}
end end
defp do_validate(token, captcha, answer_data) do
with {:ok, %{at: at, answer_data: answer_md5}} <- validate_answer_data(token, answer_data),
:ok <- validate_expiration(at),
:ok <- validate_usage(token),
:ok <- method().validate(token, captcha, answer_md5),
{:ok, _} <- mark_captcha_as_used(token) do
:ok
end
end
defp validate_answer_data(token, answer_data) do defp validate_answer_data(token, answer_data) do
{secret, sign_secret} = secret_pair(token) {secret, sign_secret} = secret_pair(token)