1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Captcha.Kocaptcha do
6 alias Calendar.DateTime
8 alias Pleroma.Captcha.Service
15 endpoint = Pleroma.Config.get!([__MODULE__, :endpoint])
17 case Tesla.get(endpoint <> "/new") do
19 %{error: "Kocaptcha service unavailable"}
22 json_resp = Poison.decode!(res.body)
24 token = json_resp["token"]
29 {token, json_resp["md5"], DateTime.now_utc() |> DateTime.Format.unix()}
32 %{type: :kocaptcha, token: token, url: endpoint <> json_resp["url"]}
37 def validate(token, captcha) do
38 with false <- is_nil(captcha),
39 [{^token, saved_md5, _}] <- :ets.lookup(@ets, token),
40 true <- :crypto.hash(:md5, captcha) |> Base.encode16() == String.upcase(saved_md5) do
41 # Clear the saved value
42 :ets.delete(@ets, token)
52 seconds_retained = Pleroma.Config.get!([Pleroma.Captcha, :seconds_retained])
53 # If the time in ETS is less than current_time - seconds_retained, then the time has
56 DateTime.subtract!(DateTime.now_utc(), seconds_retained) |> DateTime.Format.unix()
63 [{:<, :"$1", {:const, delete_after}}],