FormHandler/Field/RequestToken.pm

filter: Statementstmt 90.5% 48 / 53 Branchbran 21.4% 3 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 80.7% 67 / 83 CC 8 SCAR 15.6
CC
8
Cov
76%
verify_token
11.1
__ANON__
8.7
validate
2.0
CRAP 4.8
« FormHandler/Render/Util.pm ↑ Summary FormHandler/Widget/Field/Repeatable.pm »
1 package HTML::FormHandler::Field::RequestToken;
2 # ABSTRACT: Hidden text field which contains a unique time-stamped token
3 1 $HTML::FormHandler::Field::RequestToken::VERSION = '0.410002';
4 1 ▶use HTML::FormHandler::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'HTML::FormHandler::Field::Hidden';
6  
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MIME::Base64 qw(decode_base64 encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Moose::Util::TypeConstraints qw(class_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has '+required' => ( default => 1 );
15  
16 1 ▶has '+default_method' => ( default => sub { \&get_token });
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18  
19 1 has 'expiration_time' => (
20 is => 'rw',
21 default => 3600
22 );
23  
24  
25 1 has 'token_prefix' => (
26 is => 'rw',
27 default => ''
28 );
29  
30  
31 1 has 'crypto_key' => (
32 is => 'rw',
33 default => 'rEpLaCeMe',
34 );
35  
36  
37 1 has 'crypto_cipher_type' => (
38 is => 'rw',
39 default => 'Blowfish',
40 );
41  
42  
43 1 has 'message' => (
44 is => 'rw',
45 default => 'Form submission failed. Please try again.'
46 );
47  
48  
49 1 has 'cipher' => (
50 is => 'ro',
51 isa => class_type('Crypt::CBC'),
52 lazy => 1,
53 builder => '_build_cipher',
54 );
55  
56 1 ▶sub _build_cipher {
Subroutine _build_cipher: called CC=1 SCAR=0.0
57 1 my ($self) = @_;
58 1 return Crypt::CBC->new(
59 -key => $self->crypto_key,
60 -cipher => $self->crypto_cipher_type,
61 -salt => 1,
62 -header => 'salt',
63 );
64 }
65  
66 1 ▶sub validate {
Subroutine validate: called CC=2 SCAR=7.1
Pod documented
67 1 my ($self, $value) = @_;
68  
69 # If it's good, return it
70 1 ▶ unless ( $self->verify_token($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->verify_token($value)) 1 0 1
71 1 $self->add_error();
72 }
73  
74 }
75  
76  
77 1 ▶sub verify_token {
Subroutine verify_token: called CC=5 SCAR=24.1
Pod documented
78 1 my ($self, $token) = @_;
79  
80 1 ▶ return undef unless($token);
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 1 1
81  
82 1 my $form = $self->form;
83  
84 1 my $value = undef;
85 1 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=21.6
86 1 $value = $self->cipher->decrypt(decode_base64($token));
87 0 ▶ if ( my $prefix = $self->token_prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefix = $self->token_prefix) 0 0 0
88 0 ▶ return undef unless ($value =~ s/^\Q$prefix\E//);
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ s/^\Q$prefix\E// 0 0 0
89 }
90 1 ▶ } catch {};
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92 1 ▶ return undef unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
93 0 ▶ return undef unless ( $value =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+$/ 0 0 0
94 0 ▶ return undef if ( time() > $value );
Branch: 1 branch
BranchTrueFalseTotal
if time > $value 0 0 0
95  
96 0 return 1;
97 }
98  
99  
100 1 ▶sub get_token {
Subroutine get_token: called CC=1 SCAR=0.0
Pod documented
101 1 my $self = shift;
102  
103 1 my $value = $self->token_prefix . (time() + $self->expiration_time);
104 1 my $token = encode_base64($self->cipher->encrypt($value));
105 1 $token =~ s/[\s\r\n]+//g;
106 1 return $token;
107 }
108  
109 1 __PACKAGE__->meta->make_immutable;
110  
111 1 1;
112  
113 __END__
« FormHandler/Render/Util.pm ↑ Summary FormHandler/Widget/Field/Repeatable.pm »