| 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
Condition33%1/3
defined $config->{"mac_secret"} and length $config->{"mac_secret"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | $config->{path} = '/' | ||||||||||||||||||||||||||||
| 30 | 1 | ▶ | unless exists $config->{path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$cookie and $cookie->value
MC/DC0%0/2
$cookie and $cookie->value
$cookie
$cookie->value
Truth table0%0/3
A and B
A= $cookieB= $cookie->value
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$value{"MAC"} and $value{"MAC"} eq $mac
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
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | return \%value; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||