diff options
author | lain <lain@soykaf.club> | 2019-12-12 18:43:21 +0000 |
---|---|---|
committer | lain <lain@soykaf.club> | 2019-12-12 18:43:21 +0000 |
commit | 2f31cef71f377cf660f818e7e204436f2070bd78 (patch) | |
tree | 7c41977bdc6512918ec79073e94c4d362ea783bf /lib | |
parent | 8ad6d2784c0b79a50346dfd45fe3f3c0997b3d06 (diff) | |
parent | 470a3a678dfb4a7b9c2fe29c6b7ea03cee35ee82 (diff) | |
download | pleroma-2f31cef71f377cf660f818e7e204436f2070bd78.tar.gz |
Merge branch 'feature/native-captcha' into 'develop'
Add native captcha and enable it by default.
Closes #1017 and #1405
See merge request pleroma/pleroma!2060
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/captcha/native.ex | 35 |
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 |