Skip to main content

rustauth_plugins/captcha/verify_handlers/
captchafox.rs

1//! CaptchaFox verification handler.
2
3use serde::Deserialize;
4
5use super::{service_unavailable, CaptchaVerifyError};
6use crate::captcha::CaptchaOptions;
7
8#[derive(Deserialize)]
9struct SiteVerifyResponse {
10    success: bool,
11}
12
13pub async fn verify(
14    options: &CaptchaOptions,
15    captcha_response: &str,
16    remote_ip: Option<String>,
17) -> Result<bool, CaptchaVerifyError> {
18    let client = options.http_client_ref();
19    let mut form = vec![
20        ("secret", options.secret_key.as_str()),
21        ("response", captcha_response),
22    ];
23    if let Some(site_key) = options.site_key.as_deref() {
24        form.push(("sitekey", site_key));
25    }
26    if let Some(remote_ip) = remote_ip.as_deref() {
27        form.push(("remoteIp", remote_ip));
28    }
29
30    let response = client
31        .post(options.site_verify_url())
32        .form(&form)
33        .send()
34        .await
35        .map_err(service_unavailable)?;
36    if !response.status().is_success() {
37        return Err(service_unavailable(response.status()));
38    }
39    let data = response
40        .json::<SiteVerifyResponse>()
41        .await
42        .map_err(service_unavailable)?;
43
44    Ok(data.success)
45}