blib/lib/Google/reCAPTCHA.pm

filter: Statementstmt 51.1% 23 / 45 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 58.3% 7 / 12 Podpod 100% 2 / 2 totaltotal 47.7% 32 / 67 CC 5 SCAR 23.3
CC
5
Cov
43%
siteverify
30.0
new
2.0
__ANON__
2.0
CRAP 10.3
↑ Summary
1 package Google::reCAPTCHA;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Params::Validate qw( validate SCALAR );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.04';
12  
13 1 ▶use constant URL => 'https://www.google.com/recaptcha/api/siteverify';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
17 my $self = validate( @_, {
18 secret => {
19 type => SCALAR,
20 callbacks => {
21 'is a secret key' =>
22 0 ▶ sub { $_[0] ne '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 }
24 }
25 0 } );
26
27 0 bless $self, $class;
28
29 0 return $self;
30 }
31  
32 sub siteverify {
33 0 ▶ my $self = shift;
Subroutine siteverify: not called CC=5 SCAR=34.0
Pod documented
34 my $pd = validate( @_, {
35 response => {
36 type => SCALAR,
37 callbacks => {
38 'is a response code' =>
39 0 ▶ sub { $_[0] ne '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 }
41 },
42 remoteip => {
43 type => SCALAR,
44 optional => 1,
45 callbacks => {
46 'is a remote ipv4 address' =>
47 0 ▶ sub { $_[0] =~ /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 },
49 },
50 0 } );
51
52 0 $pd->{secret} = $self->{secret};
53
54 0 my $ua = LWP::UserAgent->new;
55 0 $ua->ssl_opts( verify_hostname => 0 );
56  
57 0 my $response = $ua->post( URL , $pd );
58
59 0 ▶ if ( $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
60 0 my $data = decode_json( $response->decoded_content );
61
62 0 ▶ if ( exists ( $data->{'error-codes'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"error-codes"}) 0 0 0
63 0 croak( 'API Error: ' . join( ', ', @{ $data->{'error-codes'} } ) );
64 }
65
66 0 ▶ return $data->{success} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$data->{'success'} ? : 0 0 0
67 }
68 else {
69 0 ▶ my $content = $response->decoded_content ? $response->decoded_content : '';
Branch: 1 branch
BranchTrueFalseTotal
$response->decoded_content ? : 0 0 0
70 0 my $message = 'HTTP Request failed with status ' . $response->code . ' : ' . $content;
71  
72 0 croak( $message );
73 }
74 }
75
76 1 1;
77 __END__
78  
79 =head1 NAME
80  
81 Google::reCAPTCHA - A simple lightweight implementation of Google's reCAPTCHA for perl
82  
83 =head1 SYNOPSIS
84  
85 use Google::reCAPTCHA;
86  
87 my $c = Google::reCAPTCHA->new( secret => 'secret_key' );
88
89 # Verifying the user's response
90 my $success = $c->siteverify( response => 'response_key', remoteip => '192.168.0.1' );
91
92 if( $success ) {
93 # CAPTCHA was valid
94 }
95  
96 =head1 GETTING STARTED
97  
98 To use reCAPTCHA you need to register your site here:
99  
100 https://www.google.com/recaptcha/admin/create
101  
102 =head1 PUBLIC INTERFACE
103  
104 =head2 Google::reCAPTCHA->new( secret => 'secret_key' )
105  
106 Our constructor, will croak when invalid parameters are given.
107  
108  
109 B<secret>
110  
111 Required. The shared key between your site and ReCAPTCHA.
112  
113 =head2 siteverify( response => 'response_key', remoteip => '192.168.0.1' )
114  
115 Request siteverify from Google reCAPTCHA API
116  
117  
118 B<response>
119  
120 Required. Form data containing g_captcha_response.
121  
122 B<remoteip>
123  
124 Optional. User's ip address.
125  
126 =head2 Errors
127  
128 B<Error code reference:>
129  
130 missing-input-secret I<The secret parameter is missing.>
131  
132 invalid-input-secret I<The secret parameter is invalid or malformed.>
133  
134 missing-input-response I<The response parameter is missing.>
135  
136 invalid-input-response I<The response parameter is invalid or malformed.>
137  
138 =head1 Git repo
139  
140 L<https://bitbucket.org/tcorkran/google-recaptcha>
141  
142 =head1 VERSION
143  
144 0.04
145  
146 =head1 AUTHOR
147  
148 Thomas Corkran C<< <thomascorkran@gmail.com> >>
149  
150 =head1 CONTRIBUTORS
151  
152 Christopher Mevissen C<< <mev412@gmail.com> >>
153  
154 =head1 ACKNOWLEDGEMENTS
155  
156 Matthew Green C<< <green.matt.na@gmail.com> >>
157  
158 ---
159  
160 This projects work was sponsored by Hostgator.com.
161  
162 L<http://www.hostgator.com>
163  
164 L<http://www.endurance.com>
165  
166 =head1 COPYRIGHT & LICENSE
167  
168 Copyright 2015, Thomas Corkran C<< <thomascorkran@gmail.com> >>
169  
170 This program is free software; you can redistribute it and/or modify
171 it under the same terms as Perl itself.
172  
173 =cut
↑ Summary