blib/lib/Captcha/Cloudflare/Turnstile.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Captcha::Cloudflare::Turnstile;
2 require 5.10.1;
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = "0.02";
7  
8 5 ▶use parent 'Captcha::reCAPTCHA::V3';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use Carp qw(carp croak);
10  
11 sub new {
12 my $class = shift;
13 my $self = bless {}, ref $class || $class;
14 my %attr = @_;
15  
16 $self->{sitekey} = $attr{sitekey} || ''; # No need to set sitekey in server-side
17 $self->{secret} = $attr{secret} || croak "missing param 'secret'";
18 $self->{query_name} = $attr{query_name} || 'cf-turnstile-response';
19 $self->{widget_api} = 'https://challenges.cloudflare.com/turnstile/v0/api.js';
20 $self->{verify_api} = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
21  
22 return $self;
23 }
24  
25 # aroud javascript =======================================================================
26 sub scriptURL {
27 my $self = shift;
28 return $self->{widget_api};
29 }
30  
31 sub widgetTag {
32 my $self = shift;
33 my %attr = @_;
34 my $sitekey = $attr{sitekey} || $self->{sitekey} || croak "missing 'sitekey'";
35 my $action = $attr{action} ? qq| data-action="$attr{action}"| : '';
36 return qq|<div class="cf-turnstile" data-sitekey="$sitekey"$action></div>|;
37 }
38  
39 sub scripts {
40 my $self = shift;
41 my %attr = @_;
42 my $sitekey = $attr{sitekey} || $self->{sitekey} || croak "missing 'sitekey'";
43 my $action = $attr{action} || 'homepage';
44 my $simple = $self->scriptTag(%attr);
45 return <<"EOL";
46 $simple
47 <div class="cf-turnstile" data-sitekey="$sitekey" data-action="$action"></div>
48 EOL
49 }
50  
51 # verifiers =======================================================================
52 sub deny_by_score {
53 my $self = shift;
54 my %attr = @_;
55 my $response = $attr{response} || croak "missing response token";
56 carp "deny_by_score() is not applicable for Cloudflare Turnstile (no score); use verify() instead";
57 return $self->verify($response);
58 }
59  
60 sub verify_or_die {
61 my $self = shift;
62 my %attr = @_;
63 my $response = $attr{response} || croak "missing response token";
64 my $content = $self->verify($response);
65 return $content if $content->{success};
66 die 'fail to verify Turnstile: ', $content->{'error-codes'}[0], "\n";
67 }
68  
69 1;
70 __END__
↑ Summary