verify

Function verify 

Source
pub async fn verify(
    secret: &str,
    response: &str,
    remoteip: Option<&IpAddr>,
) -> Result<(), RecaptchaError>
👎Deprecated since 0.2.0: Use recaptcha_verify::verify_v3 instead. Or migrate to enterprise and use recaptcha_verify::verify_enterprise.
Expand description

§Verify ReCaptcha

This is supposed to be a (near) drop-in replacement for recaptcha-rs but using more recent versions of tokio, reqwest and serde.

§Minimalist Example

Basic starting point.

ⓘ
use recaptcha_verify::{RecaptchaError, verify};

let res:Result<(), RecaptchaError> = verify("secret", "token", None).await;

§Full Example

End-to-end real-life use with actix and result handling.

#[tokio::main]
async fn main() {
    #![allow(deprecated)]
    use std::net::IpAddr;
    use recaptcha_verify::{RecaptchaError, verify as recaptcha_verify};

    let recaptcha_secret_key = "secret"; // from env or config
    let recaptcha_token = "token"; // from request
    let realip_remote_addr = Some("1.1.1.1"); // actix_web::info::ConnectionInfo

    let ip_addr;
    let mut ip: Option<&IpAddr> = None;

    if let Some(remote_addr) = realip_remote_addr {
        if let Ok(ip_addr_res) = remote_addr.to_string().parse::<IpAddr>() {
            ip_addr = ip_addr_res;
            ip = Some(&ip_addr);
        }
    }

    let res = recaptcha_verify(recaptcha_secret_key, recaptcha_token, ip).await;

    if res.is_ok() {
        assert!(matches!(res, Ok(())));
    } else {
        assert!(matches!(res, Err(RecaptchaError::InvalidInputResponse)));
    }
}