| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Captcha::reCAPTCHA; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: use Captcha::reCAPTCHA in Mojolicious apps |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
23341
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
36
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
25
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
432
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Mojo::ByteStream; |
|
10
|
|
|
|
|
|
|
use Captcha::reCAPTCHA; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.04; |
|
13
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub register { |
|
16
|
|
|
|
|
|
|
my $self = shift; |
|
17
|
|
|
|
|
|
|
my $app = shift; |
|
18
|
|
|
|
|
|
|
my $conf = shift || {}; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
die ref($self), ": need private and public key\n" |
|
21
|
|
|
|
|
|
|
unless $conf->{private_key} and $conf->{public_key}; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$app->attr( |
|
24
|
|
|
|
|
|
|
'recaptcha_obj' => sub { |
|
25
|
|
|
|
|
|
|
Captcha::reCAPTCHA->new; |
|
26
|
|
|
|
|
|
|
}, |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$app->attr( recaptcha_private_key => sub { $conf->{private_key} } ); |
|
30
|
|
|
|
|
|
|
$app->attr( recaptcha_public_key => sub { $conf->{public_key} } ); |
|
31
|
|
|
|
|
|
|
$app->attr( recaptcha_use_ssl => sub { $conf->{use_ssl} } ); |
|
32
|
|
|
|
|
|
|
$app->attr( recaptcha_options => sub { $conf->{options} } ); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$app->helper( recaptcha => sub { return shift->app->recaptcha_obj } ); |
|
35
|
|
|
|
|
|
|
$app->helper( |
|
36
|
|
|
|
|
|
|
use_recaptcha => sub { |
|
37
|
|
|
|
|
|
|
my $self = shift; |
|
38
|
|
|
|
|
|
|
$self->stash( recaptcha_html => $self->recaptcha_html(@_) ); |
|
39
|
|
|
|
|
|
|
return; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
); |
|
42
|
|
|
|
|
|
|
$app->helper( |
|
43
|
|
|
|
|
|
|
recaptcha_html => sub { |
|
44
|
|
|
|
|
|
|
my ( $self, $err, $use_ssl, $options ) = @_; |
|
45
|
|
|
|
|
|
|
if ( !defined $use_ssl ) { |
|
46
|
|
|
|
|
|
|
if ( defined $self->app->recaptcha_use_ssl ) { |
|
47
|
|
|
|
|
|
|
$use_ssl = $self->app->recaptcha_use_ssl; |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
elsif ( $self->req->url->base->scheme eq 'https' |
|
50
|
|
|
|
|
|
|
or $self->req->headers->header('X-Forwarded-Protocol') eq 'https' ) |
|
51
|
|
|
|
|
|
|
{ |
|
52
|
|
|
|
|
|
|
$use_ssl = 1; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
else { |
|
55
|
|
|
|
|
|
|
$use_ssl = undef; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
if ( !defined $options ) { |
|
59
|
|
|
|
|
|
|
$options = $self->app->recaptcha_options; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
return Mojo::ByteStream->new( |
|
62
|
|
|
|
|
|
|
$self->recaptcha->get_html( $self->app->recaptcha_public_key, $err, $use_ssl, $options ) |
|
63
|
|
|
|
|
|
|
); |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
); |
|
66
|
|
|
|
|
|
|
$app->helper( |
|
67
|
|
|
|
|
|
|
validate_recaptcha => sub { |
|
68
|
|
|
|
|
|
|
my ( $self, $params ) = @_; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $result = $self->recaptcha->check_answer( $self->app->recaptcha_private_key, |
|
71
|
|
|
|
|
|
|
$self->tx->remote_address, |
|
72
|
|
|
|
|
|
|
$params->{recaptcha_challenge_field}, |
|
73
|
|
|
|
|
|
|
$params->{recaptcha_response_field}, |
|
74
|
|
|
|
|
|
|
); |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
if ( !$result->{is_valid} ) { |
|
77
|
|
|
|
|
|
|
$self->stash( recaptcha_error => $result->{error} ); |
|
78
|
|
|
|
|
|
|
return 0; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
return 1; |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
return; |
|
85
|
|
|
|
|
|
|
} ## end sub register |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=pod |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 NAME |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Mojolicious::Plugin::Captcha::reCAPTCHA - use Captcha::reCAPTCHA in Mojolicious apps |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 VERSION |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
version 0.04 |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Provides a L object in your Mojolicious app. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
use Mojolicious::Plugin::Captcha::reCAPTCHA; |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub startup { |
|
108
|
|
|
|
|
|
|
my $self = shift; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$self->plugin('Captcha::reCAPTCHA', { |
|
111
|
|
|
|
|
|
|
private_key => 'the_public_key', |
|
112
|
|
|
|
|
|
|
public_key => 'your_private_key', |
|
113
|
|
|
|
|
|
|
use_ssl => 1, |
|
114
|
|
|
|
|
|
|
options => { theme => 'white' }, |
|
115
|
|
|
|
|
|
|
}); |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
C and C are mandatory, while C and C are optional. |
|
119
|
|
|
|
|
|
|
Unless you have a specific reason to set a certain global value for C you should |
|
120
|
|
|
|
|
|
|
probably just let the plugin decide when to use HTTPS requests. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
In your mojolicious controller you can control everything by yourself by directly |
|
123
|
|
|
|
|
|
|
invoking the C method of the L object: |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$self->stash( |
|
126
|
|
|
|
|
|
|
recaptcha_html => $self->recaptcha->get_html( $public_key [, $error [, $use_ssl [, $options ] ] ] ), |
|
127
|
|
|
|
|
|
|
); |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Following the same pattern you can also directly invoke C: |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $result = $self->recaptcha->check_answer( |
|
132
|
|
|
|
|
|
|
$private_key, |
|
133
|
|
|
|
|
|
|
$ip, |
|
134
|
|
|
|
|
|
|
$value_of_challenge_field, |
|
135
|
|
|
|
|
|
|
$value_of_response_field, |
|
136
|
|
|
|
|
|
|
); |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Or you can use the new helpers. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 NAME |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Mojolicious::Plugin::Captcha::reCAPTCHA - use Captcha::reCAPTCHA in Mojolicious apps |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 VERSION |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
version 0.04 |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 METHODS/HELPERS |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 recaptcha |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
A helper named 'recaptcha' is created that can be used to access the L |
|
153
|
|
|
|
|
|
|
object. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
my $recaptcha_obj = $self->recaptcha; |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 use_recaptcha |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This helper sets the key C in the stash and uses the HTML as the value. |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$self->use_recaptcha; |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
It automatically uses the public key and the other configuration options you passed in |
|
164
|
|
|
|
|
|
|
when registering the plugin. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
You may explicitly pass in values for C, C, and C. If you do, |
|
167
|
|
|
|
|
|
|
these params will take precedence over the configuration values. |
|
168
|
|
|
|
|
|
|
Pass C for positional params you either don't want to set or where you don't want |
|
169
|
|
|
|
|
|
|
to override the config values: |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
$self->use_recaptcha( undef, undef, { theme => 'red' } ); |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Unless explicitly passed in or set in the configuration, the correct value for C |
|
174
|
|
|
|
|
|
|
is automatically determined based on the current request (by looking at |
|
175
|
|
|
|
|
|
|
C<$self-\>req-\>url-\>base-\>scheme>). |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 recaptcha_html |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This helper works like C but returns the HTML instead of setting a stash |
|
180
|
|
|
|
|
|
|
value. Also accepts the same params as C. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Intended to be used in templates. |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 validate_recaptcha |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Handles the validation of the recaptcha. If an error occurs, the stash variable |
|
187
|
|
|
|
|
|
|
"recaptcha_error" is set. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
$self->validate_recaptcha( $params ); |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
C<$params> is a hashref with parameters of the HTTP request. |
|
192
|
|
|
|
|
|
|
Returns "true" (1) if validation was successful and "false" (0) otherwise. |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 AUTHORS |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=over 4 |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item * |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Renee Baecker |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item * |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Heiko Jansen |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=back |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This software is Copyright (c) 2012 by Hochschulbibliothekszentrum NRW (hbz). |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
This is free software, licensed under: |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 AUTHORS |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=over 4 |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Renee Baecker |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item * |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Heiko Jansen |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=back |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
This software is Copyright (c) 2012 by Hochschulbibliothekszentrum NRW (hbz). |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
This is free software, licensed under: |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
__END__ |