| 1 | package Captcha::noCAPTCHA; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use JSON::PP qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.16'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=20.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 3 | my ($class,$args) = @_; | |||||||||||||||||||||||||||
| 12 | 3 | my $self = bless {} ,$class; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | $self->site_key($args->{site_key}) || die "site_key required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | $self->secret_key($args->{secret_key}) || die "secret_key required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | $self->theme($args->{theme} || 'light'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'theme'} || 'light'
MC/DC0%0/1
$args->{'theme'} || 'light'
$args->{'theme'}
Truth table50%1/2
A
A= $args->{'theme'}
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | $self->noscript($args->{noscript} || 0); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'noscript'} || 0
MC/DC0%0/1
$args->{'noscript'} || 0
$args->{'noscript'}
Truth table50%1/2
A
A= $args->{'noscript'}
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | $self->api_url($args->{api_url} || 'https://www.google.com/recaptcha/api/siteverify'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'api_url'} || 'https://www.google.com/recaptcha/api/siteverify'
MC/DC100%1/1
$args->{'api_url'} || 'https://www.google.com/recaptcha/api/siteverify'
$args->{'api_url'}
Truth table100%2/2
A
A= $args->{'api_url'}
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | $self->api_timeout($args->{api_timeout} || 10); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'api_timeout'} || 10
MC/DC0%0/1
$args->{'api_timeout'} || 10
$args->{'api_timeout'}
Truth table50%1/2
A
A= $args->{'api_timeout'}
| |||||||||||||||||||||||||||||
| 19 | 3 | return $self; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | sub site_key { return shift->_get_set('site_key',@_); } | ||||||||||||||||||||||||||
|
Subroutine
site_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | sub secret_key { return shift->_get_set('secret_key',@_); } | ||||||||||||||||||||||||||
|
Subroutine
secret_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | sub theme { return shift->_get_set('theme',@_); } | ||||||||||||||||||||||||||
|
Subroutine
theme: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 10 | ▶ | sub noscript { return shift->_get_set('noscript',@_); } | ||||||||||||||||||||||||||
|
Subroutine
noscript: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | sub api_url { return shift->_get_set('api_url',@_); } | ||||||||||||||||||||||||||
|
Subroutine
api_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | sub api_timeout { return shift->_get_set('api_timeout',@_); } | ||||||||||||||||||||||||||
|
Subroutine
api_timeout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | sub errors { return shift->{_attrs}->{errors}; } | ||||||||||||||||||||||||||
|
Subroutine
errors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | sub response { return shift->{_response}; } | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | sub html { | ||||||||||||||||||||||||||
|
Subroutine
html: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 33 | 5 | ▶ | my $key = $self->site_key || die "site_key required!"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->site_key || die('site_key required!')
MC/DC0%0/1
$self->site_key || die('site_key required!')
$self->site_key
Truth table50%1/2
A
A= $self->site_key
| |||||||||||||||||||||||||||||
| 34 | 5 | my $theme = $self->theme; | |||||||||||||||||||||||||||
| 35 | 5 | my $output=<<EOT; | |||||||||||||||||||||||||||
| 36 | <script src="https://www.google.com/recaptcha/api.js" async defer></script> | ||||||||||||||||||||||||||||
| 37 | <div class="g-recaptcha" data-sitekey="$key" data-theme="$theme"></div> | ||||||||||||||||||||||||||||
| 38 | EOT | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 5 | ▶ | if ($self->noscript) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | $output.=<<EOT; | |||||||||||||||||||||||||||
| 42 | <noscript> | ||||||||||||||||||||||||||||
| 43 | <div style="width: 302px; height: 462px;"> | ||||||||||||||||||||||||||||
| 44 | <div style="width: 302px; height: 462px; position: relative;"> | ||||||||||||||||||||||||||||
| 45 | <div style="width: 302px; height: 422px; position: absolute;"> | ||||||||||||||||||||||||||||
| 46 | <iframe src="https://www.google.com/recaptcha/api/fallback?k=$key" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> | ||||||||||||||||||||||||||||
| 47 | </div> | ||||||||||||||||||||||||||||
| 48 | <div style="width: 300px; height: 60px; border-style: none; bottom: 0px; left: 0px; margin: 0px; padding: 0px; right: 0px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; position: absolute;"> | ||||||||||||||||||||||||||||
| 49 | <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none;"></textarea> | ||||||||||||||||||||||||||||
| 50 | </div> | ||||||||||||||||||||||||||||
| 51 | </div> | ||||||||||||||||||||||||||||
| 52 | </div> | ||||||||||||||||||||||||||||
| 53 | </noscript> | ||||||||||||||||||||||||||||
| 54 | EOT | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 5 | return $output; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub verify { | ||||||||||||||||||||||||||
|
Subroutine
verify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($self,$value,$ip) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | my $params = $self->_build_request($value,$ip); | |||||||||||||||||||||||||||
| 63 | 0 | my $http = HTTP::Tiny->new(timeout => $self->api_timeout); | |||||||||||||||||||||||||||
| 64 | 0 | my $response = $http->post_form( $self->api_url, $params ); | |||||||||||||||||||||||||||
| 65 | 0 | return $self->_parse_response($response); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | sub _build_request { | ||||||||||||||||||||||||||
|
Subroutine
_build_request: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 69 | 2 | my ($self,$value,$ip) = @_; | |||||||||||||||||||||||||||
| 70 | 2 | $self->{_attrs}->{errors} = []; | |||||||||||||||||||||||||||
| 71 | 2 | my $args = { secret => $self->secret_key }; | |||||||||||||||||||||||||||
| 72 | 2 | ▶ | $args->{response} = $value if ($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | $args->{remoteip} = $ip if ($ip); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | return $args; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 9 | ▶ | sub _parse_response { | ||||||||||||||||||||||||||
|
Subroutine
_parse_response: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 78 | 9 | my ($self,$response) = @_; | |||||||||||||||||||||||||||
| 79 | 9 | ▶ | if (!$response || !ref($response)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$response and ref $response
MC/DC100%2/2
$response and ref $response
$response
ref $response
Truth table100%3/3
A and B
A= $responseB= ref $response
| |||||||||||||||||||||||||||||
| 80 | 3 | $self->{_attrs}->{errors} = ['http-tiny-no-response']; | |||||||||||||||||||||||||||
| 81 | 3 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 6 | ▶ | if (!$response->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | my $status = $response->{status} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$response->{'status'} || 0
MC/DC100%1/1
$response->{'status'} || 0
$response->{'status'}
Truth table100%2/2
A
A= $response->{'status'}
| |||||||||||||||||||||||||||||
| 85 | 2 | $self->{_attrs}->{errors} = [sprintf('status-code-%d',$status)]; | |||||||||||||||||||||||||||
| 86 | 2 | return; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 4 | ▶ | if (!$response->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | $self->{_attrs}->{errors} = ['no-content-returned']; | |||||||||||||||||||||||||||
| 90 | 2 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 2 | my $json = eval {JSON::PP::decode_json($response->{content})}; | |||||||||||||||||||||||||||
| 93 | 2 | ▶ | if (!$json) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $self->{_attrs}->{errors} = ['invalid-json']; | |||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 2 | $self->{_response} = $json; | |||||||||||||||||||||||||||
| 98 | 2 | $self->{_attrs}->{errors} = $json->{'error-codes'}; | |||||||||||||||||||||||||||
| 99 | 2 | return $json->{success}; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 38 | ▶ | sub _get_set { | ||||||||||||||||||||||||||
|
Subroutine
_get_set: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 38 | my ($self,$name,@args) = @_; | |||||||||||||||||||||||||||
| 104 | 38 | ▶ | $self->{_attrs}->{$name} = $args[0] if (@args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 38 | return $self->{_attrs}->{$name}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 NAME | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Captcha::noCAPTCHA - Simple implementation of Google's noCAPTCHA reCAPTCHA for perl | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | The following is example usage to include captcha in page. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | my $cap = Captcha::noCAPTCHA->new({site_key => "your site key",secret_key => "your secret key"}); | ||||||||||||||||||||||||||||
| 119 | my $html = $cap->html; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Include $html in your form page. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | The following is example usage to verify captcha response. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | my $cap = Captcha::noCAPTCHA->new({site_key => "your site key",secret_key => "your secret key"}); | ||||||||||||||||||||||||||||
| 127 | my $cgi = CGI->new; | ||||||||||||||||||||||||||||
| 128 | my $captcha_response = $cgi->param('g-recaptcha-response'); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | if ($cap->verify($captcha_response',$cgi->remote_addr)) { | ||||||||||||||||||||||||||||
| 131 | # Process the rest of the form. | ||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||
| 133 | # Tell user he/she needs to prove his/her humanity. | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 METHODS | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 html | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Accepts no arguments. Returns CAPTCHA html to be rendered with form. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 verify($g_captcha_response,$users_ip_address?) | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Required $g_captcha_response. Input parameter from form containing g_captcha_response | ||||||||||||||||||||||||||||
| 145 | Optional $users_ip_address. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 errors() | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Returns an array ref of errors if verify call fails. List of possible errors: | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | missing-input-secret The secret parameter is missing. | ||||||||||||||||||||||||||||
| 152 | invalid-input-secret The secret parameter is invalid or malformed. | ||||||||||||||||||||||||||||
| 153 | missing-input-response The response parameter is missing. | ||||||||||||||||||||||||||||
| 154 | invalid-input-response The response parameter is invalid or malformed. | ||||||||||||||||||||||||||||
| 155 | http-tiny-no-response HTTP::Tiny did not return anything. No further information available. | ||||||||||||||||||||||||||||
| 156 | status-code-DDD Where DDD is the status code returned from the server. | ||||||||||||||||||||||||||||
| 157 | no-content-returned Call was successful, but no content was returned. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 response() | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Returns the response hashref for the most recent captcha response. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 FIELD OPTIONS | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Support for the following field options, over what is inherited from | ||||||||||||||||||||||||||||
| 166 | L<HTML::FormHandler::Field> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 site_key | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Required. The site key you get when you create an account on L<https://www.google.com/recaptcha/> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 secret_key | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Required. The secret key you get when you create an account on L<https://www.google.com/recaptcha/> | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 theme | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Optional. The color theme of the widget. Options are 'light ' or 'dark' (Default: light) | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 noscript | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Optional. When true, includes the <noscript> markup in the rendered html. (Default: false) | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head2 api_url | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Optional. URL to the Google API. Defaults to https://www.google.com/recaptcha/api/siteverify | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head2 api_timeout | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Optional. Seconds to wait for Google API to respond. Default is 10 seconds. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | The following modules or resources may be of interest. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | L<HTML::FormHandlerX::Field::noCAPTCHA> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Chuck Larson C<< <clarson@cpan.org> >> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | leejo C<< <leejo@cpan.org> >> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Copyright 2017, Chuck Larson C<< <chuck+github@endcapsoftwware.com> >> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This projects work sponsered by End Cap Software, LLC. | ||||||||||||||||||||||||||||
| 211 | L<http://www.endcapsoftware.com> | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 214 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||