|
|
@@ -0,0 +1,24 @@
|
|
|
+#include "captcha.hpp"
|
|
|
+#include "settings.hpp"
|
|
|
+#include <httplib.h>
|
|
|
+#include <nlohmann/json.hpp>
|
|
|
+namespace svapi {
|
|
|
+bool verifyCaptcha(const Settings& s, const std::string& token, const std::string& remoteIp) {
|
|
|
+ if (s.captchaProvider.empty() || token.empty() || s.captchaVerifyUrl.empty()) return false;
|
|
|
+ std::string url = s.captchaVerifyUrl;
|
|
|
+ while (!url.empty() && url.back() == '/') url.pop_back();
|
|
|
+ auto schemeEnd = url.find("://");
|
|
|
+ std::size_t from = (schemeEnd == std::string::npos) ? 0 : schemeEnd + 3;
|
|
|
+ auto slash = url.find('/', from);
|
|
|
+ std::string origin = (slash == std::string::npos) ? url : url.substr(0, slash);
|
|
|
+ std::string path = (slash == std::string::npos) ? "/" : url.substr(slash);
|
|
|
+ httplib::Client cli(origin);
|
|
|
+ cli.set_connection_timeout(5); cli.set_read_timeout(10);
|
|
|
+ cli.enable_server_certificate_verification(true);
|
|
|
+ httplib::Params params{{"secret", s.captchaSecret}, {"response", token}, {"remoteip", remoteIp}};
|
|
|
+ auto res = cli.Post(path, params);
|
|
|
+ if (!res || res->status != 200) return false;
|
|
|
+ try { return nlohmann::json::parse(res->body).value("success", false); }
|
|
|
+ catch (...) { return false; }
|
|
|
+}
|
|
|
+}
|