+ # If the time found is less than (current_time-seconds_valid) then the time has already passed
+ # Later we check that the time found is more than the presumed invalidatation time, that means
+ # that the data is still valid and the captcha can be checked
+ seconds_valid = Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid])
+ valid_if_after = DateTime.subtract!(DateTime.now_utc(), seconds_valid)
+
+ result =
+ with {:ok, data} <- MessageEncryptor.decrypt(answer_data, secret, sign_secret),
+ %{at: at, answer_data: answer_md5} <- :erlang.binary_to_term(data) do
+ try do
+ if DateTime.before?(at, valid_if_after), do: throw({:error, "CAPTCHA expired"})
+
+ if not is_nil(Cachex.get!(:used_captcha_cache, token)),
+ do: throw({:error, "CAPTCHA already used"})
+
+ res = method().validate(token, captcha, answer_md5)
+ # Throw if an error occurs
+ if res != :ok, do: throw(res)
+
+ # Mark this captcha as used
+ {:ok, _} =
+ Cachex.put(:used_captcha_cache, token, true, ttl: :timer.seconds(seconds_valid))