File Coverage

blib/lib/Plack/Middleware/Antibot/TextCaptcha.pm
Criterion Covered Total %
statement 38 38 100.0
branch 4 6 66.6
condition 8 14 57.1
subroutine 7 7 100.0
pod 2 2 100.0
total 59 67 88.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::Antibot::TextCaptcha;
2              
3 1     1   52918 use strict;
  1         2  
  1         30  
4 1     1   4 use warnings;
  1         1  
  1         21  
5              
6 1     1   3 use parent 'Plack::Middleware::Antibot::FilterBase';
  1         1  
  1         4  
7              
8 1     1   426 use Plack::Session;
  1         1035  
  1         23  
9 1     1   464 use Plack::Request;
  1         30583  
  1         236  
10              
11             sub new {
12 7     7 1 8413 my $self = shift->SUPER::new(@_);
13 7         9 my (%params) = @_;
14              
15 7   50     55 $self->{session_name} = $params{session_name} || 'antibot_textcaptcha';
16 7   50     40 $self->{field_name} = $params{field_name} || 'antibot_textcaptcha';
17 7   50     48 $self->{variants} = $params{variants} || [{text => '2 + 2', answer => 4}];
18 7   50     30 $self->{score} = $params{score} || 0.9;
19              
20 7         18 return $self;
21             }
22              
23             sub execute {
24 7     7 1 12125 my $self = shift;
25 7         37 my ($env) = @_;
26              
27 7         10 my $variants = $self->{variants};
28              
29 7         15 my $captcha = $variants->[int(rand(@$variants))];
30 7         13 $env->{'plack.antibot.textcaptcha.text'} = $captcha->{text};
31 7         10 $env->{'plack.antibot.textcaptcha.field_name'} = $self->{field_name};
32              
33 7 100       26 if ($env->{REQUEST_METHOD} eq 'GET') {
    50          
34 3         17 my $session = Plack::Session->new($env);
35 3         25 $session->set($self->{session_name}, $captcha->{answer});
36             }
37             elsif ($env->{REQUEST_METHOD} eq 'POST') {
38 4         18 my $session = Plack::Session->new($env);
39              
40 4         31 my $expected = $session->get($self->{session_name});
41 4         41 my $got = Plack::Request->new($env)->param($self->{field_name});
42              
43 4 50 66     1543 unless ($expected && $got && $got eq $expected) {
      66        
44 3         6 $env->{'plack.antibot.textcaptcha.detected'}++;
45             }
46              
47 4         14 $session->set($self->{session_name}, $captcha->{answer});
48             }
49              
50 7         86 return;
51             }
52              
53             1;
54             __END__