« blib/lib/Lemonldap/NG/Portal/Captcha/ReCaptcha.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/InitializePasswordReset.pm »
| 1 | package Lemonldap::NG::Portal::Captcha::ReCaptcha3; | ||
| 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.21.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 | has score => ( | ||
| 24 | is => 'ro', | ||
| 25 | default => sub { | ||
| 26 | return $_[0]->{conf}->{captchaOptions}->{score} || 0.5; | ||
| 27 | } | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub init { | ||
| 31 | my ($self) = @_; | ||
| 32 | unless ($self->conf->{captchaOptions}->{dataSiteKey} | ||
| 33 | and $self->conf->{captchaOptions}->{secretKey} ) | ||
| 34 | { | ||
| 35 | $self->logger->error('Missing required options for reCaptcha'); | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub init_captcha { | ||
| 42 | my ( $self, $req ) = @_; | ||
| 43 | |||
| 44 | $req->data->{customScript} .= | ||
| 45 | '<script src="https://www.google.com/recaptcha/api.js?render=' | ||
| 46 | . $self->conf->{captchaOptions}->{dataSiteKey} | ||
| 47 | . '"></script>' | ||
| 48 | . '<script type="application/init"> | ||
| 49 | { | ||
| 50 | "datasitekey": "' . $self->conf->{captchaOptions}->{dataSiteKey} . '" | ||
| 51 | } | ||
| 52 | </script>' | ||
| 53 | . '<script src="/static/common/js/recaptchav3.js"></script>'; | ||
| 54 | |||
| 55 | # Read option from the manager configuration | ||
| 56 | my $dataSiteKey = $self->conf->{captchaOptions}->{dataSiteKey}; | ||
| 57 | my $html = | ||
| 58 | qq'<div class="g-recaptcha" data-sitekey="$dataSiteKey" data-action="LOGIN"> | ||
| 59 | <input type="hidden" id="grr" name="g-recaptcha-response" /> | ||
| 60 | </div>'; | ||
| 61 | $req->captchaHtml($html); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub check_captcha { | ||
| 65 | my ( $self, $req ) = @_; | ||
| 66 | |||
| 67 | my $captcha_input = $req->param('g-recaptcha-response'); | ||
| 68 | unless ($captcha_input) { | ||
| 69 | $self->logger->info('No captcha value submitted'); | ||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | my $response = $self->ua->post( | ||
| 73 | 'https://www.google.com/recaptcha/api/siteverify', | ||
| 74 | { | ||
| 75 | secret => $self->conf->{captchaOptions}->{secretKey}, | ||
| 76 | response => $captcha_input, | ||
| 77 | } | ||
| 78 | ); | ||
| 79 | if ( $response->is_success ) { | ||
| 80 | my $res = eval { JSON::from_json( $response->decoded_content ) }; | ||
| 81 | if ($@) { | ||
| 82 | $self->logger->error("reCaptcha: $@"); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | my $success = | ||
| 86 | $res->{success} | ||
| 87 | and $res->{score} | ||
| 88 | and ( $res->{score} >= $self->score ); | ||
| 89 | unless ($success) { | ||
| 90 | $self->logger->info( | ||
| 91 | 'reCaptcha errors:' . $response->decoded_content ); | ||
| 92 | } | ||
| 93 | return $success; | ||
| 94 | } | ||
| 95 | $self->logger->error( 'reCaptcha error: ' . $response->status_line ); | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 |