1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Captcha do
6 import Pleroma.Web.Gettext
8 alias Calendar.DateTime
9 alias Plug.Crypto.KeyGenerator
10 alias Plug.Crypto.MessageEncryptor
16 GenServer.start_link(__MODULE__, [], name: __MODULE__)
25 Ask the configured captcha service for a new captcha
28 GenServer.call(__MODULE__, :new)
32 Ask the configured captcha service to validate the captcha
34 def validate(token, captcha, answer_data) do
35 GenServer.call(__MODULE__, {:validate, token, captcha, answer_data})
39 def handle_call(:new, _from, state) do
40 enabled = Pleroma.Config.get([__MODULE__, :enabled])
43 {:reply, %{type: :none}, state}
45 new_captcha = method().new()
47 secret_key_base = Pleroma.Config.get!([Pleroma.Web.Endpoint, :secret_key_base])
49 # This make salt a little different for two keys
50 token = new_captcha[:token]
51 secret = KeyGenerator.generate(secret_key_base, token <> "_encrypt")
52 sign_secret = KeyGenerator.generate(secret_key_base, token <> "_sign")
53 # Basicallty copy what Phoenix.Token does here, add the time to
54 # the actual data and make it a binary to then encrypt it
55 encrypted_captcha_answer =
57 at: DateTime.now_utc(),
58 answer_data: new_captcha[:answer_data]
60 |> :erlang.term_to_binary()
61 |> MessageEncryptor.encrypt(secret, sign_secret)
65 # Repalce the answer with the encrypted answer
66 %{new_captcha | answer_data: encrypted_captcha_answer},
73 def handle_call({:validate, token, captcha, answer_data}, _from, state) do
74 secret_key_base = Pleroma.Config.get!([Pleroma.Web.Endpoint, :secret_key_base])
75 secret = KeyGenerator.generate(secret_key_base, token <> "_encrypt")
76 sign_secret = KeyGenerator.generate(secret_key_base, token <> "_sign")
78 # If the time found is less than (current_time-seconds_valid) then the time has already passed
79 # Later we check that the time found is more than the presumed invalidatation time, that means
80 # that the data is still valid and the captcha can be checked
81 seconds_valid = Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid])
82 valid_if_after = DateTime.subtract!(DateTime.now_utc(), seconds_valid)
85 with {:ok, data} <- MessageEncryptor.decrypt(answer_data, secret, sign_secret),
86 %{at: at, answer_data: answer_md5} <- :erlang.binary_to_term(data) do
88 if DateTime.before?(at, valid_if_after),
89 do: throw({:error, dgettext("errors", "CAPTCHA expired")})
91 if not is_nil(Cachex.get!(:used_captcha_cache, token)),
92 do: throw({:error, dgettext("errors", "CAPTCHA already used")})
94 res = method().validate(token, captcha, answer_md5)
95 # Throw if an error occurs
96 if res != :ok, do: throw(res)
98 # Mark this captcha as used
100 Cachex.put(:used_captcha_cache, token, true, ttl: :timer.seconds(seconds_valid))
107 _ -> {:error, dgettext("errors", "Invalid answer data")}
110 {:reply, result, state}
113 defp method, do: Pleroma.Config.get!([__MODULE__, :method])