blib/lib/Lemonldap/NG/Portal/Captcha/ReCaptcha.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 64 CC 6 SCAR 27.4
CC
6
Cov
0%
check_captcha
30.0
init
6.0
init_captcha
2.0
CRAP 15.5
1 package Lemonldap::NG::Portal::Captcha::ReCaptcha;
2  
3 use strict;
4 use Mouse;
5 use Lemonldap::NG::Common::UserAgent;
6  
7 # Add constants used by this module
8  
9 our $VERSION = '2.20.0';
10  
11 extends 'Lemonldap::NG::Portal::Main::Plugin';
12  
13 has ua => (
14 is => 'rw',
15 lazy => 1,
16 builder => sub {
17 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
18 $ua->env_proxy();
19 return $ua;
20 }
21 );
22  
23 sub init {
24 my ($self) = @_;
25 unless ($self->conf->{captchaOptions}->{dataSiteKey}
26 and $self->conf->{captchaOptions}->{secretKey} )
27 {
28 $self->logger->error('Missing required options for reCaptcha');
29 return 0;
30 }
31 return 1;
32 }
33  
34 sub init_captcha {
35 my ( $self, $req ) = @_;
36  
37 $req->data->{customScript} .=
38 '<script src="https://www.google.com/recaptcha/enterprise.js"></script>';
39  
40 # Read option from the manager configuration
41 my $dataSiteKey = $self->conf->{captchaOptions}->{dataSiteKey};
42 my $html =
43 qq'<div class="g-recaptcha" data-sitekey="$dataSiteKey" data-action="LOGIN"></div>';
44 $req->captchaHtml($html);
45 }
46  
47 sub check_captcha {
48 my ( $self, $req ) = @_;
49  
50 my $captcha_input = $req->param('g-recaptcha-response');
51 unless ($captcha_input) {
52 $self->logger->info('No captcha value submitted');
53 return 0;
54 }
55 my $response = $self->ua->post(
56 'https://www.google.com/recaptcha/api/siteverify',
57 {
58 secret => $self->conf->{captchaOptions}->{secretKey},
59 response => $captcha_input,
60 }
61 );
62 if ( $response->is_success ) {
63 my $res = eval { JSON::from_json( $response->decoded_content ) };
64 if ($@) {
65 $self->logger->error("reCaptcha: $@");
66 return 0;
67 }
68 unless ( $res->{success} ) {
69 $self->logger->info(
70 'reCaptcha errors:' . $response->decoded_content );
71 }
72 return $res->{success};
73 }
74 $self->logger->error( 'reCaptcha error: ' . $response->status_line );
75 return 0;
76 }
77  
78 1;
79