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