Add native captcha and enable it by default.
[akkoma] / lib / pleroma / captcha / native.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Captcha.Native do
6 import Pleroma.Web.Gettext
7 alias Pleroma.Captcha.Service
8 @behaviour Service
9
10 @impl Service
11 def new do
12 case Captcha.get() do
13 {:timeout} ->
14 %{error: dgettext("errors", "Captcha timeout")}
15
16 {:ok, answer_data, img_binary} ->
17 %{
18 type: :native,
19 token: token(),
20 url: "data:image/png;base64," <> Base.encode64(img_binary),
21 answer_data: answer_data
22 }
23 end
24 end
25
26 @impl Service
27 def validate(_token, captcha, captcha) when not is_nil(captcha), do: :ok
28 def validate(_token, _captcha, _answer), do: {:error, dgettext("errors", "Invalid CAPTCHA")}
29
30 defp token do
31 10
32 |> :crypto.strong_rand_bytes()
33 |> Base.url_encode64(padding: false)
34 end
35 end