aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/captcha/native.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/captcha/native.ex')
-rw-r--r--lib/pleroma/captcha/native.ex34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/pleroma/captcha/native.ex b/lib/pleroma/captcha/native.ex
new file mode 100644
index 000000000..a90631d61
--- /dev/null
+++ b/lib/pleroma/captcha/native.ex
@@ -0,0 +1,34 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Captcha.Native do
+ alias Pleroma.Captcha.Service
+ @behaviour Service
+
+ @impl Service
+ def new do
+ case Captcha.get() do
+ :error ->
+ %{error: :captcha_error}
+
+ {: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, :invalid}
+
+ defp token do
+ 10
+ |> :crypto.strong_rand_bytes()
+ |> Base.url_encode64(padding: false)
+ end
+end