Sessions/ThreeS.pm

filter: Statementstmt 88.3% 83 / 94 Branchbran 70.0% 28 / 40 Conditioncond 56.5% 13 / 23 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 17 / 17 Podpod 100% 9 / 9 totaltotal 77.2% 153 / 198 CC 29 SCAR 21.9
CC
29
Cov
79%
store
15.6
load
15.2
cookie_domain
2.1
CRAP 9.0
↑ Summary Plugin/Sessions3S.pm »
1 package Mojolicious::Sessions::ThreeS;
2 3 $Mojolicious::Sessions::ThreeS::VERSION = '0.004';
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  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Mojo::Base 'Mojolicious::Sessions';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Mojolicious::Sessions::ThreeS::State::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Mojolicious::Sessions::ThreeS::SidGen::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Mojolicious::Sessions:ThreeS - A Mojolicious Sessions manager that supports controlling Storage, State and Sid generation.
15  
16 =head1 SYNOPSIS
17  
18 You can use this directly when you build your mojolicious App:
19  
20 package My::App;
21 use Mojo::Base 'Mojolicious';
22  
23 use Mojolicious::Sessions:ThreeS;
24  
25 sub startup{
26 my ($app) = @_;
27 ...
28 $app->sessions( Mojolicious::Sessions:ThreeS->new({ storage => ... , state => ... , sidgen => ... } ) );
29 ...
30 }
31  
32 Or as a plugin, with exactly the same arguments. See L<Mojolicious::Plugin::Sessions3S>.
33  
34 =cut
35  
36 3 has 'storage';
37 1 ▶has 'state' => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 return Mojolicious::Sessions::ThreeS::State::Cookie->new();
39 3 };
40 3 ▶has 'sidgen' => sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
41 3 return Mojolicious::Sessions::ThreeS::SidGen::Simple->new();
42 3 };
43  
44 =head2 new
45  
46 Builds an instance of this given the following properties (all optional):
47  
48 =over
49  
50 =item storage
51  
52 An instance of a subclass of L<Mojolicious::Sessions::ThreeS::Storage>. Defaults to C<undef>
53  
54 When not given, this will consider itself inactive and fallback to the default L<Mojolicious::Sessions> behaviour.
55  
56 =item state
57  
58 An instance of a subclass of L<Mojolicious::Sessions::ThreeS::State>. Defaults to
59 an instance of L<Mojolicious::Sessions::ThreeS::State::Cookie>.
60  
61 =item sidgen
62  
63 An Session ID generator, instance of L<Mojolicious::Sessions::ThreeS::SidGen>. Defaults
64 to an instance of L<Mojolicious::Sessions::ThreeS::SidGen::Simple>.
65  
66 =back
67  
68 =cut
69  
70 3 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
71 3 my ( $class ) = ( shift );
72  
73 3 my $self = $class->SUPER::new( @_ );
74  
75 3 return $self;
76 }
77  
78 =head2 was_set
79  
80 This was set with explicit store, storage and sid generator.
81  
82 Usage:
83  
84 if ( $this->was_set() ){
85 ...
86 }
87  
88 =cut
89  
90 80 ▶sub was_set{
Subroutine was_set: called CC=2 SCAR=7.1
Pod documented
91 80 my ($self) = @_;
92 80 ▶ return $self->storage() && $self->state();
Condition66%2/3
$self->storage && $self->state
!ll&&!rl&&r
18062
MC/DC50%1/2
$self->storage && $self->state
$self->storage $self->state
Truth table66%2/3
A && B
A= $self->storageB= $self->state
ABresult
0X0
100
111
93 }
94  
95 =head2 cookie_domain
96  
97 From L<Mojolicious::Sessions>. Delegate to the underlying Cookie
98 based state. Use this only if you know the state object supports cookies.
99  
100 =cut
101  
102 2 ▶sub cookie_domain{
Subroutine cookie_domain: called CC=2 SCAR=7.6
Pod documented
103 2 my ($self, @rest ) = @_;
104 2 ▶ unless( $self->was_set() ){ return $self->SUPER::cookie_domain( @rest ); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->was_set) 2 0 2
105 0 return $self->state->cookie_domain( @rest );
106 }
107  
108  
109 =head2 cookie_name
110  
111 From L<Mojolicious::Sessions>. Delegate to the underlying Cookie
112 based state. Use this only if you know the state object supports cookies.
113  
114 =cut
115  
116 8 ▶sub cookie_name{
Subroutine cookie_name: called CC=2 SCAR=6.9
Pod documented
117 8 my ($self, @rest ) = @_;
118 8 ▶ unless( $self->was_set() ){ return $self->SUPER::cookie_name( @rest ); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->was_set) 6 2 8
119 2 return $self->state->cookie_name( @rest );
120 }
121  
122 =head2 cookie_path
123  
124 From L<Mojolicious::Sessions>. Delegate to the underlying Cookie
125 based state. Use this only if you know the state object supports cookies.
126  
127 =cut
128  
129 4 ▶sub cookie_path{
Subroutine cookie_path: called CC=2 SCAR=6.9
Pod documented
130 4 my ($self, @rest ) = @_;
131 4 ▶ unless( $self->was_set() ){ return $self->SUPER::cookie_path( @rest ); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->was_set) 2 2 4
132 2 return $self->state->cookie_path( @rest );
133 }
134  
135 =head2 secure
136  
137 From L<Mojolicious::Sessions>. Delegate to the underlying Cookie
138 based state. Use this only if you know the state object supports cookies.
139  
140 =cut
141  
142 2 ▶sub secure{
Subroutine secure: called CC=2 SCAR=7.6
Pod documented
143 2 my ($self, @rest ) = @_;
144 2 ▶ unless( $self->was_set() ){ return $self->SUPER::secure( @rest ); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->was_set) 2 0 2
145 0 return $self->state->secure( @rest );
146 }
147  
148 =head2 load
149  
150 Implements load from L<Mojolicious::Sessions>
151  
152 =cut
153  
154 39 ▶sub load{
Subroutine load: called CC=11 SCAR=27.2
Pod documented
155 39 my ($self, $controller) = @_;
156  
157 39 ▶ unless( $self->was_set() ){ return $self->SUPER::load( $controller ); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->was_set) 3 36 39
158  
159 # Stuff was set, we need to use it.
160 36 my $session_id = $self->state()->get_session_id( $controller );
161 36 ▶ unless( $session_id ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 26 10 36
162 26 return;
163 }
164  
165 10 my $session = $self->storage->get_session( $session_id );
166 10 ▶ unless( $session ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 0 10 10
167 0 return;
168 }
169  
170 # We just want to set the session in the stash, as required
171 # by Mojolicious::Controller::session
172  
173 # Expiration management.
174 # This is the 'Policy' setting.
175 10 ▶ my $expiration = defined( $session->{expiration} ) ? $session->{expiration} : $self->default_expiration();
Branch: 1 branch
BranchTrueFalseTotal
defined $session->{'expiration'} ? : 0 10 10
176  
177 # This is the actual date at which the session should expire.
178 10 my $expires = delete $session->{expires};
179  
180 10 ▶ if( $expiration &&
Branch: 1 branch
BranchTrueFalseTotal
if ($expiration and not $expires) 0 10 10
Condition33%1/3
$expiration and not $expires
!ll&&!rl&&r
0100
MC/DC0%0/2
$expiration and not $expires
$expiration not $expires
Truth table33%1/3
A and not(B)
A= $expirationB= not $expires
ABresult
0X0
100
111
181 ! $expires ){
182 # No expiry time, but there should be one. Delete the session_id and return
183 0 $self->storage()->remove_session_id( $session_id );
184 0 return;
185 }
186 10 ▶ if( defined $expires && $expires <= time() ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires and $expires <= time) 0 10 10
Condition33%1/3
defined $expires and $expires <= time
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $expires and $expires <= time
defined $expires $expires <= time
Truth table33%1/3
A and B
A= defined $expiresB= $expires <= time
ABresult
0X0
100
111
187 # Session as expired.
188 0 $self->storage()->remove_session_id( $session_id );
189 0 return;
190 }
191  
192 # If the session is empty, we dont want it to be marked active.
193 10 ▶ return unless $controller->stash()->{'mojo.active_session'} = scalar( keys %$session );
Branch: 1 branch
BranchTrueFalseTotal
unless $controller->stash->{"mojo.active_session"} = scalar keys %$session 0 10 10
194 # Note that mojo.active_session acts both way. As a number, it indicates that
195 # the stored session was not empty at some point. As a key that just 'exists',
196 # it prevents subsequent loading of the session in the second call
197 # to $c->session();
198  
199 # This is the sessing of the session hash in the stash
200 10 $controller->stash()->{'mojo.session'} = $session;
201 # And we transfer the flash if anything has flashed something in some previous requests
202 # under the key 'new_flash'. See Mojolicious::Controller::flash
203 10 ▶ $session->{flash} = delete $session->{new_flash} if $session->{new_flash};
Branch: 1 branch
BranchTrueFalseTotal
if $session->{"new_flash"} 4 6 10
204 10 return;
205 }
206  
207 =head2 store
208  
209 Implements store from L<Mojolicious::Sessions>
210  
211 =cut
212  
213 25 ▶sub store{
Subroutine store: called CC=13 SCAR=27.5
Pod documented
214 25 my ($self, $controller) = @_;
215  
216  
217 25 ▶ unless( $self->was_set() ){ return $self->SUPER::store( $controller ); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->was_set) 3 22 25
218  
219 # Stuff was set, we need to use it.
220 # Grab the session from the stash and see if we should really save it.
221 22 my $stash = $controller->stash();
222 22 my $session = $stash->{'mojo.session'};
223  
224 # No session, no storing needed.
225 0 ▶ unless( $session ){ return ; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 0 22 22
226  
227 22 ▶ unless( keys %$session || $stash->{'mojo.active_session'} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$session or $stash->{"mojo.active_session"}) 8 14 22
Condition66%2/3
keys %$session or $stash->{"mojo.active_session"}
l!l&&r!l&&!r
1408
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
228 # The session has never contained anything for the whole duration of this
229 # request. No need to store
230 8 return;
231 }
232  
233 14 my $old_flash = delete $session->{flash};
234  
235 14 ▶ if( $stash->{'mojo.static'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{"mojo.static"}) 0 14 14
236 # Mojo is serving a static resource (like a file).
237 # This is marked with mojo.static being set on the stash.
238 # Behave as if a new_flash was set against the session
239 0 $session->{new_flash} = \%{ $old_flash };
240 }
241  
242 # Clear the new_flash if it contains nothing
243 14 ▶ unless( keys %{ $session->{new_flash} || {} } ){
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$session->{"new_flash"} or {};}) 10 4 14
Condition100%2/2
$session->{'new_flash'} || {}
l!l
410
MC/DC100%1/1
$session->{'new_flash'} || {}
$session->{'new_flash'}
Truth table100%2/2
A
A= $session->{'new_flash'}
Aresult
11
00
244 10 delete $session->{new_flash};
245 }
246  
247 14 my $session_id = $self->get_session_id( $session , $controller );
248  
249 14 ▶ if( defined( $session->{'mojox.sessions3s.old_id'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session->{"mojox.sessions3s.old_id"}) 0 14 14
250 # Session id has changed. Clear the old one.
251 0 $self->storage->remove_session_id( $session->{'mojox.session3s.old_id'} );
252 }
253  
254  
255 14 ▶ my $expiration = defined( $session->{expiration} ) ? $session->{expiration} : $self->default_expiration();
Branch: 1 branch
BranchTrueFalseTotal
defined $session->{'expiration'} ? : 0 14 14
256 14 my $set_expires = delete $session->{expires};
257  
258 14 ▶ if( $expiration || $set_expires ){
Branch: 1 branch
BranchTrueFalseTotal
if ($expiration or $set_expires) 14 0 14
Condition33%1/3
$expiration or $set_expires
l!l&&r!l&&!r
1400
MC/DC0%0/2
$expiration or $set_expires
$expiration $set_expires
Truth table33%1/3
A or B
A= $expirationB= $set_expires
ABresult
1X1
011
000
259 # There is an expiration policy or expires was set explicitely.
260 14 ▶ $session->{expires} = $set_expires || time + $expiration ;
Condition66%2/3
$set_expires || time + $expiration
l!l&&r!l&&!r
2120
MC/DC0%0/2
$set_expires || time + $expiration
$set_expires time + $expiration
Truth table66%2/3
A || B
A= $set_expiresB= time + $expiration
ABresult
1X1
011
000
261 }
262  
263 # Do the standard thing. Store the session.
264 14 $self->storage->store_session( $session_id , $session );
265 # And then inject the session id as a client state.
266 14 $self->state->set_session_id( $controller , $session_id , { expires => $session->{expires} } );
267 }
268  
269 =head2 get_session_id
270  
271 Get the current session ID or generate a fresh one and store it in the given session object,
272 using the given controller.
273  
274 Usage:
275  
276 my $session_id = $this->session_id( $session, $controller );
277  
278 =cut
279  
280 18 ▶sub get_session_id{
Subroutine get_session_id: called CC=2 SCAR=7.1
Pod documented
281 18 my ($self, $session, $controller ) = @_;
282 18 ▶ return $session->{'mojox.sessions3s.id'} ||= $self->sidgen()->generate_sid( $controller );
Condition66%2/3
$session->{"mojox.sessions3s.id"} ||= $self->sidgen->generate_sid($controller)
l!l&&r!l&&!r
1440
MC/DC0%0/2
$session->{"mojox.sessions3s.id"} ||= $self->sidgen->generate_sid($controller)
$session->{"mojox.sessions3s.id"} $self->sidgen->generate_sid($controller)
Truth table66%2/3
A ||= B
A= $session->{"mojox.sessions3s.id"}B= $self->sidgen->generate_sid($controller)
ABresult
1X1
011
000
283 }
284  
285  
286 3 1;
287  
↑ Summary Plugin/Sessions3S.pm »