blib/lib/POE/Component/Captcha/reCAPTCHA.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package POE::Component::Captcha::reCAPTCHA;
2 $POE::Component::Captcha::reCAPTCHA::VERSION = '0.04';
3 #ABSTRACT: A POE implementation of the reCAPTCHA API
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POE qw(Component::Client::HTTP);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use HTTP::Request::Common;
10 use Captcha::reCAPTCHA;
11  
12 use constant API_VERIFY_SERVER => 'http://api-verify.recaptcha.net';
13 use constant SERVER_ERROR => 'recaptcha-not-reachable';
14  
15 # Stolen from POE::Wheel. This is static data, shared by all
16 my $current_id = 0;
17 my %active_identifiers;
18  
19 sub _allocate_identifier {
20 while (1) {
21 last unless exists $active_identifiers{ ++$current_id };
22 }
23 return $active_identifiers{$current_id} = $current_id;
24 }
25  
26 sub _free_identifier {
27 my $id = shift;
28 delete $active_identifiers{$id};
29 }
30  
31  
32 sub spawn {
33 my $package = shift;
34 my %opts = @_;
35 $opts{lc $_} = delete $opts{$_} for keys %opts;
36 my $options = delete $opts{options};
37 my $self = bless \%opts, $package;
38 $self->{session_id} = POE::Session->create(
39 object_states => [
40 $self => { shutdown => '_shutdown',
41 check_answer => '_check_answer',
42 },
43 $self => [ qw(_start _check_answer _dispatch _http_request _http_response) ],
44 ],
45 heap => $self,
46 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
47 )->ID();
48 my $captcha = Captcha::reCAPTCHA->new();
49 $self->{_captcha} = $captcha;
50 return $self;
51 }
52  
53 sub session_id {
54 return $_[0]->{session_id};
55 }
56  
57 sub shutdown {
58 my $self = shift;
59 $poe_kernel->post( $self->{session_id}, 'shutdown' );
60 }
61  
62 sub _start {
63 my ($kernel,$self) = @_[KERNEL,OBJECT];
64 $self->{session_id} = $_[SESSION]->ID();
65 if ( $self->{alias} ) {
66 $kernel->alias_set( $self->{alias} );
67 }
68 else {
69 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
70 }
71 $self->{_httpc} = 'httpc-' . $self->{session_id};
72 POE::Component::Client::HTTP->spawn(
73 Alias => $self->{_httpc},
74 FollowRedirects => 2,
75 );
76 return;
77 }
78  
79 sub _shutdown {
80 my ($kernel,$self) = @_[KERNEL,OBJECT];
81 $kernel->alias_remove( $_ ) for $kernel->alias_list();
82 $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
83 $self->{_shutdown} = 1;
84 $kernel->post( $self->{_httpc}, 'shutdown' );
85 undef;
86 }
87  
88 sub get_html {
89 my $self = shift;
90 $self->{_captcha}->get_html( @_ );
91 }
92  
93 sub get_options_setter {
94 my $self = shift;
95 $self->{_captcha}->get_options_setter( @_ );
96 }
97  
98 sub check_answer {
99 my $self = shift;
100 $poe_kernel->post( $self->{session_id}, 'check_answer', @_ );
101 }
102  
103 sub _check_answer {
104 my ($kernel,$self,$state) = @_[KERNEL,OBJECT,STATE];
105 my $sender = $_[SENDER]->ID();
106 return if $self->{_shutdown};
107 my $args;
108 if ( ref( $_[ARG0] ) eq 'HASH' ) {
109 $args = { %{ $_[ARG0] } };
110 } else {
111 $args = { @_[ARG0..$#_] };
112 }
113  
114 $args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args };
115  
116 unless ( $args->{event} ) {
117 warn "No 'event' specified for $state";
118 return;
119 }
120  
121 croak
122 "To use reCAPTCHA you must get an API key from http://recaptcha.net/api/getkey"
123 unless $args->{privatekey};
124  
125 croak "For security reasons, you must pass the remote ip to reCAPTCHA"
126 unless $args->{remoteip};
127  
128 $args->{sender} = $sender;
129 $kernel->refcount_increment( $sender => __PACKAGE__ );
130 $kernel->yield( '_http_request', $args );
131  
132 return;
133 }
134  
135 sub _http_request {
136 my ($kernel,$self,$req) = @_[KERNEL,OBJECT,ARG0];
137  
138 unless ( $req->{challenge} and $req->{response} ) {
139 $req->{is_valid} = 0;
140 $req->{error} = 'incorrect-captcha-sol';
141 $kernel->yield( '_dispatch', $req );
142 return;
143 }
144  
145 my %postargs = map {
146 ( $_ => $req->{$_} )
147 } qw(privatekey remoteip challenge response);
148  
149 my $id = _allocate_identifier();
150  
151 $kernel->post(
152 $self->{_httpc},
153 'request',
154 '_http_response',
155 POST( API_VERIFY_SERVER . '/verify', \%postargs ),
156 "$id",
157 );
158  
159 $self->{_requests}->{ $id } = $req;
160 return;
161 }
162  
163 sub _http_response {
164 my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1];
165 my $id = $request_packet->[1];
166 my $req = delete $self->{_requests}->{ $id };
167 _free_identifier( $id );
168 my $resp = $response_packet->[0];
169 if ( $resp->is_success ) {
170 my ( $answer, $message ) = split( /\n/, $resp->content, 2 );
171 if ( $answer =~ /true/ ) {
172 $req->{is_valid} = 1;
173 }
174 else {
175 chomp $message;
176 $req->{is_valid} = 0; $req->{error} = $message;
177 }
178 }
179 else {
180 $req->{is_valid} = 0; $req->{error} = SERVER_ERROR;
181 }
182  
183 $kernel->yield( '_dispatch', $req );
184 return;
185 }
186  
187 sub _dispatch {
188 my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
189 my $session = delete $input->{sender};
190 my $event = delete $input->{event};
191 $kernel->post( $session, $event, $input );
192 $kernel->refcount_decrement( $session => __PACKAGE__ );
193 return;
194 }
195  
196 qq[CAPTCH!];
197  
198 __END__
↑ Summary