| 1 | package Web::VWF::CAPTCHA; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version; our $VERSION = version->declare('0.003.0'); | ||
| 7 | use Carp qw(croak); | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use JSON::MaybeXS; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, %args) = @_; | ||
| 13 | |||
| 14 | # Checked one at a time: "die LIST" inside the hash below would swallow | ||
| 15 | # every key after it, losing the secret and the logger | ||
| 16 | my $site_key = $args{site_key} or croak('site_key required'); | ||
| 17 | my $secret_key = $args{secret_key} or croak('secret_key required'); | ||
| 18 | |||
| 19 | my $self = { | ||
| 20 | site_key => $site_key, | ||
| 21 | secret_key => $secret_key, | ||
| 22 | logger => $args{logger}, | ||
| 23 | }; | ||
| 24 | |||
| 25 | return bless $self, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub verify { | ||
| 29 | my ($self, $response_token, $remote_ip) = @_; | ||
| 30 | |||
| 31 | return 0 unless $response_token; | ||
| 32 | $remote_ip //= 'unknown'; # Only used in log messages | ||
| 33 | |||
| 34 | my $ua = LWP::UserAgent->new(timeout => 10); | ||
| 35 | |||
| 36 | my $response = $ua->post('https://www.google.com/recaptcha/api/siteverify', { | ||
| 37 | secret => $self->{secret_key}, | ||
| 38 | response => $response_token, | ||
| 39 | remoteip => $remote_ip, | ||
| 40 | }); | ||
| 41 | |||
| 42 | unless ($response->is_success) { | ||
| 43 | $self->{logger}->error("reCAPTCHA verification failed: " . $response->status_line) if $self->{logger}; | ||
| 44 | return 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | # A malformed reply is a failed verification, not a crash | ||
| 48 | my $result = eval { decode_json($response->decoded_content()) }; | ||
| 49 | if($@ || (ref($result) ne 'HASH')) { | ||
| 50 | $self->{logger}->error("reCAPTCHA verification failed: unparsable reply from $remote_ip") if $self->{logger}; | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | if ($result->{success}) { | ||
| 55 | $self->{logger}->info("reCAPTCHA verification successful for $remote_ip") if $self->{logger}; | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | $self->{logger}->warn("reCAPTCHA verification failed for $remote_ip: " . join(', ', @{$result->{'error-codes'} || []})) | ||
| 60 | if $self->{logger}; | ||
| 61 | |||
| 62 | return 0; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub get_site_key { | ||
| 66 | my $self = $_[0]; | ||
| 67 | return $self->{site_key}; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | |||
| 72 | =encoding utf8 | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | Web::VWF::CAPTCHA - Check a Google reCAPTCHA answer | ||
| 77 | |||
| 78 | =head1 VERSION | ||
| 79 | |||
| 80 | Version 0.003.0 | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | my $captcha = Web::VWF::CAPTCHA->new(site_key => $site, secret_key => $secret, logger => $logger); | ||
| 85 | if($captcha->verify($info->param('g-recaptcha-response'), $ENV{'REMOTE_ADDR'})) { | ||
| 86 | # a person | ||
| 87 | } | ||
| 88 | |||
| 89 | =head1 DESCRIPTION | ||
| 90 | |||
| 91 | Used by L<Web::VWF> when a client has been shown the CAPTCHA page because it went | ||
| 92 | over the rate limit. The keys are the C<recaptcha> settings, see | ||
| 93 | L<Web::VWF/CONFIGURATION>. | ||
| 94 | |||
| 95 | =head1 METHODS | ||
| 96 | |||
| 97 | =head2 new | ||
| 98 | |||
| 99 | Takes C<site_key> and C<secret_key>, both required, and optionally a C<logger>. | ||
| 100 | Croaks when either key is missing. | ||
| 101 | |||
| 102 | =head3 API SPECIFICATION | ||
| 103 | |||
| 104 | =head4 INPUT | ||
| 105 | |||
| 106 | { | ||
| 107 | site_key => { type => 'string' }, | ||
| 108 | secret_key => { type => 'string' }, | ||
| 109 | logger => { type => 'object', optional => 1 }, | ||
| 110 | } | ||
| 111 | |||
| 112 | =head4 OUTPUT | ||
| 113 | |||
| 114 | { type => 'object', isa => 'Web::VWF::CAPTCHA' } | ||
| 115 | |||
| 116 | =head2 verify | ||
| 117 | |||
| 118 | Asks Google whether the answer in C<$response_token> is a good one, with a | ||
| 119 | ten-second timeout. The second argument is the client's address, which Google | ||
| 120 | is told and which is used in log messages. Anything that goes wrong, such as no | ||
| 121 | token, no reply or a reply that cannot be read, is a failed verification and not | ||
| 122 | an error. | ||
| 123 | |||
| 124 | =head3 API SPECIFICATION | ||
| 125 | |||
| 126 | =head4 INPUT | ||
| 127 | |||
| 128 | { response_token => { type => 'string' }, remote_ip => { type => 'string', optional => 1 } } | ||
| 129 | |||
| 130 | =head4 OUTPUT | ||
| 131 | |||
| 132 | { type => 'boolean' } # 1 or 0 | ||
| 133 | |||
| 134 | =head2 get_site_key | ||
| 135 | |||
| 136 | Returns the site key, which the CAPTCHA page puts in the form. | ||
| 137 | |||
| 138 | =head3 API SPECIFICATION | ||
| 139 | |||
| 140 | =head4 INPUT | ||
| 141 | |||
| 142 | {} | ||
| 143 | |||
| 144 | =head4 OUTPUT | ||
| 145 | |||
| 146 | { type => 'string' } | ||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 151 | |||
| 152 | =head1 LICENCE AND COPYRIGHT | ||
| 153 | |||
| 154 | Copyright 2010-2026 Nigel Horne. | ||
| 155 | |||
| 156 | Usage is subject to the GPL2 licence terms. | ||
| 157 | If you use it, | ||
| 158 | please let me know. | ||
| 159 | |||
| 160 | =cut |