blib/lib/Mojolicious/Plugin/ReCAPTCHAv2.pm

filter: Statementstmt 78.0% 78 / 100 Branchbran 50.0% 15 / 30 Conditioncond 47.8% 11 / 23 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 65.1% 116 / 178 CC 26 SCAR 20.2
CC
26
Cov
68%
__ANON__
12.7
__ANON__
11.7
register
7.5
CRAP 7.5
↑ Summary
1 package Mojolicious::Plugin::ReCAPTCHAv2;
2 6 $Mojolicious::Plugin::ReCAPTCHAv2::VERSION = '1.06';
3 # vim:syntax=perl:tabstop=4:number:noexpandtab:
4  
5 6 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: use Googles "No CAPTCHA reCAPCTHA" (reCAPTCHA v2) service in Mojolicious apps
8  
9 6 ▶use Mojo::JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Mojo::UserAgent qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶has conf => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 5 ▶has ua => sub { Mojo::UserAgent->new()->max_redirects( 0 ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15  
16 5 ▶sub register {
Subroutine register: called CC=6 SCAR=20.2
Pod documented
17 5 my $plugin = shift;
18 5 my $app = shift;
19 5 ▶ my $conf = shift || {};
Condition50%1/2
shift() || {}
l!l
50
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
20  
21 die ref( $plugin ), ": need sitekey and secret!\n"
22 5 ▶ unless $conf->{'sitekey'} and $conf->{'secret'};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"sitekey"} and $conf->{"secret"} 0 5 5
Condition33%1/3
$conf->{"sitekey"} and $conf->{"secret"}
!ll&&!rl&&r
005
MC/DC0%0/2
$conf->{"sitekey"} and $conf->{"secret"}
$conf->{"sitekey"} $conf->{"secret"}
Truth table33%1/3
A and B
A= $conf->{"sitekey"}B= $conf->{"secret"}
ABresult
0X0
100
111
23  
24 5 ▶ $conf->{'api_url'} //= 'https://www.google.com/recaptcha/api/siteverify';
Condition50%1/2
$conf->{"api_url"} //= "https://www.google.com/recaptcha/api/siteverify"
l!l
05
MC/DC0%0/1
$conf->{"api_url"} //= "https://www.google.com/recaptcha/api/siteverify"
$conf->{"api_url"}
Truth table50%1/2
A
A= $conf->{"api_url"}
Aresult
11
00
25 5 ▶ $conf->{'api_timeout'} //= 10;
Condition50%1/2
$conf->{"api_timeout"} //= 10
l!l
05
MC/DC0%0/1
$conf->{"api_timeout"} //= 10
$conf->{"api_timeout"}
Truth table50%1/2
A
A= $conf->{"api_timeout"}
Aresult
11
00
26  
27 5 $plugin->conf( $conf );
28 5 $plugin->ua->request_timeout( $conf->{'api_timeout'} );
29  
30 $app->helper(
31 3 ▶ recaptcha_get_html => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
32 3 my $c = shift;
33 3 ▶ my $language = $_[0] ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 2 3
34  
35 3 my %data_attr = map { $_ => $plugin->conf->{$_} } grep { index( $_, 'api_' ) != 0 } keys %{ $plugin->conf };
36  
37 # Never expose this!
38 3 delete $data_attr{'secret'};
39  
40 3 my $hl = '';
41 3 ▶ if ( defined $language and $language ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $language and $language) { } 1 2 3
elsif (exists $data_attr{'language'}) { } 0 2 2
Condition66%2/3
defined $language and $language
!ll&&!rl&&r
201
MC/DC50%1/2
defined $language and $language
defined $language $language
Truth table66%2/3
A and B
A= defined $languageB= $language
ABresult
0X0
100
111
42 1 $hl = $language;
43 }
44 elsif ( exists $data_attr{'language'} ) {
45 0 $hl = delete $data_attr{'language'};
46 }
47  
48 3 my $output = '';
49 3 my $template = q|<script src="https://www.google.com/recaptcha/api.js?hl=<%= $hl %>" async defer></script>
50 <div class="g-recaptcha"<% foreach my $k ( sort keys %{$attr} ) { %> data-<%= $k %>="<%= $attr->{$k} %>"<% } %>></div>|;
51  
52 3 return $c->render_to_string(
53 handler => 'ep',
54 inline => $template,
55 hl => $hl,
56 attr => \%data_attr,
57 );
58 }
59 5 );
60  
61 $app->helper(
62 2 ▶ recaptcha_verify => sub {
Subroutine __ANON__: called CC=5 SCAR=18.9
63 2 my $c = shift;
64  
65 2 my $cb = shift;
66 2 ▶ unless ( defined( $cb ) and ref( $cb ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cb and ref $cb eq "CODE") 2 0 2
Condition33%1/3
defined $cb and ref $cb eq "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $cb and ref $cb eq "CODE"
defined $cb ref $cb eq "CODE"
Truth table33%1/3
A and B
A= defined $cbB= ref $cb eq "CODE"
ABresult
0X0
100
111
67 2 $cb = '';
68 }
69  
70 my %verify_params = (
71 remoteip => $c->tx->remote_address,
72 response => ( $c->req->param( 'g-recaptcha-response' ) || '' ),
73 2 ▶ secret => $plugin->conf->{'secret'},
Condition50%1/2
$c->req->param('g-recaptcha-response') || ''
l!l
20
MC/DC0%0/1
$c->req->param('g-recaptcha-response') || ''
$c->req->param('g-recaptcha-response')
Truth table50%1/2
A
A= $c->req->param('g-recaptcha-response')
Aresult
11
00
74 );
75  
76 2 my $url = $plugin->conf->{'api_url'};
77  
78 2 ▶ my $response_handler = sub {
Subroutine __ANON__: called CC=7 SCAR=24.6
79 2 my ( $ua, $tx ) = @_;
80  
81 2 my ( $verified, $err ) = ( 0, [] );
82 2 ▶ if ( my $txerr = $tx->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $txerr = $tx->error) { } 0 2 2
83 0 my $txt = 'Retrieving captcha verifcation failed';
84 0 ▶ $txt .= ' (HTTP ' . $txerr->{'code'} . ')' if $txerr->{'code'};
Branch: 1 branch
BranchTrueFalseTotal
if $txerr->{"code"} 0 0 0
85  
86 0 $c->app->log->error( $txt . ': ' . $txerr->{'message'} );
87 0 $c->app->log->error( 'Request was: ' . $tx->req->to_string );
88  
89 0 ( $verified, $err ) = ( 0, ['x-http-communication-failed'] );
90 }
91 else {
92 2 my $json = '';
93 2 eval { $json = Mojo::JSON::decode_json( $tx->res->body ); };
94  
95 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
96 0 $c->app->log->error( 'Decoding JSON response failed: ' . $@ );
97 0 $c->app->log->error( 'Request was: ' . $tx->req->to_string );
98 0 $c->app->log->error( 'Response was: ' . $tx->res->to_string );
99  
100 0 ( $verified, $err ) = ( 0, ['x-unparseable-data-received'] );
101 }
102 else {
103 2 ▶ unless ( $json->{'success'} == Mojo::JSON->true ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"success"} == "Mojo::JSON"->true) 2 0 2
104 2 ▶ @{$err} = @{ $json->{'error-codes'} // [] };
Condition50%1/2
$json->{"error-codes"} // []
l!l
20
MC/DC0%0/1
$json->{"error-codes"} // []
$json->{"error-codes"}
Truth table50%1/2
A
A= $json->{"error-codes"}
Aresult
11
00
105 }
106 2 $verified = $json->{'success'};
107 }
108 }
109  
110 2 ▶ return $cb->( $verified, $err ) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 2 2
111 2 return ( $verified, $err );
112 2 };
113  
114 2 ▶ if ( $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 2 2
115 0 return $plugin->ua->post( $url, form => \%verify_params, $response_handler );
116 }
117 else {
118 2 my $tx = $plugin->ua->post( $url, form => \%verify_params );
119 2 return $response_handler->( $plugin->ua, $tx );
120 }
121 }
122 5 );
123  
124 $app->helper(
125 1 ▶ recaptcha_verify_p => sub {
Subroutine __ANON__: called CC=2 SCAR=8.7
126 1 my $c = shift;
127  
128 my %verify_params = (
129 remoteip => $c->tx->remote_address,
130 response => ( $c->req->param( 'g-recaptcha-response' ) || '' ),
131 1 ▶ secret => $plugin->conf->{'secret'},
Condition50%1/2
$c->req->param('g-recaptcha-response') || ''
l!l
10
MC/DC0%0/1
$c->req->param('g-recaptcha-response') || ''
$c->req->param('g-recaptcha-response')
Truth table50%1/2
A
A= $c->req->param('g-recaptcha-response')
Aresult
11
00
132 );
133  
134 1 my $url = $plugin->conf->{'api_url'};
135  
136 # Async request using promises
137 1 require Mojo::Promise;
138 1 my $p = Mojo::Promise->new();
139 $plugin->ua->post(
140 $url => form => \%verify_params,
141 1 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=25.4
142 1 my ( $ua, $tx ) = @_;
143  
144 1 ▶ if ( my $err = $tx->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) 0 1 1
145 0 my $txt = 'Retrieving captcha verification failed';
146 0 ▶ $txt .= ' (HTTP ' . $err->{'code'} . ')' if $err->{'code'};
Branch: 1 branch
BranchTrueFalseTotal
if $err->{"code"} 0 0 0
147  
148 0 $c->app->log->error( $txt . ': ' . $err->{'message'} );
149 0 $c->app->log->error( 'Request was: ' . $tx->req->to_string );
150 0 return $p->reject( ['x-http-communication-failed'] );
151 }
152  
153 1 my $res = $tx->res;
154 1 my $json = eval { $res->json };
155  
156 1 ▶ if ( not defined $json ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $json) 0 1 1
157 0 $c->app->log->error( 'Decoding JSON response failed: ' . $@ );
158 0 $c->app->log->error( 'Request was: ' . $tx->req->to_string );
159 0 $c->app->log->error( 'Response was: ' . $tx->res->to_string );
160 0 return $p->reject( ['x-unparseable-data-received'] );
161 }
162  
163 1 ▶ unless ( $json->{'success'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json->{"success"}) 1 0 1
164 1 ▶ return $p->reject( $json->{'error-codes'} // [] );
Condition50%1/2
$json->{"error-codes"} // []
l!l
10
MC/DC0%0/1
$json->{"error-codes"} // []
$json->{"error-codes"}
Truth table50%1/2
A
A= $json->{"error-codes"}
Aresult
11
00
165 }
166  
167 0 return $p->resolve( $json->{'success'} );
168 }
169 1 );
170  
171 1 return $p;
172 }
173 5 );
174  
175 5 return;
176 } ## end sub register
177  
178 6 1;
179  
180 __END__
↑ Summary