blib/lib/Web/VWF/Display/captcha.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 72 CC 5 SCAR 28.7
CC
5
Cov
0%
html
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 17.6
1 package Web::VWF::Display::captcha;
2  
3 use strict;
4 use warnings;
5  
6 use version; our $VERSION = version->declare('0.004.3');
7 use parent 'Web::VWF::Display';
8  
9 sub html {
10 my $self = shift;
11 my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
12  
13 my $config = $self->{'config'} or die "Missing 'config' handle";
14 $self->{'info'} or die "Missing 'info' handle";
15  
16 # Get reCAPTCHA configuration. This must be a hash lookup, not a method
17 # call: Web::VWF::Display->new() flattens the Web::VWF::Config object that
18 # it is given into a plain hash.
19 my $recaptcha_config = $config->{'recaptcha'};
20 unless ($recaptcha_config && $recaptcha_config->{enabled}) {
21 die 'reCAPTCHA is not enabled in configuration';
22 }
23  
24 my $site_key = $recaptcha_config->{site_key};
25 my $script_name = $ENV{'SCRIPT_NAME'} || '/cgi-bin/page.fcgi';
26  
27 # Get rate limit info for display
28 my $soft_limit = $config->{'security'}->{'rate_limiting'}->{'max_requests'} || 100;
29 my $time_window = $config->{'security'}->{'rate_limiting'}->{'time_window'} || '60s';
30 $time_window =~ s/s$//; # Remove 's' suffix
31  
32 # Determine if this is a hard block or soft limit
33 my $is_hard_block = $args{'hard_block'} || 0;
34 my $request_count = $args{'request_count'} || 0;
35  
36 if ($self->{'logger'}) {
37 $self->{'logger'}->debug('Displaying CAPTCHA page for ' . ($is_hard_block ? 'hard block' : 'soft limit'));
38 }
39  
40 return $self->SUPER::html({
41 site_key => $site_key,
42 script_name => $script_name,
43 soft_limit => $soft_limit,
44 time_window => $time_window,
45 is_hard_block => $is_hard_block,
46 request_count => $request_count,
47 });
48 }
49  
50 1;
51  
52 =encoding utf8
53  
54 =head1 NAME
55  
56 Web::VWF::Display::captcha - The page that asks a client to prove it is a person
57  
58 =head1 VERSION
59  
60 Version 0.004.3
61  
62 =head1 DESCRIPTION
63  
64 Shown by L<Web::VWF> and not by C<?page=>, when a client goes over the soft rate
65 limit (see C<security.rate_limiting> in L<Web::VWF/CONFIGURATION>), and again,
66 with C<hard_block> set, with the hard limit's warning when reCAPTCHA is enabled.
67 The answer comes back as C<g-recaptcha-response>, which L<Web::VWF::CAPTCHA>
68 checks.
69  
70 =head1 METHODS
71  
72 =head2 html
73  
74 Takes C<hard_block> and C<request_count>, both optional, which are only shown to
75 the client. Dies when reCAPTCHA is not enabled or the object has no C<config> or
76 C<info>. The template is F<captcha.tmpl>; it is given C<site_key>,
77 C<script_name> and the limits.
78  
79 =head3 API SPECIFICATION
80  
81 =head4 INPUT
82  
83 { hard_block => { type => 'boolean', optional => 1 }, request_count => { type => 'integer', optional => 1 } }
84  
85 =head4 OUTPUT
86  
87 { type => 'string' } # the HTML
88  
89 =head1 AUTHOR
90  
91 Nigel Horne, C<< <njh at nigelhorne.com> >>
92  
93 =head1 LICENCE AND COPYRIGHT
94  
95 Copyright 2010-2026 Nigel Horne.
96  
97 Usage is subject to the GPL2 licence terms.
98 If you use it,
99 please let me know.
100  
101 =cut