Mojolicious/Sessions.pm

filter: Statementstmt 77.5% 38 / 49 Branchbran 38.4% 10 / 26 Conditioncond 23.8% 5 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 52.0% 63 / 121 CC 21 SCAR 36.5
CC
21
Cov
55%
load
87.2
store
13.5
_deserialize
2.0
CRAP 38.5
« Mojo/Server/PSGI.pm ↑ Summary Mojolicious/Plugin/HeaderCondition.pm »
1 package Mojolicious::Sessions;
2 20 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::Util qw(b64_decode b64_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 has [qw(cookie_domain encrypted partitioned secure)];
8 20 has cookie_name => 'mojolicious';
9 20 has cookie_path => '/';
10 20 has default_expiration => 3600;
11 0 ▶has deserialize => sub { \&_deserialize };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 20 has samesite => 'Lax';
13 1 ▶has serialize => sub { \&_serialize };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 23 ▶sub load {
Subroutine load: called CC=11 SCAR=44.7
Pod documented
16 23 my ($self, $c) = @_;
17  
18 23 ▶ my $method = $self->encrypted ? 'encrypted_cookie' : 'signed_cookie';
Branch: 1 branch
BranchTrueFalseTotal
$self->encrypted ? : 0 23 23
19 23 ▶ return unless my $value = $c->$method($self->cookie_name);
Branch: 1 branch
BranchTrueFalseTotal
unless my $value = $c->$method($self->cookie_name) 23 0 23
20 0 $value =~ y/-/=/;
21 0 ▶ return unless my $session = $self->deserialize->(b64_decode $value);
Branch: 1 branch
BranchTrueFalseTotal
unless my $session = $self->deserialize->(&b64_decode($value)) 0 0 0
22  
23 # "expiration" value is inherited
24 0 ▶ my $expiration = $session->{expiration} // $self->default_expiration;
Condition0%0/3
$session->{"expiration"} // $self->default_expiration
l!l&&r!l&&!r
000
MC/DC0%0/2
$session->{"expiration"} // $self->default_expiration
$session->{"expiration"} $self->default_expiration
Truth table0%0/3
A // B
A= $session->{"expiration"}B= $self->default_expiration
ABresult
1X1
011
000
25 0 ▶ return if !(my $expires = delete $session->{expires}) && $expiration;
Branch: 1 branch
BranchTrueFalseTotal
if not my $expires = delete $session->{"expires"} and $expiration 0 0 0
Condition0%0/3
not my $expires = delete $session->{"expires"} and $expiration
!ll&&!rl&&r
000
MC/DC0%0/2
not my $expires = delete $session->{"expires"} and $expiration
not my $expires = delete $session->{"expires"} $expiration
Truth table0%0/3
not(A) and B
A= not my $expires = delete $session->{"expires"}B= $expiration
ABresult
0X0
100
111
26 0 ▶ return if defined $expires && $expires <= time;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires and $expires <= time 0 0 0
Condition0%0/3
defined $expires and $expires <= time
!ll&&!rl&&r
000
MC/DC0%0/2
defined $expires and $expires <= time
defined $expires $expires <= time
Truth table0%0/3
A and B
A= defined $expiresB= $expires <= time
ABresult
0X0
100
111
27  
28 0 my $stash = $c->stash;
29 0 ▶ return unless $stash->{'mojo.active_session'} = keys %$session;
Branch: 1 branch
BranchTrueFalseTotal
unless $stash->{"mojo.active_session"} = keys %$session 0 0 0
30 0 $stash->{'mojo.session'} = $session;
31 0 ▶ $session->{flash} = delete $session->{new_flash} if $session->{new_flash};
Branch: 1 branch
BranchTrueFalseTotal
if $session->{"new_flash"} 0 0 0
32 }
33  
34 171 ▶sub store {
Subroutine store: called CC=11 SCAR=26.0
Pod documented
35 171 my ($self, $c) = @_;
36  
37 # Make sure session was active
38 171 my $stash = $c->stash;
39 171 ▶ return unless my $session = $stash->{'mojo.session'};
Branch: 1 branch
BranchTrueFalseTotal
unless my $session = $stash->{"mojo.session"} 159 12 171
40 12 ▶ return unless keys %$session || $stash->{'mojo.active_session'};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$session or $stash->{"mojo.active_session"} 11 1 12
Condition66%2/3
keys %$session or $stash->{"mojo.active_session"}
l!l&&r!l&&!r
1011
MC/DC50%1/2
keys %$session or $stash->{"mojo.active_session"}
keys %$session $stash->{"mojo.active_session"}
Truth table66%2/3
A or B
A= keys %$sessionB= $stash->{"mojo.active_session"}
ABresult
1X1
011
000
41  
42 # Don't reset flash for static files
43 1 my $old = delete $session->{flash};
44 1 ▶ $session->{new_flash} = $old if $stash->{'mojo.static'};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"mojo.static"} 0 1 1
45 1 ▶ delete $session->{new_flash} unless keys %{$session->{new_flash}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$session{"new_flash"};} 1 0 1
46  
47 # Generate "expires" value from "expiration" if necessary
48 1 ▶ my $expiration = $session->{expiration} // $self->default_expiration;
Condition33%1/3
$session->{"expiration"} // $self->default_expiration
l!l&&r!l&&!r
010
MC/DC0%0/2
$session->{"expiration"} // $self->default_expiration
$session->{"expiration"} $self->default_expiration
Truth table33%1/3
A // B
A= $session->{"expiration"}B= $self->default_expiration
ABresult
1X1
011
000
49 1 my $default = delete $session->{expires};
50 1 ▶ $session->{expires} = $default || time + $expiration if $expiration || $default;
Branch: 1 branch
BranchTrueFalseTotal
if $expiration or $default 1 0 1
Condition33%2/6
$expiration or $default
l!l&&r!l&&!r
100
$default || time + $expiration
l!l&&r!l&&!r
010
MC/DC0%0/2
$expiration or $default
$expiration $default
MC/DC0%0/2
$default || time + $expiration
$default time + $expiration
Truth table33%1/3
A or B
A= $expirationB= $default
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $defaultB= time + $expiration
ABresult
1X1
011
000
51  
52 1 my $value = b64_encode $self->serialize->($session), '';
53 1 $value =~ y/=/-/;
54 my $options = {
55 domain => $self->cookie_domain,
56 expires => $session->{expires},
57 1 httponly => 1,
58 partitioned => $self->partitioned,
59 path => $self->cookie_path,
60 samesite => $self->samesite,
61 secure => $self->secure,
62 };
63 1 ▶ my $method = $self->encrypted ? 'encrypted_cookie' : 'signed_cookie';
Branch: 1 branch
BranchTrueFalseTotal
$self->encrypted ? : 0 1 1
64 1 $c->$method($self->cookie_name, $value, $options);
65 }
66  
67 # DEPRECATED! (Remove once old sessions with padding are no longer a concern)
68 0 ▶sub _deserialize { Mojo::JSON::decode_json($_[0] =~ s/\}\KZ*$//r) }
Subroutine _deserialize: not called CC=1 SCAR=6.9
69  
70 1 ▶sub _serialize { Mojo::JSON::encode_json($_[0]) }
Subroutine _serialize: called CC=1 SCAR=0.0
71  
72 20 1;
73  
74 =encoding utf8
75  
76 =head1 NAME
77  
78 Mojolicious::Sessions - Session manager based on signed cookies
79  
80 =head1 SYNOPSIS
81  
82 use Mojolicious::Sessions;
83  
84 my $sessions = Mojolicious::Sessions->new;
85 $sessions->cookie_name('myapp');
86 $sessions->default_expiration(86400);
87  
88 =head1 DESCRIPTION
89  
90 L<Mojolicious::Sessions> manages sessions based on signed cookies for L<Mojolicious>. All data gets serialized with
91 L<Mojo::JSON> and stored Base64 encoded on the client-side, but is protected from unwanted changes with a HMAC-SHA256
92 signature.
93  
94 =head1 ATTRIBUTES
95  
96 L<Mojolicious::Sessions> implements the following attributes.
97  
98 =head2 cookie_domain
99  
100 my $domain = $sessions->cookie_domain;
101 $sessions = $sessions->cookie_domain('.example.com');
102  
103 Domain for session cookies, not defined by default.
104  
105 =head2 cookie_name
106  
107 my $name = $sessions->cookie_name;
108 $sessions = $sessions->cookie_name('session');
109  
110 Name for session cookies, defaults to C<mojolicious>.
111  
112 =head2 cookie_path
113  
114 my $path = $sessions->cookie_path;
115 $sessions = $sessions->cookie_path('/foo');
116  
117 Path for session cookies, defaults to C</>.
118  
119 =head2 default_expiration
120  
121 my $time = $sessions->default_expiration;
122 $sessions = $sessions->default_expiration(3600);
123  
124 Default time for sessions to expire in seconds from now, defaults to C<3600>. The expiration timeout gets refreshed for
125 every request. Setting the value to C<0> will allow sessions to persist until the browser window is closed, this can
126 have security implications though. For more control you can also use the C<expiration> and C<expires> session values.
127  
128 # Expiration date in seconds from now (persists between requests)
129 $c->session(expiration => 604800);
130  
131 # Expiration date as absolute epoch time (only valid for one request)
132 $c->session(expires => time + 604800);
133  
134 # Delete whole session by setting an expiration date in the past
135 $c->session(expires => 1);
136  
137 =head2 deserialize
138  
139 my $cb = $sessions->deserialize;
140 $sessions = $sessions->deserialize(sub ($bytes) {...});
141  
142 A callback used to deserialize sessions, defaults to L<Mojo::JSON/"j">.
143  
144 $sessions->deserialize(sub ($bytes) { return {} });
145  
146 =head2 encrypted
147  
148 my $bool = $sessions->encrypted;
149 $sessions = $sessions->encrypted($bool);
150  
151 Use encrypted session cookies instead of merely cryptographically signed ones.
152  
153 =head2 partitioned
154  
155 my $bool = $sessions->partitioned;
156 $sessions = $sessions->partitioned($bool);
157  
158 Partitioned flag, this is to be used in accordance to the CHIPS amendment to RFC 6265.
159 Note that this attribute is B<EXPERIMENTAL> because even though most commonly used browsers support the
160 feature, there is no specification yet besides L<this
161 draft|https://www.ietf.org/archive/id/draft-cutler-httpbis-partitioned-cookies-00.html>.
162  
163 Partitioned cookies are held within a separate cookie jar per top-level site.
164  
165 =head2 samesite
166  
167 my $samesite = $sessions->samesite;
168 $sessions = $sessions->samesite('Strict');
169  
170 Set the SameSite value on all session cookies, defaults to C<Lax>.
171  
172 # Disable SameSite feature
173 $sessions->samesite(undef);
174  
175 =head2 secure
176  
177 my $bool = $sessions->secure;
178 $sessions = $sessions->secure($bool);
179  
180 Set the secure flag on all session cookies, so that browsers send them only over HTTPS connections.
181  
182 =head2 serialize
183  
184 my $cb = $sessions->serialize;
185 $sessions = $sessions->serialize(sub ($hash) {...});
186  
187 A callback used to serialize sessions, defaults to L<Mojo::JSON/"encode_json">.
188  
189 $sessions->serialize(sub ($hash) { return '' });
190  
191 =head1 METHODS
192  
193 L<Mojolicious::Sessions> inherits all methods from L<Mojo::Base> and implements the following new ones.
194  
195 =head2 load
196  
197 $sessions->load(Mojolicious::Controller->new);
198  
199 Load session data from signed cookie.
200  
201 =head2 store
202  
203 $sessions->store(Mojolicious::Controller->new);
204  
205 Store session data in signed cookie.
206  
207 =head1 SEE ALSO
208  
209 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
210  
211 =cut
« Mojo/Server/PSGI.pm ↑ Summary Mojolicious/Plugin/HeaderCondition.pm »