lib/Dancer2/Core/Role/SessionFactory.pm

filter: Statementstmt 98.2% 114 / 116 Branchbran 71.4% 20 / 28 Conditioncond 66.6% 8 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 92.5% 25 / 27 Podpod 83.3% 10 / 12 totaltotal 88.6% 180 / 203 CC 19 SCAR 10.5
CC
19
Cov
91%
cookie
6.3
retrieve
6.1
create
3.0
CRAP 2.9
« lib/Dancer2/ConfigUtils.pm ↑ Summary blib/lib/Dancer2/Plugin.pm »
1 package Dancer2::Core::Role::SessionFactory;
2 # ABSTRACT: Role for session factories
3 143 $Dancer2::Core::Role::SessionFactory::VERSION = '2.3.0';
4 143 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 143 with 'Dancer2::Core::Role::Engine';
6  
7 143 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 143 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 143 ▶use Dancer2::Core::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 143 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 143 ▶use MIME::Base64 'encode_base64url';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 143 ▶use Ref::Util qw< is_ref is_arrayref is_hashref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 20405 ▶sub hook_aliases { +{} }
Subroutine hook_aliases: called CC=1 SCAR=0.0
Pod undocumented
15 47 ▶sub supported_hooks {
Subroutine supported_hooks: called CC=1 SCAR=0.0
Pod undocumented
16 47 qw/
17 engine.session.before_retrieve
18 engine.session.after_retrieve
19  
20 engine.session.before_create
21 engine.session.after_create
22  
23 engine.session.before_change_id
24 engine.session.after_change_id
25  
26 engine.session.before_destroy
27 engine.session.after_destroy
28  
29 engine.session.before_flush
30 engine.session.after_flush
31 /;
32 }
33  
34 0 ▶sub _build_type {
Subroutine _build_type: not called CC=1 SCAR=6.9
35 0 'SessionFactory';
36 } # XXX vs 'Session'? Unused, so I can't tell -- xdg
37  
38 has log_cb => (
39 is => 'ro',
40 isa => CodeRef,
41 0 ▶ default => sub { sub {1} },
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 143 );
43  
44 has cookie_name => (
45 is => 'ro',
46 isa => Str,
47 216 ▶ default => sub {'dancer.session'},
Subroutine __ANON__: called CC=1 SCAR=0.0
48 143 );
49  
50 143 has cookie_domain => (
51 is => 'ro',
52 isa => Str,
53 predicate => 1,
54 );
55  
56 has cookie_path => (
57 is => 'ro',
58 isa => Str,
59 221 ▶ default => sub {"/"},
Subroutine __ANON__: called CC=1 SCAR=0.0
60 143 );
61  
62 143 has cookie_duration => (
63 is => 'ro',
64 isa => Str,
65 predicate => 1,
66 );
67  
68 143 has session_duration => (
69 is => 'ro',
70 isa => Num,
71 predicate => 1,
72 );
73  
74 has is_secure => (
75 is => 'rw',
76 isa => Bool,
77 222 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
78 143 );
79  
80 has is_http_only => (
81 is => 'rw',
82 isa => Bool,
83 221 ▶ default => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
84 143 );
85  
86 has cookie_same_site => (
87 is => 'ro',
88 isa => Str,
89 predicate => 1,
90 2 ▶ coerce => sub { ucfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 143 );
92  
93 10063 ▶sub create {
Subroutine create: called CC=3 SCAR=11.0
Pod documented
94 10063 my ($self) = @_;
95  
96 10063 my %args = ( id => $self->generate_id, );
97  
98 10063 ▶ $args{expires} = $self->cookie_duration
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cookie_duration 2 10061 10063
99 if $self->has_cookie_duration;
100  
101 10063 my $session = Dancer2::Core::Session->new(%args);
102  
103 10063 $self->execute_hook( 'engine.session.before_create', $session );
104  
105 # XXX why do we _flush now? Seems unnecessary -- xdg, 2013-03-03
106 10063 eval { $self->_flush( $session->id, $session->data ) };
107 10063 ▶ croak "Unable to create a new session: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 10063 10063
108 if $@;
109  
110 10063 $self->execute_hook( 'engine.session.after_create', $session );
111 10063 return $session;
112 }
113  
114 # prepend epoch seconds so session ID is roughly monotonic
115 10066 ▶sub generate_id {
Subroutine generate_id: called CC=1 SCAR=0.0
Pod documented
116 10066 my ($self) = @_;
117  
118 10066 return encode_base64url(
119 pack( "Na*", time, Crypt::URandom::urandom(20) )
120 );
121 }
122  
123 66 ▶sub validate_id {
Subroutine validate_id: called CC=3 SCAR=11.0
Pod documented
124 66 my ($self, $id) = @_;
125  
126 66 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if not defined $id or length $id > 4096 2 64 66
Condition100%3/3
not defined $id or length $id > 4096
l!l&&r!l&&!r
1164
MC/DC100%2/2
not defined $id or length $id > 4096
not defined $id length $id > 4096
Truth table100%3/3
not(A) or B
A= not defined $idB= length $id > 4096
ABresult
1X1
011
000
127 if !defined $id || length($id) > 4096;
128  
129 64 return $id =~ m/\A[A-Za-z0-9_\-~]+\z/;
130 }
131  
132 143 requires '_retrieve';
133  
134 61 ▶sub retrieve {
Subroutine retrieve: called CC=6 SCAR=18.0
Pod documented
135 61 my ( $self, %params ) = @_;
136 61 my $id = $params{id};
137  
138 61 $self->execute_hook( 'engine.session.before_retrieve', $id );
139  
140 61 my $data;
141 # validate format of session id before attempt to retrieve
142 61 my $rc = eval {
143 61 ▶ $self->validate_id($id) && ( $data = $self->_retrieve($id) );
Condition100%3/3
$self->validate_id($id) && ($data = $self->_retrieve($id))
!ll&&!rl&&r
2455
MC/DC50%1/2
$self->validate_id($id) && ($data = $self->_retrieve($id))
$self->validate_id($id) ($data = $self->_retrieve($id))
Truth table66%2/3
A && B
A= $self->validate_id($id)B= ($data = $self->_retrieve($id))
ABresult
0X0
100
111
144 };
145 61 ▶ croak "Unable to retrieve session with id '$id'"
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 7 54 61
146 if ! $rc;
147  
148 54 my %args = ( id => $id, );
149  
150 54 ▶ $args{data} = $data
Branch: 1 branch
BranchTrueFalseTotal
if $data and is_hashref($data) 54 0 54
Condition33%1/3
$data and is_hashref($data)
!ll&&!rl&&r
0054
MC/DC0%0/2
$data and is_hashref($data)
$data is_hashref($data)
Truth table33%1/3
A and B
A= $dataB= is_hashref($data)
ABresult
0X0
100
111
151 if $data and is_hashref($data);
152  
153 54 ▶ $args{expires} = $self->cookie_duration
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cookie_duration 1 53 54
154 if $self->has_cookie_duration;
155  
156 54 my $session = Dancer2::Core::Session->new(%args);
157  
158 54 $self->execute_hook( 'engine.session.after_retrieve', $session );
159 54 return $session;
160 }
161  
162 # XXX eventually we could perhaps require '_change_id'?
163  
164 4 ▶sub change_id {
Subroutine change_id: called CC=2 SCAR=6.9
Pod documented
165 4 my ( $self, %params ) = @_;
166 4 my $session = $params{session};
167 4 my $old_id = $session->id;
168  
169 4 $self->execute_hook( 'engine.session.before_change_id', $old_id );
170  
171 4 my $new_id = $self->generate_id;
172 4 $session->id( $new_id );
173  
174 4 eval { $self->_change_id( $old_id, $new_id ) };
175 4 ▶ croak "Unable to change session id for session with id $old_id: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
176 if $@;
177  
178 4 $self->execute_hook( 'engine.session.after_change_id', $new_id );
179 }
180  
181 143 requires '_destroy';
182  
183 18 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=7.0
Pod documented
184 18 my ( $self, %params ) = @_;
185 18 my $id = $params{id};
186 18 $self->execute_hook( 'engine.session.before_destroy', $id );
187  
188 18 eval { $self->_destroy($id) };
189 18 ▶ croak "Unable to destroy session with id '$id': $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 18 18
190 if $@;
191  
192 18 $self->execute_hook( 'engine.session.after_destroy', $id );
193 18 return $id;
194 }
195  
196 143 requires '_flush';
197  
198 60 ▶sub flush {
Subroutine flush: called CC=2 SCAR=7.0
Pod documented
199 60 my ( $self, %params ) = @_;
200 60 my $session = $params{session};
201 60 $self->execute_hook( 'engine.session.before_flush', $session );
202  
203 60 eval { $self->_flush( $session->id, $session->data ) };
204 60 ▶ croak "Unable to flush session: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 60 60
205 if $@;
206  
207 60 $self->execute_hook( 'engine.session.after_flush', $session );
208 60 return $session->id;
209 }
210  
211 108 ▶sub set_cookie_header {
Subroutine set_cookie_header: called CC=1 SCAR=0.0
Pod documented
212 108 my ( $self, %params ) = @_;
213 $params{response}->push_header(
214 'Set-Cookie',
215 108 $self->cookie( session => $params{session} )->to_header
216 );
217 }
218  
219 108 ▶sub cookie {
Subroutine cookie: called CC=6 SCAR=18.4
Pod documented
220 108 my ( $self, %params ) = @_;
221 108 my $session = $params{session};
222 108 ▶ croak "cookie() requires a valid 'session' parameter"
Branch: 1 branch
BranchTrueFalseTotal
unless is_ref($session) and $session->isa("Dancer2::Core::Session") 0 108 108
Condition33%1/3
is_ref($session) and $session->isa("Dancer2::Core::Session")
!ll&&!rl&&r
00108
MC/DC0%0/2
is_ref($session) and $session->isa("Dancer2::Core::Session")
is_ref($session) $session->isa("Dancer2::Core::Session")
Truth table33%1/3
A and B
A= is_ref($session)B= $session->isa("Dancer2::Core::Session")
ABresult
0X0
100
111
223 unless is_ref($session) && $session->isa("Dancer2::Core::Session");
224  
225 108 my %cookie = (
226 value => $session->id,
227 name => $self->cookie_name,
228 path => $self->cookie_path,
229 secure => $self->is_secure,
230 http_only => $self->is_http_only,
231 );
232  
233 108 ▶ $cookie{same_site} = $self->cookie_same_site
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cookie_same_site 3 105 108
234 if $self->has_cookie_same_site;
235  
236 108 ▶ $cookie{domain} = $self->cookie_domain
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cookie_domain 0 108 108
237 if $self->has_cookie_domain;
238  
239 108 ▶ if ( my $expires = $session->expires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = $session->expires) 16 92 108
240 16 $cookie{expires} = $expires;
241 }
242  
243 108 return Dancer2::Core::Cookie->new(%cookie);
244 }
245  
246 143 requires '_sessions';
247  
248 3 ▶sub sessions {
Subroutine sessions: called CC=2 SCAR=7.0
Pod documented
249 3 my ($self) = @_;
250 3 my $sessions = $self->_sessions;
251  
252 3 ▶ croak "_sessions() should return an array ref"
Branch: 1 branch
BranchTrueFalseTotal
unless is_arrayref($sessions) 0 3 3
253 unless is_arrayref($sessions);
254  
255 3 return $sessions;
256 }
257  
258 143 1;
259  
260 __END__
« lib/Dancer2/ConfigUtils.pm ↑ Summary blib/lib/Dancer2/Plugin.pm »