| 1 | package HTML::FormHandlerX::Field::noCAPTCHA; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Captcha::noCAPTCHA; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | our $VERSION = '0.07'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | extends 'HTML::FormHandler::Field'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | has '+widget' => ( default => 'noCAPTCHA' ); | |||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | has '+input_param' => ( default => 'g-recaptcha-response' ); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | has '+messages' => ( default => sub { {required => 'You must prove your Humanity!'} }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | has '+required' => ( default => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | has [qw/site_key secret_key/] => (is=>'rw', isa=>'Str', required => 1,lazy_build => 1); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | has 'theme' => (is=>'ro',isa=> enum([qw(dark light)]),default => 'light'); | |||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | has 'noscript' => (is=>'ro',isa=> 'Bool',default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | has 'remote_address' => (is=>'ro', isa=>'Str', required => 1, lazy_build => 1); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | has 'api_url' => (is=>'ro', isa=>'Str', required => 1, lazy_build => 1); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | has 'api_timeout' => (is=>'ro', isa=>'Int', required => 1, lazy_build => 1); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | has 'g_captcha_message' => (is=>'ro', isa=>'Str', default=>'You\'ve failed to prove your Humanity!'); | |||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | has 'g_captcha_failure_message' => (is=>'ro', isa=>'Str', default=>'We\'ve had trouble processing your request, please try again.'); | |||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | has 'config_key' => (is=>'ro', isa=>'Str', default=> __PACKAGE__); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | has '_nocaptcha' => (is=>'ro', isa=>'Captcha::noCAPTCHA', lazy_build => 1); | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | sub _build_remote_address { | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_remote_address: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return '' unless ($self->form->ctx); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | return $self->form->ctx->req->address; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | sub _build_secret_key { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_secret_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $config = $self->_g_captcha_config || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->_g_captcha_config || (return)
MC/DC0%0/1
$self->_g_captcha_config || (return)
$self->_g_captcha_config
Truth table0%0/2
A
A= $self->_g_captcha_config
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return $config->{secret_key}; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | sub _build_site_key { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_site_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $config = $self->_g_captcha_config || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->_g_captcha_config || (return)
MC/DC0%0/1
$self->_g_captcha_config || (return)
$self->_g_captcha_config
Truth table0%0/2
A
A= $self->_g_captcha_config
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | return $config->{site_key}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | sub _build_api_url { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_url: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my $config = $self->_g_captcha_config; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | return $config && $config->{api_url} ? $config->{api_url} : 'https://www.google.com/recaptcha/api/siteverify'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$config and $config->{"api_url"}
MC/DC0%0/2
$config and $config->{"api_url"}
$config
$config->{"api_url"}
Truth table33%1/3
A and B
A= $configB= $config->{"api_url"}
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | sub _build_api_timeout { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_timeout: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | my $config = $self->_g_captcha_config; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | return $config && exists $config->{api_timeout} ? $config->{api_timeout} : 10; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$config and exists $config->{"api_timeout"}
MC/DC0%0/2
$config and exists $config->{"api_timeout"}
$config
exists $config->{"api_timeout"}
Truth table33%1/3
A and B
A= $configB= exists $config->{"api_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | sub _g_captcha_config { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_g_captcha_config: called CC=4 SCAR=18.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | return unless ($self->form && $self->form->ctx && $self->form->ctx->config); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$self->form and $self->form->ctx and $self->form->ctx->config
$self->form and $self->form->ctx
MC/DC0%0/3
$self->form and $self->form->ctx and $self->form->ctx->config
$self->form
$self->form->ctx
$self->form->ctx->config
Truth table25%1/4
A and B and C
A= $self->formB= $self->form->ctxC= $self->form->ctx->config
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return $self->form->ctx->config->{$self->config_key}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | sub _build__nocaptcha { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__nocaptcha: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | return Captcha::noCAPTCHA->new({ | |||||||||||||||||||||||||||||||||||||||||
| 67 | api_url => $self->api_url, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | api_timeout => $self->api_timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | site_key => $self->site_key, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | secret_key => $self->secret_key, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | theme => $self->theme, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | noscript => $self->noscript, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | sub validate { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | my $cap = $self->_nocaptcha; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my $success = $cap->verify( $self->value, $self->remote_address ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | if (not defined $success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | $self->add_error($self->g_captcha_failure_message); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } elsif (!$success) { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | $self->add_error($self->g_captcha_message); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | HTML::FormHandlerX::Field::noCAPTCHA - Google's noCAPTCHA reCAPTCHA for HTML::FormHandler | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | The following is example usage. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | In your L<HTML::FormHandler> subclass, "YourApp::HTML::Forms::YourForm": | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | has_field 'nocaptcha' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 106 | type=>'noCAPTCHA', | ||||||||||||||||||||||||||||||||||||||||||
| 107 | site_key=>'[YOUR SITE KEY]', | ||||||||||||||||||||||||||||||||||||||||||
| 108 | secret_key=>'[YOUR SECRET KEY]', | ||||||||||||||||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | Example L<Catalyst> controller: | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | my $form = YourApp::HTML::Forms::YourForm->new({ctx => $c}); | ||||||||||||||||||||||||||||||||||||||||||
| 114 | my $params = $c->request->body_parameters; | ||||||||||||||||||||||||||||||||||||||||||
| 115 | if($form->process($c->req->body_paramseters) { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | ## Do something with the form. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | ## Redisplay form and ask to try again. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | Example L<Catalyst> config: | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | __PACKAGE__->config( | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 'HTML::FormHandlerX::Field::noCAPTCHA' => { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | site_key => '[YOUR SITE KEY]', | ||||||||||||||||||||||||||||||||||||||||||
| 126 | secret_key => '[YOUR SECRET KEY]-IaHaF3jRN4j340MkGLNw6tcDu', | ||||||||||||||||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head1 FIELD OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | Support for the following field options, over what is inherited from | ||||||||||||||||||||||||||||||||||||||||||
| 133 | L<HTML::FormHandler::Field> | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =head2 site_key | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | Required. The site key you get when you create an account on L<https://www.google.com/recaptcha/> | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head2 secret_key | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | Required. The secret key you get when you create an account on L<https://www.google.com/recaptcha/> | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =head2 theme | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | Optional. The color theme of the widget. Options are 'light ' or 'dark' (Default: light) | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head2 noscript | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Optional. When true, includes the <noscript> markup in the rendered html. (Default: false) | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =head2 remote_address | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | Optional. The user's IP address. Google states this is optional. If you are using | ||||||||||||||||||||||||||||||||||||||||||
| 154 | catalyst and pass the context to the form, noCAPTCHA will use it by default. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =head2 api_url | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | Optional. URL to the Google API. Defaults to https://www.google.com/recaptcha/api/siteverify | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =head2 api_timeout | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | Optional. Seconds to wait for Google API to respond. Default is 10 seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =head2 g_captcha_message | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | Optional. Message to display if user answers captcha incorrectly. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | Default is "You've failed to prove your Humanity!" | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head2 g_captcha_failure_message | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | Optional. Message to display if there was an issue with Google's API response. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | Default is "We've had trouble processing your request, please try again." | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =head2 config_key | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | Optional. When passing catalyst context to L<HTML::FormHandler>, uses this values | ||||||||||||||||||||||||||||||||||||||||||
| 177 | as the key to lookup configurations for this package. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | Default is HTML::FormHandlerX::Field::noCAPTCHA | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | The following modules or resources may be of interest. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | L<HTML::FormHandler> | ||||||||||||||||||||||||||||||||||||||||||
| 185 | L<Captcha::noCAPTCHA> | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | See it in action at L<https://www.httpuptime.com> | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | Chuck Larson C<< <clarson@cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | Copyright 2015, Chuck Larson C<< <chuck+github@endcapsoftwware.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | This projects work sponsered by End Cap Software, LLC. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | L<http://www.endcapsoftware.com> | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | Original work by John Napiorkowski C<< <jjnapiork@cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 203 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||||||||||||||||