2018-12-15 19:06:44 +00:00
|
|
|
defmodule Pleroma.Captcha do
|
|
|
|
use GenServer
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def start_link() do
|
|
|
|
GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def init(_) do
|
|
|
|
{:ok, nil}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Ask the configured captcha service for a new captcha
|
|
|
|
"""
|
|
|
|
def new() do
|
|
|
|
GenServer.call(__MODULE__, :new)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Ask the configured captcha service to validate the captcha
|
|
|
|
"""
|
2018-12-20 21:32:37 +00:00
|
|
|
def validate(token, captcha, answer_data) do
|
|
|
|
GenServer.call(__MODULE__, {:validate, token, captcha, answer_data})
|
2018-12-15 19:06:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def handle_call(:new, _from, state) do
|
|
|
|
enabled = Pleroma.Config.get([__MODULE__, :enabled])
|
|
|
|
|
|
|
|
if !enabled do
|
|
|
|
{:reply, %{type: :none}, state}
|
|
|
|
else
|
2018-12-16 20:41:11 +00:00
|
|
|
{:reply, method().new(), state}
|
2018-12-15 19:06:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2018-12-20 21:32:37 +00:00
|
|
|
def handle_call({:validate, token, captcha, answer_data}, _from, state) do
|
|
|
|
{:reply, method().validate(token, captcha, answer_data), state}
|
2018-12-16 19:04:43 +00:00
|
|
|
end
|
|
|
|
|
2018-12-15 19:06:44 +00:00
|
|
|
defp method, do: Pleroma.Config.get!([__MODULE__, :method])
|
|
|
|
end
|