Captcha-CLOUDFLARE-Turnstile

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

    unless ( $content->{'success'} ) {
       die 'fail to verify Turnstile: ', @{ $content->{'error-codes'} }, "\n";
    }

# DESCRIPTION

Captcha::Cloudflare::Turnstile is a subclass of [Captcha::reCAPTCHA::V3](https://metacpan.org/pod/Captcha%3A%3AreCAPTCHA%3A%3AV3) that implements
the [Cloudflare Turnstile](https://www.cloudflare.com/products/turnstile/) CAPTCHA service.

It inherits `verify()`, `name()`, `sitekey()`, and the utility methods from the base class,
and overrides the API endpoints and JavaScript helpers for Turnstile.

## Basic Usage

### new( secret => _secret_, \[ sitekey => _sitekey_, query\_name => _query\_name_ \] )

Requires only secret when constructing.

You have to get them from [Cloudflare Turnstile dashboard](https://dash.cloudflare.com/?to=/:account/turnstile).

    my $ts = Captcha::Cloudflare::Turnstile->new(

README.md  view on Meta::CPAN

    );

### name(\[_name_\])

Get/set the form field name (_query\_name_). Defaults to `'cf-turnstile-response'`.

    my $query_name = "$ts";   # stringification returns query_name

### verify( _response_ )

Sends the token to the Cloudflare Turnstile siteverify endpoint and returns the decoded JSON response.

    my $content = $ts->verify($param{$ts});
    unless ( $content->{'success'} ) {
       die 'fail to verify Turnstile: ', @{ $content->{'error-codes'} }, "\n";
    }

### verify\_or\_die( response => _response_ )

Calls `verify()` and dies immediately on failure.

lib/Captcha/Cloudflare/Turnstile.pm  view on Meta::CPAN

 unless ( $content->{'success'} ) {
    die 'fail to verify Turnstile: ', @{ $content->{'error-codes'} }, "\n";
 }

=head1 DESCRIPTION

Captcha::Cloudflare::Turnstile is a subclass of L<Captcha::reCAPTCHA::V3> that implements
the L<Cloudflare Turnstile|https://www.cloudflare.com/products/turnstile/> CAPTCHA service.

It inherits C<verify()>, C<name()>, C<sitekey()>, and the utility methods from the base class,
and overrides the API endpoints and JavaScript helpers for Turnstile.

=head2 Basic Usage

=head3 new( secret => I<secret>, [ sitekey => I<sitekey>, query_name => I<query_name> ] )

Requires only secret when constructing.

You have to get them from L<Cloudflare Turnstile dashboard|https://dash.cloudflare.com/?to=/:account/turnstile>.

 my $ts = Captcha::Cloudflare::Turnstile->new(

lib/Captcha/Cloudflare/Turnstile.pm  view on Meta::CPAN

 );

=head3 name([I<name>])

Get/set the form field name (I<query_name>). Defaults to C<'cf-turnstile-response'>.

 my $query_name = "$ts";   # stringification returns query_name

=head3 verify( I<response> )

Sends the token to the Cloudflare Turnstile siteverify endpoint and returns the decoded JSON response.

 my $content = $ts->verify($param{$ts});
 unless ( $content->{'success'} ) {
    die 'fail to verify Turnstile: ', @{ $content->{'error-codes'} }, "\n";
 }

=head3 verify_or_die( response => I<response> )

Calls C<verify()> and dies immediately on failure.

t/03_turnstile.t  view on Meta::CPAN


my $ts = Captcha::Cloudflare::Turnstile->new(
    secret  => 'Secret',
    sitekey => 'SiteKey',
);

is $ts->name,        'cf-turnstile-response',  'default name for cloudflare';
is "$ts",            'cf-turnstile-response',  'overloaded stringify';

my $api_url = 'https://challenges.cloudflare.com/turnstile/v0/api.js';
is $ts->scriptURL, $api_url, 'scriptURL returns correct API endpoint';
like $ts->scriptTag, qr|<script src="\Q$api_url\E" defer></script>|, 'scriptTag correct';

is $ts->widgetTag,
    '<div class="cf-turnstile" data-sitekey="SiteKey"></div>',
    'widgetTag with stored sitekey';

my $scripts = $ts->scripts( action => 'login' );
like $scripts, qr|<div class="cf-turnstile" data-sitekey="SiteKey" data-action="login"></div>|,
    'scripts includes widget with action';



( run in 2.508 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )