aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEgor Kislitsyn <egor@kislitsyn.com>2019-12-11 22:29:31 +0700
committerEgor Kislitsyn <egor@kislitsyn.com>2019-12-12 17:39:39 +0700
commite53679698424a7d58c308c21d466b07e34e8c3e9 (patch)
treeb81b4ab121df013ccf9c2f4949be01881c02a049 /lib
parent79532a7f7c91f30a738d9c7a3b429b27c29a782d (diff)
downloadpleroma-e53679698424a7d58c308c21d466b07e34e8c3e9.tar.gz
Add native captcha and enable it by default.
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/captcha/native.ex35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/pleroma/captcha/native.ex b/lib/pleroma/captcha/native.ex
new file mode 100644
index 000000000..5306fe1aa
--- /dev/null
+++ b/lib/pleroma/captcha/native.ex
@@ -0,0 +1,35 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# 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
+ {:timeout} ->
+ %{error: dgettext("errors", "Captcha timeout")}
+
+ {: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