Rust

recaptcha

Module

[ReCaptcha]: asks Google whether a reCAPTCHA token came from a person.

Contents

use utopia_abuse::recaptcha::HttpClient;
Rust
pub struct HttpClient

The default [Client]: HTTPS with the WebPKI roots.

Also implements Debug.
use utopia_abuse::ReCaptcha;
Rust
pub struct ReCaptcha<C = HttpClient>

Verifies reCAPTCHA v3 tokens: a token passes when Google answers "success": true with a score of at least the threshold.

Rust
use utopia_abuse::ReCaptcha;
use utopia_abuse::recaptcha::{BoxFuture, Client, Request};

// A client that answers as Google would.
struct Google;

impl Client for Google {
    fn send<'a>(&'a self, _request: Request) -> BoxFuture<'a, utopia_abuse::Result<Vec<u8>>> {
        Box::pin(async { Ok(br#"{"success": true, "score": 0.9}"#.to_vec()) })
    }
}

let recaptcha = ReCaptcha::with_client("secret", Google);
assert!(recaptcha.verify("token", "203.0.113.7", 0.5).await?);
assert!(!recaptcha.verify("token", "203.0.113.7", 0.95).await?);
Also implements Debug.
use utopia_abuse::recaptcha::Request;
Rust
pub struct Request

A request ReCaptcha sends.

use utopia_abuse::recaptcha::Client;
Rust
pub trait Client: Send + Sync

Sends ReCaptcha's request and returns the response's body. Give ReCaptcha your own client to route, record or replay its traffic; HttpClient is the default.

Implemented byHttpClient
Rust
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>

A boxed future, so Client can be a trait object.