laracaptcha 0.1.0

Driver-based captcha for Laravel Rust: Cloudflare Turnstile and Google reCAPTCHA v2 and v3 behind one API.
Documentation
//! Driver-based captcha for Laravel Rust: one API for **Cloudflare Turnstile** and **Google
//! reCAPTCHA v2/v3**. Switch providers by changing one environment variable, no code changes.
//!
//! ```text
//! // In a template: the widget, drawn for the configured driver.
//! {{ laracaptcha_widget(action='register') }}
//!
//! // On the form's input type: the rule.
//! #[serde(rename = "cf-turnstile-response")]
//! #[validate(required, Captcha::new())]
//! captcha: String,
//! ```
//!
//! It is the port of the PHP package [`edulazaro/laracaptcha`](https://github.com/edulazaro/laracaptcha)
//! to Laravel Rust 0.1 (beta), and its API follows the framework's changes until 1.0.
//!
//! The facade of the PHP package is this module's free functions: [`driver()`], [`verify()`], [`fake()`],
//! [`props()`], [`site_key()`], [`script_url()`], [`response_field()`] and [`extend()`].

pub mod config;
pub mod contracts;
pub mod drivers;
pub mod manager;
pub mod provider;
pub mod provider_client;
pub mod result;
pub mod rule;
pub mod support;
pub mod testing;
#[cfg(feature = "minijinja")]
pub mod views;
pub mod widget;

use std::sync::Arc;

use laravel::container::Inject;

pub use config::{DriverConfig, Drivers, LaracaptchaConfig, Messages, RecaptchaV3Config};
pub use contracts::CaptchaDriver;
pub use drivers::{RecaptchaV2Driver, RecaptchaV3Driver, TurnstileDriver};
pub use manager::{CaptchaManager, Props};
pub use provider::LaracaptchaServiceProvider;
pub use result::VerificationResult;
pub use rule::Captcha;
pub use testing::{Attempt, FakeDriver};
pub use widget::Widget;

/// The captcha manager of the running application.
///
/// # Errors
///
/// Fails when the [`LaracaptchaServiceProvider`] is not registered.
pub async fn manager() -> laravel::Result<Inject<CaptchaManager>> {
    laravel::resolve::<CaptchaManager>().await
}

/// The named driver, or the default one when `None`: `Captcha::driver('recaptcha_v3')`.
///
/// # Errors
///
/// Fails when the provider is not registered or the driver does not exist.
pub async fn driver(name: Option<&str>) -> laravel::Result<Arc<dyn CaptchaDriver>> {
    manager().await?.driver(name)
}

/// Verify a token with the default driver: `Captcha::verify($token, $request->ip())`.
///
/// # Errors
///
/// Fails when the provider is not registered or the default driver does not exist. A refused token
/// is not an error: it is a failed [`VerificationResult`].
pub async fn verify(token: &str, ip: Option<&str>) -> laravel::Result<VerificationResult> {
    manager().await?.verify(token, ip).await
}

/// Replace every driver with a fake for testing: `Captcha::fake($success, $score)`. Assert on the
/// returned [`FakeDriver`].
///
/// # Errors
///
/// Fails when the provider is not registered.
pub async fn fake(success: bool, score: Option<f64>) -> laravel::Result<FakeDriver> {
    Ok(manager().await?.fake(success, score))
}

/// What the React, Vue and Svelte components need: `Captcha::props($driver, $action)`.
///
/// # Errors
///
/// Fails when the provider is not registered or the driver does not exist.
pub async fn props(driver: Option<&str>, action: Option<&str>) -> laravel::Result<Props> {
    manager().await?.props(driver, action)
}

/// The public site key of the default driver.
///
/// # Errors
///
/// Fails when the provider is not registered or the default driver does not exist.
pub async fn site_key() -> laravel::Result<String> {
    manager().await?.site_key()
}

/// The script url of the default driver.
///
/// # Errors
///
/// Fails when the provider is not registered or the default driver does not exist.
pub async fn script_url() -> laravel::Result<String> {
    manager().await?.script_url()
}

/// The name of the field carrying the token, for the default driver.
///
/// # Errors
///
/// Fails when the provider is not registered or the default driver does not exist.
pub async fn response_field() -> laravel::Result<String> {
    manager().await?.response_field()
}

/// Register a driver of your own while the application runs: `Captcha::extend('name', fn)`.
///
/// # Errors
///
/// Fails when the provider is not registered.
pub async fn extend<D: CaptchaDriver + 'static>(
    name: impl Into<String>,
    creator: impl Fn() -> D + Send + Sync + 'static,
) -> laravel::Result<()> {
    manager().await?.extend(name, creator);

    Ok(())
}