Up captcha timer to 60 secs again, save used captchas in cachex
[akkoma] / lib / pleroma / captcha / captcha.ex
1 defmodule Pleroma.Captcha do
2 alias Plug.Crypto.KeyGenerator
3 alias Plug.Crypto.MessageEncryptor
4 alias Calendar.DateTime
5
6 use GenServer
7
8 @doc false
9 def start_link() do
10 GenServer.start_link(__MODULE__, [], name: __MODULE__)
11 end
12
13 @doc false
14 def init(_) do
15 {:ok, nil}
16 end
17
18 @doc """
19 Ask the configured captcha service for a new captcha
20 """
21 def new() do
22 GenServer.call(__MODULE__, :new)
23 end
24
25 @doc """
26 Ask the configured captcha service to validate the captcha
27 """
28 def validate(token, captcha, answer_data) do
29 GenServer.call(__MODULE__, {:validate, token, captcha, answer_data})
30 end
31
32 @doc false
33 def handle_call(:new, _from, state) do
34 enabled = Pleroma.Config.get([__MODULE__, :enabled])
35
36 if !enabled do
37 {:reply, %{type: :none}, state}
38 else
39 new_captcha = method().new()
40
41 secret_key_base = Pleroma.Config.get!([Pleroma.Web.Endpoint, :secret_key_base])
42
43 # This make salt a little different for two keys
44 token = new_captcha[:token]
45 secret = KeyGenerator.generate(secret_key_base, token <> "_encrypt")
46 sign_secret = KeyGenerator.generate(secret_key_base, token <> "_sign")
47 # Basicallty copy what Phoenix.Token does here, add the time to
48 # the actual data and make it a binary to then encrypt it
49 encrypted_captcha_answer =
50 %{
51 at: DateTime.now_utc(),
52 answer_data: new_captcha[:answer_data]
53 }
54 |> :erlang.term_to_binary()
55 |> MessageEncryptor.encrypt(secret, sign_secret)
56
57 IO.inspect(%{new_captcha | answer_data: encrypted_captcha_answer})
58
59 {
60 :reply,
61 # Repalce the answer with the encrypted answer
62 %{new_captcha | answer_data: encrypted_captcha_answer},
63 state
64 }
65 end
66 end
67
68 @doc false
69 def handle_call({:validate, token, captcha, answer_data}, _from, state) do
70 secret_key_base = Pleroma.Config.get!([Pleroma.Web.Endpoint, :secret_key_base])
71 secret = KeyGenerator.generate(secret_key_base, token <> "_encrypt")
72 sign_secret = KeyGenerator.generate(secret_key_base, token <> "_sign")
73
74 # If the time found is less than (current_time - seconds_valid), then the time has already passed.
75 # Later we check that the time found is more than the presumed invalidatation time, that means
76 # that the data is still valid and the captcha can be checked
77 seconds_valid = Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid])
78 valid_if_after = DateTime.subtract!(DateTime.now_utc(), seconds_valid)
79
80 result =
81 with {:ok, data} <- MessageEncryptor.decrypt(answer_data, secret, sign_secret),
82 %{at: at, answer_data: answer_md5} <- :erlang.binary_to_term(data) do
83 try do
84 if DateTime.before?(at, valid_if_after), do: throw({:error, "CAPTCHA expired"})
85
86 if not is_nil(Cachex.get!(:used_captcha_cache, token)),
87 do: throw({:error, "CAPTCHA already used"})
88
89 res = method().validate(token, captcha, answer_md5)
90 # Throw if an error occurs
91 if res != :ok, do: throw(res)
92
93 # Mark this captcha as used
94 {:ok, _} =
95 Cachex.put(:used_captcha_cache, token, true, ttl: :timer.seconds(seconds_valid))
96
97 :ok
98 catch
99 :throw, e -> e
100 end
101 else
102 _ -> {:error, "Invalid answer data"}
103 end
104
105 {:reply, result, state}
106 end
107
108 defp method, do: Pleroma.Config.get!([__MODULE__, :method])
109 end