1 defmodule Pleroma.Captcha.Kocaptcha do
2 alias Calendar.DateTime
4 alias Pleroma.Captcha.Service
11 endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
13 case Tesla.get(endpoint <> "/new") do
15 %{error: "Kocaptcha service unavailable"}
18 json_resp = Poison.decode!(res.body)
20 token = json_resp["token"]
25 {token, json_resp["md5"], DateTime.now_utc() |> DateTime.Format.unix()}
28 %{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}
33 def validate(token, captcha) do
34 with false <- is_nil(captcha),
35 [{^token, saved_md5, _}] <- :ets.lookup(@ets, token),
36 true <- :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(saved_md5) do
37 # Clear the saved value
38 :ets.delete(@ets, token)
48 seconds_retained = Pleroma.Config.get!([Pleroma.Captcha, :seconds_retained])
49 # If the time in ETS is less than current_time - seconds_retained, then the time has
52 DateTime.subtract!(DateTime.now_utc(), seconds_retained) |> DateTime.Format.unix()
59 [{:<, :"$1", {:const, delete_after}}],