line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::TraitFor::Controller::reCAPTCHA; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Catalyst::TraitFor::Controller::reCAPTCHA::VERSION = '1.122510'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: authenticate people and read books! |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
21252
|
use Moose::Role; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use MooseX::MethodAttributes::Role; |
9
|
|
|
|
|
|
|
use namespace::autoclean; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Captcha::reCAPTCHA; |
12
|
|
|
|
|
|
|
use Carp 'croak'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has recaptcha => ( is => 'ro', default => sub { Captcha::reCAPTCHA->new } ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub captcha_get :Private { |
17
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $recaptcha = $self->recaptcha->get_html( |
20
|
|
|
|
|
|
|
$c->config->{recaptcha}{pub_key}, |
21
|
|
|
|
|
|
|
$c->stash->{recaptcha_error}, |
22
|
|
|
|
|
|
|
$c->req->secure, |
23
|
|
|
|
|
|
|
$c->config->{recaptcha}{options} |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$c->stash( recaptcha => $recaptcha ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub captcha_check :Private { |
30
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $challenge = $c->req->param('recaptcha_challenge_field'); |
33
|
|
|
|
|
|
|
my $response = $c->req->param('recaptcha_response_field'); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
unless ( $response && $challenge ) { |
36
|
|
|
|
|
|
|
$c->stash->{recaptcha_error} = 'User appears not to have submitted a recaptcha'; |
37
|
|
|
|
|
|
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $res = $self->recaptcha->check_answer( |
41
|
|
|
|
|
|
|
$c->config->{recaptcha}{priv_key}, |
42
|
|
|
|
|
|
|
$c->req->address, |
43
|
|
|
|
|
|
|
$challenge, |
44
|
|
|
|
|
|
|
$response |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
croak 'Failed to get valid result from reCaptcha' |
48
|
|
|
|
|
|
|
unless ref $res eq 'HASH'; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
unless ( $res->{is_valid} ) { |
51
|
|
|
|
|
|
|
$c->stash( recaptcha_error => $res->{error} || 'Invalid recaptcha' ); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$c->stash( recaptcha_ok => $res->{is_valid} ); |
55
|
|
|
|
|
|
|
return $res->{is_valid}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__END__ |
62
|
|
|
|
|
|
|
=pod |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Catalyst::TraitFor::Controller::reCAPTCHA - authenticate people and read books! |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 VERSION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
version 1.122510 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
In your controller |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
package MyApp::Controller::Comment; |
77
|
|
|
|
|
|
|
use Moose; |
78
|
|
|
|
|
|
|
use namespace::autoclean; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
BEGIN { extends 'Catalyst::Controller' } |
81
|
|
|
|
|
|
|
with 'Catalyst::TraitFor::Controller::reCAPTCHA'; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub example : Local { |
84
|
|
|
|
|
|
|
my ( $self, $c ) = @; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# validate received form |
87
|
|
|
|
|
|
|
if ( $c->forward('captcha_check') ) { |
88
|
|
|
|
|
|
|
$c->detach('my_form_is_ok'); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Set reCAPTCHA html code |
92
|
|
|
|
|
|
|
$c->forward('captcha_get'); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 SUMMARY |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Catalyst::Controller role around L<Captcha::reCAPTCHA>. Provides |
100
|
|
|
|
|
|
|
a number of C<Private> methods that deal with the recaptcha. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This module is based/copied from L<Catalyst::Controller::reCAPTCHA>, |
103
|
|
|
|
|
|
|
it just adds support for option passing and automatically sets ssl |
104
|
|
|
|
|
|
|
when used on a secure request. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
If you are using L<Catalyst::Controller::reCAPTCHA> and want to move |
107
|
|
|
|
|
|
|
to this role, you only need to stop extending L<Catalyst::Controller> |
108
|
|
|
|
|
|
|
and apply this role as shown in the SYNOPSIS. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 CONFIGURATION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
In MyApp.pm (or equivalent in config file): |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__PACKAGE__->config->{recaptcha} = { |
115
|
|
|
|
|
|
|
pub_key => '6LcsbAAAAAAAAPDSlBaVGXjMo1kJHwUiHzO2TDze', |
116
|
|
|
|
|
|
|
priv_key => '6LcsbAAAAAAAANQQGqwsnkrTd7QTGRBKQQZwBH-L', |
117
|
|
|
|
|
|
|
options => { theme => 'white' } |
118
|
|
|
|
|
|
|
}; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
(the two keys above work for http://localhost unless someone hammers the |
121
|
|
|
|
|
|
|
reCAPTCHA server with failures, in which case the API keys get a temporary |
122
|
|
|
|
|
|
|
ban). |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 METHODS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head3 captcha_get : Private |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Sets $c->stash->{recaptcha} to be the html form for the L<http://recaptcha.net/> reCAPTCHA service which can be included in your HTML form. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head3 captcha_check : Private |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Validates the reCaptcha using L<Captcha::reCAPTCHA>. sets |
133
|
|
|
|
|
|
|
$c->stash->{recaptcha_ok} which will be 1 on success. The action also returns |
134
|
|
|
|
|
|
|
true if there is success. This means you can do: |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
if ( $c->forward(captcha_check) ) { |
137
|
|
|
|
|
|
|
# do something based on the reCAPTCHA passing |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
or alternatively: |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$c->forward(captcha_check); |
143
|
|
|
|
|
|
|
if ( $c->stash->{recaptcha_ok} ) { |
144
|
|
|
|
|
|
|
# do something based on the reCAPTCHA passing |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
If there's an error, $c->stash->{recaptcha_error} is |
148
|
|
|
|
|
|
|
set with the error string provided by L<Captcha::reCAPTCHA>. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SEE ALSO |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over 4 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L<Captcha::reCAPTCHA> |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L<Catalyst::Controller> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
L<Catalyst> |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This module is almost copied from Kieren Diment L<Catalyst::Controller::reCAPTCHA>. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Diego Kuperman <diego@freekeylabs.com> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Diego Kuperman. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
181
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|