blib/lib/PAGI/Middleware/Session/State/Cookie.pm

filter: Statementstmt 100% 55 / 55 Branchbran 75.0% 12 / 16 Conditioncond 70.5% 12 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 87.3% 97 / 111 CC 19 SCAR 13.9
CC
19
Cov
90%
_format_cookie
6.3
_parse_cookies
5.1
_get_header
4.0
CRAP 4.0
« blib/lib/PAGI/Stash.pm ↑ Summary lib/PAGI/Middleware/RequestId.pm »
1 package PAGI::Middleware::Session::State::Cookie;
2 3 $PAGI::Middleware::Session::State::Cookie::VERSION = '0.002002';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent 'PAGI::Middleware::Session::State';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 PAGI::Middleware::Session::State::Cookie - Cookie-based session ID transport
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::Middleware::Session::State::Cookie;
14  
15 my $state = PAGI::Middleware::Session::State::Cookie->new(
16 cookie_name => 'pagi_session',
17 cookie_options => { httponly => 1, path => '/', samesite => 'Lax' },
18 expire => 3600,
19 );
20  
21 # Extract session ID from request
22 my $id = $state->extract($scope);
23  
24 # Inject Set-Cookie header into response
25 $state->inject(\@headers, $id, {});
26  
27 =head1 DESCRIPTION
28  
29 Implements the L<PAGI::Middleware::Session::State> interface using HTTP
30 cookies for session ID transport. The session ID is read from the Cookie
31 request header and set via the Set-Cookie response header.
32  
33 =head1 CONFIGURATION
34  
35 =over 4
36  
37 =item * cookie_name (default: 'pagi_session')
38  
39 Name of the cookie used to store the session ID.
40  
41 =item * cookie_options (default: { httponly => 1, path => '/', samesite => 'Lax' })
42  
43 Cookie attributes applied when setting the response cookie.
44  
45 =item * expire (default: 3600)
46  
47 Max-Age value for the session cookie, in seconds.
48  
49 =back
50  
51 =cut
52  
53 37 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
54 37 my ($class, %options) = @_;
55  
56 37 ▶ $options{cookie_name} //= 'pagi_session';
Condition100%2/2
$options{"cookie_name"} //= "pagi_session"
l!l
2314
MC/DC100%1/1
$options{"cookie_name"} //= "pagi_session"
$options{"cookie_name"}
Truth table100%2/2
A
A= $options{"cookie_name"}
Aresult
11
00
57 $options{cookie_options} //= {
58 37 ▶ httponly => 1,
Condition100%2/2
$options{"cookie_options"} //= {"httponly", 1, "path", "/", "samesite", "Lax"}
l!l
2314
MC/DC100%1/1
$options{"cookie_options"} //= {"httponly", 1, "path", "/", "samesite", "Lax"}
$options{"cookie_options"}
Truth table100%2/2
A
A= $options{"cookie_options"}
Aresult
11
00
59 path => '/',
60 samesite => 'Lax',
61 };
62 37 ▶ $options{expire} //= 3600;
Condition100%2/2
$options{"expire"} //= 3600
l!l
2116
MC/DC100%1/1
$options{"expire"} //= 3600
$options{"expire"}
Truth table100%2/2
A
A= $options{"expire"}
Aresult
11
00
63  
64 37 return $class->SUPER::new(%options);
65 }
66  
67 =head2 extract
68  
69 my $session_id = $state->extract($scope);
70  
71 Find the Cookie header in C<$scope-E<gt>{headers}> (case-insensitive),
72 parse the cookie string, and return the value matching C<cookie_name>.
73 Returns undef if no matching cookie is found.
74  
75 =cut
76  
77 42 ▶sub extract {
Subroutine extract: called CC=2 SCAR=6.9
Pod documented
78 42 my ($self, $scope) = @_;
79  
80 42 my $cookie_header = $self->_get_header($scope, 'cookie');
81 42 ▶ return unless defined $cookie_header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie_header 20 22 42
82  
83 22 my $cookies = $self->_parse_cookies($cookie_header);
84 22 return $cookies->{$self->{cookie_name}};
85 }
86  
87 =head2 inject
88  
89 $state->inject(\@headers, $id, \%options);
90  
91 Format a Set-Cookie string and push C<['Set-Cookie', $cookie_string]>
92 onto the provided headers arrayref.
93  
94 =cut
95  
96 28 ▶sub inject {
Subroutine inject: called CC=1 SCAR=0.0
Pod documented
97 28 my ($self, $headers, $id, $options) = @_;
98  
99 28 my $cookie = $self->_format_cookie($id);
100 28 push @$headers, ['Set-Cookie', $cookie];
101 }
102  
103 =head2 clear
104  
105 $state->clear(\@headers);
106  
107 Expire the session cookie by pushing a Set-Cookie header with
108 C<Max-Age=0>. Called when a session is destroyed.
109  
110 =cut
111  
112 5 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
113 5 my ($self, $headers) = @_;
114 5 ▶ my $cookie = "$self->{cookie_name}=; Path=" . ($self->{cookie_options}{path} // '/') . "; Max-Age=0";
Condition50%1/2
$self->{"cookie_options"}{"path"} // "/"
l!l
50
MC/DC0%0/1
$self->{"cookie_options"}{"path"} // "/"
$self->{"cookie_options"}{"path"}
Truth table50%1/2
A
A= $self->{"cookie_options"}{"path"}
Aresult
11
00
115 5 ▶ $cookie .= "; HttpOnly" if $self->{cookie_options}{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cookie_options"}{"httponly"} 4 1 5
116 5 push @$headers, ['Set-Cookie', $cookie];
117 }
118  
119 42 ▶sub _get_header {
Subroutine _get_header: called CC=4 SCAR=13.9
120 42 my ($self, $scope, $name) = @_;
121  
122 42 $name = lc($name);
123 42 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
420
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
124 23 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 22 1 23
125 }
126 20 return;
127 }
128  
129 22 ▶sub _parse_cookies {
Subroutine _parse_cookies: called CC=5 SCAR=16.3
130 22 my ($self, $header) = @_;
131  
132 22 my %cookies;
133 22 for my $pair (split /\s*;\s*/, $header) {
134 33 my ($name, $value) = split /=/, $pair, 2;
135 33 ▶ next unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 33 33
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
0033
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table33%1/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
136 33 $name =~ s/^\s+//;
137 33 $name =~ s/\s+$//;
138 33 ▶ $value //= '';
Condition100%2/2
$value //= ""
l!l
312
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
139 33 $value =~ s/^\s+//;
140 33 $value =~ s/\s+$//;
141 33 $cookies{$name} = $value;
142 }
143 22 return \%cookies;
144 }
145  
146 28 ▶sub _format_cookie {
Subroutine _format_cookie: called CC=6 SCAR=18.5
147 28 my ($self, $session_id) = @_;
148  
149 28 my $cookie = "$self->{cookie_name}=$session_id";
150 28 my $opts = $self->{cookie_options};
151  
152 28 ▶ $cookie .= "; Path=" . ($opts->{path} // '/');
Condition50%1/2
$opts->{"path"} // "/"
l!l
280
MC/DC0%0/1
$opts->{"path"} // "/"
$opts->{"path"}
Truth table50%1/2
A
A= $opts->{"path"}
Aresult
11
00
153 28 ▶ $cookie .= "; HttpOnly" if $opts->{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"httponly"} 28 0 28
154 28 ▶ $cookie .= "; Secure" if $opts->{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"secure"} 1 27 28
155 28 ▶ $cookie .= "; SameSite=$opts->{samesite}" if $opts->{samesite};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"samesite"} 28 0 28
156 28 ▶ $cookie .= "; Max-Age=$self->{expire}" if $self->{expire};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"expire"} 28 0 28
157  
158 28 return $cookie;
159 }
160  
161 3 1;
162  
163 __END__
« blib/lib/PAGI/Stash.pm ↑ Summary lib/PAGI/Middleware/RequestId.pm »