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

filter: Statementstmt 100% 55 / 55 Branchbran 75.0% 12 / 16 Conditioncond 64.7% 11 / 17 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 85.5% 95 / 111 CC 19 SCAR 14.0
CC
19
Cov
89%
_format_cookie
6.3
_parse_cookies
5.2
_get_header
4.0
CRAP 4.0
« blib/lib/PAGI/Middleware/Session.pm ↑ Summary blib/lib/PAGI/Stash.pm »
1 package PAGI::Middleware::Session::State::Cookie;
2 3 $PAGI::Middleware::Session::State::Cookie::VERSION = '0.002001';
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 sub new {
54 26 ▶ my ($class, %options) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
55  
56 26 ▶ $options{cookie_name} //= 'pagi_session';
Condition100%2/2
$options{"cookie_name"} //= "pagi_session"
l!l
1214
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 26 ▶ httponly => 1,
Condition100%2/2
$options{"cookie_options"} //= {"httponly", 1, "path", "/", "samesite", "Lax"}
l!l
1214
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 26 ▶ $options{expire} //= 3600;
Condition100%2/2
$options{"expire"} //= 3600
l!l
1016
MC/DC100%1/1
$options{"expire"} //= 3600
$options{"expire"}
Truth table100%2/2
A
A= $options{"expire"}
Aresult
11
00
63  
64 26 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 sub extract {
78 20 ▶ my ($self, $scope) = @_;
Subroutine extract: called CC=2 SCAR=6.9
Pod documented
79  
80 20 my $cookie_header = $self->_get_header($scope, 'cookie');
81 20 ▶ return unless defined $cookie_header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie_header 10 10 20
82  
83 10 my $cookies = $self->_parse_cookies($cookie_header);
84 10 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 sub inject {
97 16 ▶ my ($self, $headers, $id, $options) = @_;
Subroutine inject: called CC=1 SCAR=0.0
Pod documented
98  
99 16 my $cookie = $self->_format_cookie($id);
100 16 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 sub clear {
113 5 ▶ my ($self, $headers) = @_;
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
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 sub _get_header {
120 20 ▶ my ($self, $scope, $name) = @_;
Subroutine _get_header: called CC=4 SCAR=13.9
121  
122 20 $name = lc($name);
123 20 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
200
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
124 11 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 10 1 11
125 }
126 10 return;
127 }
128  
129 sub _parse_cookies {
130 10 ▶ my ($self, $header) = @_;
Subroutine _parse_cookies: called CC=5 SCAR=16.6
131  
132 10 my %cookies;
133 10 for my $pair (split /\s*;\s*/, $header) {
134 13 my ($name, $value) = split /=/, $pair, 2;
135 13 ▶ next unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 0 13 13
Condition33%1/3
defined $name and $name ne ""
!ll&&!rl&&r
0013
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 13 $name =~ s/^\s+//;
137 13 $name =~ s/\s+$//;
138 13 ▶ $value //= '';
Condition50%1/2
$value //= ""
l!l
130
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
139 13 $value =~ s/^\s+//;
140 13 $value =~ s/\s+$//;
141 13 $cookies{$name} = $value;
142 }
143 10 return \%cookies;
144 }
145  
146 sub _format_cookie {
147 16 ▶ my ($self, $session_id) = @_;
Subroutine _format_cookie: called CC=6 SCAR=18.5
148  
149 16 my $cookie = "$self->{cookie_name}=$session_id";
150 16 my $opts = $self->{cookie_options};
151  
152 16 ▶ $cookie .= "; Path=" . ($opts->{path} // '/');
Condition50%1/2
$opts->{"path"} // "/"
l!l
160
MC/DC0%0/1
$opts->{"path"} // "/"
$opts->{"path"}
Truth table50%1/2
A
A= $opts->{"path"}
Aresult
11
00
153 16 ▶ $cookie .= "; HttpOnly" if $opts->{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"httponly"} 16 0 16
154 16 ▶ $cookie .= "; Secure" if $opts->{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"secure"} 1 15 16
155 16 ▶ $cookie .= "; SameSite=$opts->{samesite}" if $opts->{samesite};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"samesite"} 16 0 16
156 16 ▶ $cookie .= "; Max-Age=$self->{expire}" if $self->{expire};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"expire"} 16 0 16
157  
158 16 return $cookie;
159 }
160  
161 3 1;
162  
163 __END__
« blib/lib/PAGI/Middleware/Session.pm ↑ Summary blib/lib/PAGI/Stash.pm »