| 1 | package HTML::FormHandlerX::Field::reCAPTCHA; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Captcha::reCAPTCHA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Crypt::CBC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | extends 'HTML::FormHandler::Field'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 11 | 3 | our $AUTHORITY = 'cpan:JJNAPIORK'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | has '+widget' => ( default => 'reCAPTCHA' ); | |||||||||||||||||||||||||||
| 14 | 3 | has '+input_param' => ( default => 'recaptcha_response_field' ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | has [qw/public_key private_key/] => (is=>'rw', isa=>'Str', lazy_build=>1); | |||||||||||||||||||||||||||
| 17 | 3 | has 'use_ssl' => (is=>'rw', isa=>'Bool', required=>1, default=>0); | |||||||||||||||||||||||||||
| 18 | 3 | has 'remote_address' => (is=>'rw', isa=>'Str', lazy_build=>1); | |||||||||||||||||||||||||||
| 19 | 2 | ▶ | has 'recaptcha_options' => (is=>'rw', isa=>'HashRef', required=>1, default=>sub{ +{} }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | has 'recaptcha_message' => (is=>'rw', isa=>'Str', default=>'Error validating reCAPTCHA'); | |||||||||||||||||||||||||||
| 21 | 3 | has 'recaptcha_instance' => (is=>'ro', init_arg=>undef, lazy_build=>1); | |||||||||||||||||||||||||||
| 22 | 3 | has 'encrypter' => (is=>'ro', init_arg=>undef, lazy_build=>1, | |||||||||||||||||||||||||||
| 23 | handles=>[qw/encrypt_hex decrypt_hex/]); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub _build_public_key { | ||||||||||||||||||||||||||
|
Subroutine
_build_public_key: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 26 | 1 | my $self = shift @_; | |||||||||||||||||||||||||||
| 27 | 1 | my $form = $self->form; | |||||||||||||||||||||||||||
| 28 | 1 | my $method = $self->name.'_public_key'; | |||||||||||||||||||||||||||
| 29 | 1 | ▶ | if ($form->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 1 | return $form->$method; | |||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 0 | die "You either have to set the 'public_key' field option or defined a $method method in your form!"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub _build_private_key { | ||||||||||||||||||||||||||
|
Subroutine
_build_private_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 37 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 38 | 0 | my $form = $self->form; | |||||||||||||||||||||||||||
| 39 | 0 | my $method = $self->name.'_private_key'; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($form->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | return $form->$method; | |||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 0 | die "You either have to set the 'private_key' field option or defined a $method method in your form!"; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub _build_encrypter { | ||||||||||||||||||||||||||
|
Subroutine
_build_encrypter: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 49 | 0 | my $key = pack("H16",$self->private_key); | |||||||||||||||||||||||||||
| 50 | 0 | return Crypt::CBC->new(-key=>$key,-cipher=>"Blowfish"); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _build_remote_address { | ||||||||||||||||||||||||||
|
Subroutine
_build_remote_address: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 0 | $ENV{REMOTE_ADDR}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub _build_recaptcha_instance { | ||||||||||||||||||||||||||
|
Subroutine
_build_recaptcha_instance: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 1 | Captcha::reCAPTCHA->new(); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub prepare_private_recaptcha_args { | ||||||||||||||||||||||||||
|
Subroutine
prepare_private_recaptcha_args: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 63 | return ( | ||||||||||||||||||||||||||||
| 64 | 0 | $self->private_key, | |||||||||||||||||||||||||||
| 65 | $self->prepare_recaptcha_args, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub prepare_recaptcha_args { | ||||||||||||||||||||||||||
|
Subroutine
prepare_recaptcha_args: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift @_; | |||||||||||||||||||||||||||
| 71 | return ( | ||||||||||||||||||||||||||||
| 72 | $self->remote_address, | ||||||||||||||||||||||||||||
| 73 | $self->form->params->{'recaptcha_challenge_field'}, | ||||||||||||||||||||||||||||
| 74 | 0 | $self->form->params->{'recaptcha_response_field'}, | |||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 0 | my ($self, @rest) = @_; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | unless(my $super = $self->SUPER::validate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | return $super; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | my $recaptcha_response_field = $self->form->params->{'recaptcha_response_field'}; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | if($self->form->params->{'recaptcha_already_validated'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if($recaptcha_response_field && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$recaptcha_response_field and $self->decrypt_hex($recaptcha_response_field) eq $self->public_key
MC/DC0%0/2
$recaptcha_response_field and $self->decrypt_hex($recaptcha_response_field) eq $self->public_key
$recaptcha_response_field
$self->decrypt_hex($recaptcha_response_field) eq $self->public_key
Truth table0%0/3
A and B
A= $recaptcha_response_fieldB= $self->decrypt_hex($recaptcha_response_field) eq $self->public_key
| |||||||||||||||||||||||||||||
| 86 | ($self->decrypt_hex($recaptcha_response_field) eq $self->public_key) | ||||||||||||||||||||||||||||
| 87 | ) { | ||||||||||||||||||||||||||||
| 88 | 0 | return 1; | |||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 0 | $self->add_error("Previous reCAPTCHA validation lost. Please try again."); | |||||||||||||||||||||||||||
| 91 | 0 | return undef; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 0 | my @args = $self->prepare_private_recaptcha_args; | |||||||||||||||||||||||||||
| 95 | 0 | my $result = $self->recaptcha_instance->check_answer(@args); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if($result->{is_valid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | return 1; | |||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||
| 99 | 0 | $self->{recaptcha_error} = $result->{error}; | |||||||||||||||||||||||||||
| 100 | 0 | $self->add_error($self->recaptcha_message); | |||||||||||||||||||||||||||
| 101 | 0 | return undef; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 NAME | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | HTML::FormHandlerX::Field::reCAPTCHA - A Captcha::reCAPTCHA field for HTML::FormHandler | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | The following is example usage. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | In your L<HTML::FormHandler> subclass, "MyApp::HTML::Forms::MyForm": | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | has_field 'recaptcha' => ( | ||||||||||||||||||||||||||||
| 117 | type=>'reCAPTCHA', | ||||||||||||||||||||||||||||
| 118 | public_key=>'[YOUR PUBLIC KEY]', | ||||||||||||||||||||||||||||
| 119 | private_key=>'[YOUR PRIVATE KEY]', | ||||||||||||||||||||||||||||
| 120 | recaptcha_message => "You're failed to prove your Humanity!", | ||||||||||||||||||||||||||||
| 121 | required=>1, | ||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Example L<Catalyst> controller: | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | my $form = MyApp::HTML::Forms::MyForm->new; | ||||||||||||||||||||||||||||
| 127 | my $params = $c->request->body_parameters; | ||||||||||||||||||||||||||||
| 128 | if(my $result = $form->process(params=>$params) { | ||||||||||||||||||||||||||||
| 129 | ## The Form is totally valid. Go ahead with whatever is next. | ||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||
| 131 | ## Invalid results, you need to display errors and try again. | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Uses L<Captcha::reCAPTCHA> to add a "Check if the agent is human" field. You | ||||||||||||||||||||||||||||
| 137 | will need an account from L<http://recaptcha.net/> to make this work. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | This is a thin wrapper on top of L<Captcha::reCAPTCHA> so you should review the | ||||||||||||||||||||||||||||
| 140 | docs for that. However there's not much too it, just register for an account | ||||||||||||||||||||||||||||
| 141 | over at L<http://recaptcha.net> and use it. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 FIELD OPTIONS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | We support the following additional field options, over what is inherited from | ||||||||||||||||||||||||||||
| 146 | L<HTML::FormHandler::Field> | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 public_key | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | The public key you get when you create an account on L<http://recaptcha.net/> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 private_key | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | The private key you get when you create an account on L<http://recaptcha.net/> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 use_ssl | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | control the 'use_ssl' option in L<Captcha::reCAPTCHA> when calling 'get_html'. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 recaptcha_options | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | control the 'options' option in L<Captcha::reCAPTCHA> when calling 'get_html'. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 recaptcha_message | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | What to show if the recaptcha fails. Defaults to 'Error validating reCAPTCHA'. | ||||||||||||||||||||||||||||
| 167 | This error message is in addition to any other constraints you add, such as | ||||||||||||||||||||||||||||
| 168 | 'required'. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Please note that the recaptcha control also displays an error message internal | ||||||||||||||||||||||||||||
| 171 | to itself. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 FORM METHODS | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | The following methods or attributes can be set in the form which contains the | ||||||||||||||||||||||||||||
| 176 | recapcha field. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 $name_public_key or $name_private_key | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | "$name" is the name you gave to the reCAPTCHA field (the word directy after the | ||||||||||||||||||||||||||||
| 181 | "has_field" command. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | You may wish to set your public key from a method or attribute contained from | ||||||||||||||||||||||||||||
| 184 | within the form. This would make it easier to have one form class and use | ||||||||||||||||||||||||||||
| 185 | configuration tools, such as what L<Catalyst> offers, to set the pubic key. | ||||||||||||||||||||||||||||
| 186 | For example: | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | ## In my form "MyApp::Form::MyForm | ||||||||||||||||||||||||||||
| 189 | has ['MY_recaptcha_public_key', 'MY_recapcha_private_key'] => ( | ||||||||||||||||||||||||||||
| 190 | is=>'ro', isa=>'Str', required=>1, | ||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||
| 192 | has_field 'MY_recaptcha' => ( | ||||||||||||||||||||||||||||
| 193 | type=>'reCAPTCHA', | ||||||||||||||||||||||||||||
| 194 | recaptcha_message => "You're failed to prove your Humanity!", | ||||||||||||||||||||||||||||
| 195 | required=>1, | ||||||||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Then you might construct this in a L<Catalyst::Controller>: | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | my $form = MyApp::Form::MyForm->new( | ||||||||||||||||||||||||||||
| 201 | MY_recaptcha_public_key => $self->controller_public_key, | ||||||||||||||||||||||||||||
| 202 | MY_recaptcha_private_key => $self->controller_private_key, | ||||||||||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | ## 'process', etc. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Then your controller could populate the attributes 'controller_public_key' and | ||||||||||||||||||||||||||||
| 208 | 'controller_private_key' from your global L<Catalyst> configuration, allowing | ||||||||||||||||||||||||||||
| 209 | you to use one set of keys in development and another for production, or even | ||||||||||||||||||||||||||||
| 210 | use different keys for different forms if you wish. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | The following modules or resources may be of interest. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | L<HTML::FormHandler>, L<Captch::reCAPTCHA> | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | John Napiorkowski C<< <jjnapiork@cpan.org> >> | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Ferruccio Zamuner (ferz) | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Copyright 2013, John Napiorkowski C<< <jjnapiork@cpan.org> >> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Original work sponsered by Shutterstock, LLC. | ||||||||||||||||||||||||||||
| 231 | L<http://shutterstock.com> | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 234 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 239 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 3 | 1; | |||||||||||||||||||||||||||