From 28c43a417e89ad68674f6e60d7d3025fbb4655ff Mon Sep 17 00:00:00 2001 From: Ekaterina Vaartis Date: Sat, 15 Dec 2018 02:00:00 +0300 Subject: [PATCH] Add an ability to disabled captcha --- config/config.exs | 1 + lib/pleroma/captcha.ex | 44 +++++++++++++--------- lib/pleroma/web/twitter_api/twitter_api.ex | 10 ++++- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/config/config.exs b/config/config.exs index df4c618a7..32593045c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -11,6 +11,7 @@ config :pleroma, ecto_repos: [Pleroma.Repo] config :pleroma, Pleroma.Repo, types: Pleroma.PostgresTypes config :pleroma, Pleroma.Captcha, + enabled: false, method: Pleroma.Captcha.Kocaptcha # Kocaptcha is a very simple captcha service, the source code is here: https://github.com/koto-bank/kocaptcha diff --git a/lib/pleroma/captcha.ex b/lib/pleroma/captcha.ex index 31f3bc797..ffa5640ea 100644 --- a/lib/pleroma/captcha.ex +++ b/lib/pleroma/captcha.ex @@ -28,27 +28,37 @@ defmodule Pleroma.Captcha do @doc false def handle_call(:new, _from, state) do - method = Pleroma.Config.get!([__MODULE__, :method]) + enabled = Pleroma.Config.get([__MODULE__, :enabled]) - case method do - __MODULE__.Kocaptcha -> - endpoint = Pleroma.Config.get!([method, :endpoint]) - case HTTPoison.get(endpoint <> "/new") do - {:error, _} -> - %{error: "Kocaptcha service unavailable"} - {:ok, res} -> - json_resp = Poison.decode!(res.body) + if !enabled do + { + :reply, + %{type: :none}, + state + } + else + method = Pleroma.Config.get!([__MODULE__, :method]) - token = json_resp["token"] + case method do + __MODULE__.Kocaptcha -> + endpoint = Pleroma.Config.get!([method, :endpoint]) + case HTTPoison.get(endpoint <> "/new") do + {:error, _} -> + %{error: "Kocaptcha service unavailable"} + {:ok, res} -> + json_resp = Poison.decode!(res.body) - true = :ets.insert(@ets, {token, json_resp["md5"]}) + token = json_resp["token"] - { - :reply, - %{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}, - state - } - end + true = :ets.insert(@ets, {token, json_resp["md5"]}) + + { + :reply, + %{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}, + state + } + end + end end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index c9e8fbcbb..9f98c43c9 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -137,8 +137,16 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do captcha_token: params["captcha_token"] } + captcha_enabled = Pleroma.Config.get([Pleroma.Captcha, :enabled]) + # true if captcha is disabled or enabled and valid, false otherwise + captcha_ok = if !captcha_enabled do + true + else + Pleroma.Captcha.validate(params[:captcha_token], params[:captcha_solution]) + end + # Captcha invalid - if not Pleroma.Captcha.validate(params[:captcha_token], params[:captcha_solution]) do + if not captcha_ok do # I have no idea how this error handling works {:error, %{error: Jason.encode!(%{captcha: ["Invalid CAPTCHA"]})}} else