| 1 | package Mojolicious::Plugin::ReCAPTCHAv2Async; | ||||||||||||
| 2 | # ABSTRACT: Adds async recaptcha_verify_p helper to Mojolicious ReCAPTCHAv2 plugin. | ||||||||||||
| 3 | 2 | $Mojolicious::Plugin::ReCAPTCHAv2Async::VERSION = '0.001'; | |||||||||||
| 4 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin::ReCAPTCHAv2'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Mojo::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use Mojo::Promise; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | has ua => sub { Mojo::UserAgent->new->max_redirects(0); }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | sub register { | ||||||||||
|
Subroutine
register: called CC=1 SCAR=0.6
Pod
documented
| |||||||||||||
| 11 | 1 | my $plugin = shift; | |||||||||||
| 12 | 1 | my ($app, $conf) = @_; | |||||||||||
| 13 | |||||||||||||
| 14 | 1 | $plugin->next::method(@_); | |||||||||||
| 15 | |||||||||||||
| 16 | 1 | $plugin->ua->request_timeout($plugin->conf->{'api_timeout'}); | |||||||||||
| 17 | |||||||||||||
| 18 | 2 | ▶ | $app->helper(recaptcha_verify_p => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.6
| |||||||||||||
| 19 | 2 | my $c = shift; | |||||||||||
| 20 | |||||||||||||
| 21 | my %verify_params = ( | ||||||||||||
| 22 | remote_ip => $c->tx->remote_address, | ||||||||||||
| 23 | response => ( $c->req->param('g-recaptcha-response') || '' ), | ||||||||||||
| 24 | 2 | ▶ | secret => $plugin->conf->{'secret'}, | ||||||||||
|
Condition50%1/2
$c->req->param('g-recaptcha-response') || ''
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')
| |||||||||||||
| 25 | ); | ||||||||||||
| 26 | |||||||||||||
| 27 | 2 | my $url = $plugin->conf->{'api_url'}; | |||||||||||
| 28 | 2 | my $timeout = $plugin->conf->{'api_timeout'}; | |||||||||||
| 29 | |||||||||||||
| 30 | 2 | my $p = Mojo::Promise->new(); | |||||||||||
| 31 | 2 | ▶ | $plugin->ua->post( $url => form => \%verify_params, sub { | ||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=24.9
| |||||||||||||
| 32 | 2 | my ($ua, $tx) = @_; | |||||||||||
| 33 | |||||||||||||
| 34 | 2 | ▶ | if (my $err = $tx->error) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | my $txt = 'Retrieving captcha verification failed'; | |||||||||||
| 36 | 0 | ▶ | $txt .= ' (HTTP ' . $err->{'code'} . ')' if $err->{'code'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | |||||||||||||
| 38 | 0 | $c->app->log->error( $txt . ': ' . $err->{'message'} ); | |||||||||||
| 39 | 0 | $c->app->log->error( 'Request was: ' . $tx->req->to_string ); | |||||||||||
| 40 | 0 | return $p->reject( 'x-http-communication-failed' ); | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 2 | my $res = $tx->res; | |||||||||||
| 44 | 2 | my $json = eval { $res->json }; | |||||||||||
| 45 | |||||||||||||
| 46 | 2 | ▶ | if (not defined $json) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 0 | $c->app->log->error( 'Decoding JSON response failed: ' . $@ ); | |||||||||||
| 48 | 0 | $c->app->log->error( 'Request was: ' . $tx->req->to_string ); | |||||||||||
| 49 | 0 | $c->app->log->error( 'Response was: ' . $tx->res->to_string ); | |||||||||||
| 50 | 0 | return $p->reject( 'x-unparseable-data-received' ); | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 2 | ▶ | if (not $json->{'success'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 2 | ▶ | return $p->reject( @{ $json->{'error-codes'} // [] } ); | ||||||||||
|
Condition50%1/2
$json->{"error-codes"} // []
MC/DC0%0/1
$json->{"error-codes"} // []
$json->{"error-codes"}
Truth table50%1/2
A
A= $json->{"error-codes"}
| |||||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 0 | return $p->resolve; | |||||||||||
| 58 | |||||||||||||
| 59 | 2 | }); | |||||||||||
| 60 | |||||||||||||
| 61 | 2 | return $p; | |||||||||||
| 62 | 1 | }); | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 2 | 1; | |||||||||||
| 66 | |||||||||||||
| 67 | __END__ | ||||||||||||