File Coverage

blib/lib/Catalyst/Controller/reCAPTCHA.pm
Criterion Covered Total %
statement 18 22 81.8
branch n/a
condition n/a
subroutine 6 7 85.7
pod n/a
total 24 29 82.7


line stmt bran cond sub pod time code
1             package Catalyst::Controller::reCAPTCHA;
2 1     1   41466 use strict;
  1         3  
  1         35  
3 1     1   6 use warnings;
  1         2  
  1         31  
4 1     1   6 use base 'Catalyst::Controller';
  1         6  
  1         1130  
5 1     1   2482 use Captcha::reCAPTCHA;
  1         106767  
  1         43  
6 1     1   14 use Carp 'croak';
  1         2  
  1         154  
7             our $VERSION = '0.8';
8              
9              
10             sub captcha_get : Private {
11 0     0     my ($self, $c) = @_;
12 0           my $cap = Captcha::reCAPTCHA->new;
13 0           $c->stash->{recaptcha} =
14             $cap->get_html($c->config->{recaptcha}->{pub_key});
15 0           return;
16 1     1   1271 }
  1         1411  
  1         5  
17              
18             sub captcha_check : Private {
19             my ($self, $c) = @_;
20             my $cap = Captcha::reCAPTCHA->new;
21             my $challenge = $c->req->param('recaptcha_challenge_field');
22             my $response = $c->req->param('recaptcha_response_field');
23              
24             unless ( $response && $challenge ) {
25             $c->stash->{recaptcha_error} =
26             'User appears not to have submitted a recaptcha';
27             return;
28             }
29              
30             my $key = $c->config->{recaptcha}->{priv_key} ||
31             croak 'must set recaptcha priv_key in config';
32              
33             my $result = $cap->check_answer(
34             $key,
35             $c->req->address,
36             $challenge,
37             $response,
38             );
39              
40             croak 'Failed to get valid result from reCaptcha'
41             unless ref $result eq 'HASH' && exists $result->{is_valid};
42              
43              
44             $c->stash->{recaptcha_error} = $result->{error} ||
45             'Unknown error'
46             unless $result->{is_valid};
47              
48             $c->stash->{recaptcha_ok} = 1 if $result->{is_valid};
49             return 1;
50             }
51              
52              
53              
54              
55             =head1 NAME
56              
57             Catalyst::Controller::reCAPTCHA - authenticate people and read books!
58              
59             WARNING: Deprecated. Please use L<Catalyst::TraitFor::Controller::reCAPTCHA> instead.
60              
61             =head1 SUMMARY
62              
63             This module has been deprecated and has been replaced with
64             L<Catalyst::TraitFor::Controller::reCAPTCHA>. Please do not use this for new
65             projects. Version 0.8 is very likely the last to be released for this module.
66              
67             Catalyst::Controller wrapper around L<Capatcha::reCAPTCHA>. Provides
68             a number of C<Private> methods that deal with the recaptcha.
69              
70             =head2 CONFIGURATION
71              
72             In MyApp.pm (or equivalent in config file):
73              
74             __PACKAGE__->config->{recaptcha}->{pub_key} =
75             '6LcsbAAAAAAAAPDSlBaVGXjMo1kJHwUiHzO2TDze';
76             __PACKAGE__->config->{recaptcha}->{priv_key} =
77             '6LcsbAAAAAAAANQQGqwsnkrTd7QTGRBKQQZwBH-L';
78              
79             (the two keys above work for http://localhost unless someone hammers the
80             reCAPTCHA server with failures, in which case the API keys get a temporary
81             ban).
82              
83             =head2 METHOD
84              
85             captcha_get : Private
86              
87             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.
88              
89             =head2 METHOD
90              
91             captcha_check : Private
92              
93             Validates the reCaptcha using L<Captcha::reCAPTCHA>. sets
94             $c->stash->{recaptcha_ok} which will be 1 on success. The action also returns
95             true if there is success. This means you can do:
96              
97             if ( $c->forward(captcha_check) ) {
98             # do something based on the reCAPTCHA passing
99             }
100              
101             or alternatively:
102              
103             if ( $c->stash->{recaptcha_ok} ) {
104             # do something based on the reCAPTCHA passing
105             }
106              
107              
108             If there's an error, $c->stash->{recaptcha_error} is
109             set with the error string provided by L<Captcha::reCAPTCHA>.
110              
111             =head2 EXAMPLES
112              
113             See the t/lib/TestApp example in the
114             L<Catalyst::Controller::reCAPTCHA> distribution.
115              
116             =head2 BUGS
117              
118             This module is deprecated. Please report any bugs you find against
119             L<Catalyst::TraitFor::Controller::reCAPTCHA>.
120              
121             =head1 SEE ALSO
122              
123             L<Captcha::reCAPTCHA>, L<Catalyst::Controller>, L<Catalyst>.
124              
125             =head1 AUTHOR and Copyright
126              
127             Kieren Diment L<zarquon@cpan.org>.
128              
129             =head1 LICENCE
130              
131             This library is free software, you can redistribute it and/or modify it under
132             the same terms as Perl itself.
133              
134             =cut
135              
136             1;