Files
AstralRinth/apps/labrinth/src/util/captcha.rs
T
aecsocket ec81bcb13c Improve environment variable handling and reading (#5389)
* wip: better env var reading

* move most env vars to env.rs

* migrate more env vars

* more migration

* more migrations

* More migration

* 🦀 dotenvy is gone (almost)

* 🦀 dotenvy is gone 🦀

* Fix mural source account env var handling

* Remove defaults from admin key vars

* dummy commit to update github pr

* fix ci
2026-02-19 17:33:41 +00:00

56 lines
1.3 KiB
Rust

use crate::env::ENV;
use crate::routes::ApiError;
use actix_web::HttpRequest;
use serde::Deserialize;
use std::collections::HashMap;
pub async fn check_hcaptcha(
req: &HttpRequest,
challenge: &str,
) -> Result<bool, ApiError> {
let secret = &ENV.HCAPTCHA_SECRET;
if secret.is_empty() || secret == "none" {
tracing::info!("hCaptcha secret not set, skipping check");
return Ok(true);
}
let conn_info = req.connection_info().clone();
let ip_addr = if ENV.CLOUDFLARE_INTEGRATION {
if let Some(header) = req.headers().get("CF-Connecting-IP") {
header.to_str().ok()
} else {
conn_info.peer_addr()
}
} else {
conn_info.peer_addr()
};
let ip_addr = ip_addr.ok_or(ApiError::Turnstile)?;
let client = reqwest::Client::new();
#[derive(Deserialize)]
struct Response {
success: bool,
}
let mut form = HashMap::new();
form.insert("response", challenge);
form.insert("secret", secret);
form.insert("remoteip", ip_addr);
let val: Response = client
.post("https://api.hcaptcha.com/siteverify")
.form(&form)
.send()
.await
.map_err(|_| ApiError::Turnstile)?
.json()
.await
.map_err(|_| ApiError::Turnstile)?;
Ok(val.success)
}