+defmodule Pleroma.Captcha do
+ alias Calendar.DateTime
+ alias Plug.Crypto.KeyGenerator
+ alias Plug.Crypto.MessageEncryptor
+
+ @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
+
+ @doc """
+ Ask the configured captcha service for a new captcha
+ """
+ def new do
+ if not enabled?() do
+ %{type: :none}
+ else
+ new_captcha = method().new()
+
+ # This make salt a little different for two keys
+ {secret, sign_secret} = secret_pair(new_captcha[:token])
+
+ # Basically copy what Phoenix.Token does here, add the time to
+ # the actual data and make it a binary to then encrypt it
+ encrypted_captcha_answer =
+ %{
+ at: DateTime.now_utc(),
+ answer_data: new_captcha[:answer_data]
+ }
+ |> :erlang.term_to_binary()
+ |> MessageEncryptor.encrypt(secret, sign_secret)
+
+ # Replace the answer with the encrypted answer
+ %{new_captcha | answer_data: encrypted_captcha_answer}
+ end
+ end