line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::ReCAPTCHAv2; |
2
|
|
|
|
|
|
|
$Mojolicious::Plugin::ReCAPTCHAv2::VERSION = '1.04'; |
3
|
|
|
|
|
|
|
# vim:syntax=perl:tabstop=4:number:noexpandtab: |
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
71423
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
6
|
|
|
|
|
189688
|
|
|
6
|
|
|
|
|
43
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: use Googles "No CAPTCHA reCAPCTHA" (reCAPTCHA v2) service in Mojolicious apps |
8
|
|
|
|
|
|
|
|
9
|
6
|
|
|
6
|
|
2375
|
use Mojo::JSON qw(); |
|
6
|
|
|
|
|
21308
|
|
|
6
|
|
|
|
|
105
|
|
10
|
6
|
|
|
6
|
|
581
|
use Mojo::UserAgent qw(); |
|
6
|
|
|
|
|
235992
|
|
|
6
|
|
|
|
|
7931
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has conf => sub { +{} }; |
13
|
|
|
|
|
|
|
has ua => sub { Mojo::UserAgent->new()->max_redirects( 0 ) }; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
5
|
|
|
5
|
1
|
186
|
my $plugin = shift; |
18
|
5
|
|
|
|
|
8
|
my $app = shift; |
19
|
5
|
|
50
|
|
|
19
|
my $conf = shift || {}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
die ref( $plugin ), ": need sitekey and secret!\n" |
22
|
5
|
50
|
33
|
|
|
37
|
unless $conf->{'sitekey'} and $conf->{'secret'}; |
23
|
|
|
|
|
|
|
|
24
|
5
|
|
50
|
|
|
30
|
$conf->{'api_url'} //= 'https://www.google.com/recaptcha/api/siteverify'; |
25
|
5
|
|
50
|
|
|
27
|
$conf->{'api_timeout'} //= 10; |
26
|
|
|
|
|
|
|
|
27
|
5
|
|
|
|
|
16
|
$plugin->conf( $conf ); |
28
|
5
|
|
|
|
|
68
|
$plugin->ua->request_timeout( $conf->{'api_timeout'} ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$app->helper( |
31
|
|
|
|
|
|
|
recaptcha_get_html => sub { |
32
|
3
|
|
|
3
|
|
43923
|
my $c = shift; |
33
|
3
|
100
|
|
|
|
12
|
my $language = $_[0] ? shift : undef; |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
|
|
6
|
my %data_attr = map { $_ => $plugin->conf->{$_} } grep { index( $_, 'api_' ) != 0 } keys %{ $plugin->conf }; |
|
12
|
|
|
|
|
54
|
|
|
18
|
|
|
|
|
64
|
|
|
3
|
|
|
|
|
12
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Never expose this! |
38
|
3
|
|
|
|
|
22
|
delete $data_attr{'secret'}; |
39
|
|
|
|
|
|
|
|
40
|
3
|
|
|
|
|
7
|
my $hl = ''; |
41
|
3
|
100
|
66
|
|
|
28
|
if ( defined $language and $language ) { |
|
|
50
|
|
|
|
|
|
42
|
1
|
|
|
|
|
2
|
$hl = $language; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif ( exists $data_attr{'language'} ) { |
45
|
0
|
|
|
|
|
0
|
$hl = delete $data_attr{'language'}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
3
|
|
|
|
|
14
|
my $output = ''; |
49
|
3
|
|
|
|
|
22
|
my $template = q| |
50
|
|
|
|
|
|
|
data-<%= $k %>="<%= $attr->{$k} %>"<% } %>> |; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
31
|
return $c->render_to_string( |
53
|
|
|
|
|
|
|
handler => 'ep', |
54
|
|
|
|
|
|
|
inline => $template, |
55
|
|
|
|
|
|
|
hl => $hl, |
56
|
|
|
|
|
|
|
attr => \%data_attr, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
} |
59
|
5
|
|
|
|
|
147
|
); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$app->helper( |
62
|
|
|
|
|
|
|
recaptcha_verify => sub { |
63
|
3
|
|
|
3
|
|
42994
|
my $c = shift; |
64
|
|
|
|
|
|
|
|
65
|
3
|
|
|
|
|
6
|
my $cb = shift; |
66
|
3
|
100
|
66
|
|
|
20
|
unless ( defined( $cb ) and ref( $cb ) eq 'CODE' ) { |
67
|
2
|
|
|
|
|
5
|
$cb = ''; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my %verify_params = ( |
71
|
|
|
|
|
|
|
remoteip => $c->tx->remote_address, |
72
|
|
|
|
|
|
|
response => ( $c->req->param( 'g-recaptcha-response' ) || '' ), |
73
|
3
|
|
50
|
|
|
11
|
secret => $plugin->conf->{'secret'}, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
3
|
|
|
|
|
846
|
my $url = $plugin->conf->{'api_url'}; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $response_handler = sub { |
79
|
3
|
|
|
|
|
65736
|
my ( $ua, $tx ) = @_; |
80
|
|
|
|
|
|
|
|
81
|
3
|
|
|
|
|
11
|
my ( $verified, $err ) = ( 0, [] ); |
82
|
3
|
50
|
|
|
|
11
|
if ( my $txerr = $tx->error ) { |
83
|
0
|
|
|
|
|
0
|
my $txt = 'Retrieving captcha verifcation failed'; |
84
|
0
|
0
|
|
|
|
0
|
$txt .= ' (HTTP ' . $txerr->{'code'} . ')' if $txerr->{'code'}; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
$c->app->log->error( $txt . ': ' . $txerr->{'message'} ); |
87
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Request was: ' . $tx->req->to_string ); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
( $verified, $err ) = ( 0, ['x-http-communication-failed'] ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
else { |
92
|
3
|
|
|
|
|
69
|
my $json = ''; |
93
|
3
|
|
|
|
|
7
|
eval { $json = Mojo::JSON::decode_json( $tx->res->body ); }; |
|
3
|
|
|
|
|
20
|
|
94
|
|
|
|
|
|
|
|
95
|
3
|
50
|
|
|
|
799
|
if ( $@ ) { |
96
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Decoding JSON response failed: ' . $@ ); |
97
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Request was: ' . $tx->req->to_string ); |
98
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Response was: ' . $tx->res->to_string ); |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
( $verified, $err ) = ( 0, ['x-unparseable-data-received'] ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
else { |
103
|
3
|
50
|
|
|
|
33
|
unless ( $json->{'success'} == Mojo::JSON->true ) { |
104
|
3
|
|
50
|
|
|
144
|
@{$err} = @{ $json->{'error-codes'} // [] }; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
18
|
|
105
|
|
|
|
|
|
|
} |
106
|
3
|
|
|
|
|
14
|
$verified = $json->{'success'}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
3
|
100
|
|
|
|
20
|
return $cb->( $verified, $err ) if $cb; |
111
|
2
|
|
|
|
|
43
|
return ( $verified, $err ); |
112
|
3
|
|
|
|
|
50
|
}; |
113
|
|
|
|
|
|
|
|
114
|
3
|
100
|
|
|
|
21
|
if ( $cb ) { |
115
|
1
|
|
|
|
|
6
|
return $plugin->ua->post( $url, form => \%verify_params, $response_handler ); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
else { |
118
|
2
|
|
|
|
|
9
|
my $tx = $plugin->ua->post( $url, form => \%verify_params ); |
119
|
2
|
|
|
|
|
157219
|
return $response_handler->( $plugin->ua, $tx ); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
5
|
|
|
|
|
716
|
); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
$app->helper( |
125
|
|
|
|
|
|
|
recaptcha_verify_p => sub { |
126
|
1
|
|
|
1
|
|
17468
|
my $c = shift; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my %verify_params = ( |
129
|
|
|
|
|
|
|
remoteip => $c->tx->remote_address, |
130
|
|
|
|
|
|
|
response => ( $c->req->param( 'g-recaptcha-response' ) || '' ), |
131
|
1
|
|
50
|
|
|
4
|
secret => $plugin->conf->{'secret'}, |
132
|
|
|
|
|
|
|
); |
133
|
|
|
|
|
|
|
|
134
|
1
|
|
|
|
|
315
|
my $url = $plugin->conf->{'api_url'}; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# Async request using promises |
137
|
1
|
|
|
|
|
10
|
require Mojo::Promise; |
138
|
1
|
|
|
|
|
8
|
my $p = Mojo::Promise->new(); |
139
|
|
|
|
|
|
|
$plugin->ua->post( |
140
|
|
|
|
|
|
|
$url => form => \%verify_params, |
141
|
|
|
|
|
|
|
sub { |
142
|
1
|
|
|
|
|
89134
|
my ( $ua, $tx ) = @_; |
143
|
|
|
|
|
|
|
|
144
|
1
|
50
|
|
|
|
6
|
if ( my $err = $tx->error ) { |
145
|
0
|
|
|
|
|
0
|
my $txt = 'Retrieving captcha verification failed'; |
146
|
0
|
0
|
|
|
|
0
|
$txt .= ' (HTTP ' . $err->{'code'} . ')' if $err->{'code'}; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
0
|
$c->app->log->error( $txt . ': ' . $err->{'message'} ); |
149
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Request was: ' . $tx->req->to_string ); |
150
|
0
|
|
|
|
|
0
|
return $p->reject( ['x-http-communication-failed'] ); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
1
|
|
|
|
|
34
|
my $res = $tx->res; |
154
|
1
|
|
|
|
|
11
|
my $json = eval { $res->json }; |
|
1
|
|
|
|
|
21
|
|
155
|
|
|
|
|
|
|
|
156
|
1
|
50
|
|
|
|
350
|
if ( not defined $json ) { |
157
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Decoding JSON response failed: ' . $@ ); |
158
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Request was: ' . $tx->req->to_string ); |
159
|
0
|
|
|
|
|
0
|
$c->app->log->error( 'Response was: ' . $tx->res->to_string ); |
160
|
0
|
|
|
|
|
0
|
return $p->reject( ['x-unparseable-data-received'] ); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
1
|
50
|
|
|
|
39
|
unless ( $json->{'success'} ) { |
164
|
1
|
|
50
|
|
|
22
|
return $p->reject( $json->{'error-codes'} // [] ); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
0
|
return $p->resolve( $json->{'success'} ); |
168
|
|
|
|
|
|
|
} |
169
|
1
|
|
|
|
|
63
|
); |
170
|
|
|
|
|
|
|
|
171
|
1
|
|
|
|
|
1277
|
return $p; |
172
|
|
|
|
|
|
|
} |
173
|
5
|
|
|
|
|
429
|
); |
174
|
|
|
|
|
|
|
|
175
|
5
|
|
|
|
|
374
|
return; |
176
|
|
|
|
|
|
|
} ## end sub register |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__END__ |