File Coverage

blib/lib/Plack/Session/State/Cookie.pm
Criterion Covered Total %
statement 43 43 100.0
branch 16 16 100.0
condition 18 20 90.0
subroutine 11 11 100.0
pod 3 4 75.0
total 91 94 96.8


line stmt bran cond sub pod time code
1             package Plack::Session::State::Cookie;
2 11     11   642407 use strict;
  11         51  
  11         459  
3 11     11   62 use warnings;
  11         34  
  11         2070  
4              
5             our $VERSION = '0.36';
6             our $AUTHORITY = 'cpan:STEVAN';
7              
8 11     11   695 use parent 'Plack::Session::State';
  11         362  
  11         113  
9 11     11   709 use Cookie::Baker;
  11         24  
  11         860  
10 11     11   3488 use Plack::Util;
  11         21978  
  11         472  
11              
12 11         94 use Plack::Util::Accessor qw[
13             path
14             domain
15             expires
16             secure
17             httponly
18             samesite
19             partitioned
20 11     11   61 ];
  11         21  
21              
22             sub get_session_id {
23 28     28 1 128 my ($self, $env) = @_;
24 28         172 crush_cookie($env->{HTTP_COOKIE})->{$self->session_key};
25             }
26              
27             sub merge_options {
28 33     33 0 174 my($self, %options) = @_;
29              
30 33         90 delete $options{id};
31              
32 33 100 100     178 $options{path} = $self->path || '/' if !exists $options{path};
33 33 100 100     373 $options{domain} = $self->domain if !exists $options{domain} && defined $self->domain;
34 33 100 100     296 $options{secure} = $self->secure if !exists $options{secure} && defined $self->secure;
35 33 100 66     310 $options{httponly} = $self->httponly if !exists $options{httponly} && defined $self->httponly;
36 33 100 66     278 $options{samesite} = $self->samesite if !exists $options{samesite} && defined $self->samesite;
37              
38             # https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
39 33 100 100     363 $options{partitioned} = $self->partitioned if !exists $options{partitioned} && defined $self->partitioned;
40              
41              
42 33 100 100     287 if (!exists $options{expires} && defined $self->expires) {
43 2         17 $options{expires} = time + $self->expires;
44             }
45              
46 33 100       192 if ($options{partitioned}) {
47 4         12 $options{secure} = 1;
48 4         13 $options{samesite} = 'None';
49             }
50              
51 33         215 return %options;
52             }
53              
54             sub expire_session_id {
55 2     2 1 27 my ($self, $id, $res, $options) = @_;
56 2         11 my %opts = $self->merge_options(%$options, expires => time);
57 2         9 $self->_set_cookie($id, $res, %opts);
58             }
59              
60             sub finalize {
61 26     26 1 180 my ($self, $id, $res, $options) = @_;
62 26         110 my %opts = $self->merge_options(%$options);
63 26         107 $self->_set_cookie($id, $res, %opts);
64             }
65              
66             sub _set_cookie {
67 28     28   93 my($self, $id, $res, %options) = @_;
68              
69 28         107 my $cookie = bake_cookie(
70             $self->session_key, {
71             value => $id,
72             %options,
73             }
74             );
75 28         2530 Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
76             }
77              
78             1;
79              
80             __END__