blib/lib/Catalyst/Plugin/AuthenCookie.pm

filter: Statementstmt 37.8% 25 / 66 Branchbran 21.4% 3 / 14 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 6 / 12 Podpod 75.0% 3 / 4 totaltotal 34.2% 38 / 111 CC 12 SCAR 22.8
CC
12
Cov
33%
authen_cookie_value
30.0
setup
6.3
set_authen_cookie
6.0
CRAP 9.8
↑ Summary
1 package Catalyst::Plugin::AuthenCookie;
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 our $VERSION = '0.07';
7  
8 # No doubt SHA-512 is way overkill but it can't hurt (I hope).
9 1 ▶use Digest::SHA qw( sha512_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Params::Validate qw( validate HASHREF SCALAR );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub setup
14 1 ▶{
Subroutine setup: called CC=5 SCAR=18.4
Pod undocumented
15 1 my $self = shift;
16  
17 1 $self->next::method(@_);
18  
19 1 my $config = $self->config()->{authen_cookie};
20  
21 1 ▶ unless ( defined $config->{mac_secret} && length $config->{mac_secret} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"mac_secret"} and length $config->{"mac_secret"}) 0 1 1
Condition33%1/3
defined $config->{"mac_secret"} and length $config->{"mac_secret"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $config->{"mac_secret"} and length $config->{"mac_secret"}
defined $config->{"mac_secret"} length $config->{"mac_secret"}
Truth table33%1/3
A and B
A= defined $config->{"mac_secret"}B= length $config->{"mac_secret"}
ABresult
0X0
100
111
22 {
23 0 die "You must provide a mac_secret config value for the AuthenCookie plugin\n";
24 }
25  
26 $config->{name} = 'authen-cookie'
27 1 ▶ unless exists $config->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"name"} 1 0 1
28  
29 $config->{path} = '/'
30 1 ▶ unless exists $config->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{"path"} 1 0 1
31 }
32  
33 {
34 1 my $spec = { value => { type => HASHREF },
35 expires => { type => SCALAR, default => undef },
36 };
37  
38 sub set_authen_cookie
39 0 ▶ {
Subroutine set_authen_cookie: not called CC=2 SCAR=17.9
Pod documented
40 0 my $self = shift;
41 0 my %p = validate( @_, $spec );
42  
43 0 my $value = $p{value};
44  
45 0 $value->{MAC} = $self->_cookie_mac($value);
46  
47 0 my %cookie = $self->_cookie_from_config();
48  
49 0 $cookie{value} = $value;
50  
51 $cookie{expires} = $p{expires}
52 0 ▶ if $p{expires};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"expires"} 0 0 0
53  
54 0 my $config = $self->_authen_cookie_config();
55  
56 0 $self->response()->cookies()->{ $config->{name} } = \%cookie;
57 }
58 }
59  
60 sub _cookie_from_config
61 1 ▶{
Subroutine _cookie_from_config: not called CC=2 SCAR=15.9
62 0 my $self = shift;
63  
64 0 my $config = $self->_authen_cookie_config();
65  
66 0 my %cookie;
67  
68 $cookie{domain} = $config->{domain}
69 0 ▶ if $config->{domain};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"domain"} 0 0 0
70  
71 0 $cookie{path} = $config->{path};
72  
73 0 $cookie{secure} = $config->{secure};
74  
75 0 return %cookie;
76 }
77  
78 sub _authen_cookie_config
79 0 ▶{
Subroutine _authen_cookie_config: not called CC=1 SCAR=6.9
80 0 my $self = shift;
81  
82 0 return $self->config()->{authen_cookie};
83 }
84  
85 sub _cookie_mac
86 0 ▶{
Subroutine _cookie_mac: not called CC=2 SCAR=17.9
87 0 my $self = shift;
88 0 my $value = shift;
89  
90 0 my @hash_data;
91 0 for my $key ( sort grep { $_ ne 'MAC' } keys %{ $value } )
92 {
93 0 push @hash_data, $key, $value->{$key};
94 }
95  
96 0 my $config = $self->_authen_cookie_config();
97  
98 0 return sha512_base64( @hash_data, $config->{mac_secret} );
99 }
100  
101 sub unset_authen_cookie
102 0 ▶{
Subroutine unset_authen_cookie: not called CC=1 SCAR=6.9
Pod documented
103 0 my $self = shift;
104  
105 0 my %cookie = ( $self->_cookie_from_config(),
106 value => '',
107 expires => '-1y',
108 );
109  
110 0 my $config = $self->_authen_cookie_config();
111  
112 0 $self->response()->cookies()->{ $config->{name} } = \%cookie;
113  
114 0 return;
115 }
116  
117 sub authen_cookie_value
118 0 ▶{
Subroutine authen_cookie_value: not called CC=5 SCAR=34.0
Pod documented
119 0 my $self = shift;
120  
121 0 my $config = $self->_authen_cookie_config();
122  
123 0 my $cookie = $self->request()->cookies()->{ $config->{name} };
124  
125 0 ▶ return unless $cookie && $cookie->value();
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie and $cookie->value 0 0 0
Condition0%0/3
$cookie and $cookie->value
!ll&&!rl&&r
000
MC/DC0%0/2
$cookie and $cookie->value
$cookie $cookie->value
Truth table0%0/3
A and B
A= $cookieB= $cookie->value
ABresult
0X0
100
111
126  
127 0 my %value = $cookie->value();
128  
129 0 my $mac = $self->_cookie_mac(\%value);
130  
131 0 ▶ return unless $value{MAC} && $value{MAC} eq $mac;
Branch: 1 branch
BranchTrueFalseTotal
unless $value{"MAC"} and $value{"MAC"} eq $mac 0 0 0
Condition0%0/3
$value{"MAC"} and $value{"MAC"} eq $mac
!ll&&!rl&&r
000
MC/DC0%0/2
$value{"MAC"} and $value{"MAC"} eq $mac
$value{"MAC"} $value{"MAC"} eq $mac
Truth table0%0/3
A and B
A= $value{"MAC"}B= $value{"MAC"} eq $mac
ABresult
0X0
100
111
132  
133 0 return \%value;
134 }
135  
136 1 1;
137  
138 __END__
↑ Summary