blib/lib/Lemonldap/NG/Common/Session.pm

filter: Statementstmt 88.2% 150 / 170 Branchbran 73.6% 53 / 72 Conditioncond 71.7% 38 / 53 MC/DCmcdc 53.1% 17 / 32 Subroutinesub 90.9% 20 / 22 Podpod 16.6% 1 / 6 totaltotal 78.5% 279 / 355 CC 58 SCAR 26.8
CC
58
Cov
82%
BUILD
28.3
update
14.7
_tie_session
13.6
CRAP 14.6
« blib/lib/Lemonldap/NG/Common/Notifications/DBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/SAML/Metadata.pm »
1 ##@file
2 # Base package for LemonLDAP::NG session object
3  
4 ##@class
5 # Specify a session object, how to create/update/remove session
6  
7 package Lemonldap::NG::Common::Session;
8  
9 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Lemonldap::NG::Common::Apache::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Lemonldap::NG::Common::Apache::Session::Generate::SHA256;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 our $VERSION = '2.20.0';
17  
18 # Export method needed to handle hashed storage
19 7 our @EXPORT = qw(id2storage hashedKinds reHashedKinds);
20  
21 7 ▶use constant hashedKinds => ( 'SSO', 'OIDC', 'CDA' );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 0 ▶sub reHashedKinds {
Subroutine reHashedKinds: not called CC=1 SCAR=6.9
Pod undocumented
24 0 my $s = '^(' . join( '|', hashedKinds() ) . ')$';
25 0 return qr/$s/;
26 }
27  
28 # Workaround for another ModPerl/Mouse issue...
29 7 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=15.8
30 7 require Mouse;
31 7 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 ▶ my $v =
Branch: 1 branch
BranchTrueFalseTotal
$Mouse::VERSION ? : 7 0 7
33 $Mouse::VERSION
34 ? sprintf( "%d.%03d%03d", ( $Mouse::VERSION =~ /(\d+)/g ) )
35 : 0;
36 7 ▶ if ( $v < 2.005001 and $Lemonldap::NG::Handler::Apache2::Main::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v < 2.005001 and $Lemonldap::NG::Handler::Apache2::Main::VERSION) { } 0 7 7
Condition33%1/3
$v < "2.005001" and $Lemonldap::NG::Handler::Apache2::Main::VERSION
!ll&&!rl&&r
700
MC/DC0%0/2
$v < "2.005001" and $Lemonldap::NG::Handler::Apache2::Main::VERSION
$v < "2.005001" $Lemonldap::NG::Handler::Apache2::Main::VERSION
Truth table33%1/3
A and B
A= $v < "2.005001"B= $Lemonldap::NG::Handler::Apache2::Main::VERSION
ABresult
0X0
100
111
37 0 require Moose;
38 0 Moose->import();
39 }
40 else {
41 7 Mouse->import();
42 }
43 }
44  
45 # Convert a session ID into store entry
46 27 ▶sub id2storage {
Subroutine id2storage: called CC=2 SCAR=7.6
Pod undocumented
47 27 ▶ return $_[0] ? Digest::SHA::sha256_hex( $_[0] ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 27 0 27
48 }
49  
50 1 ▶sub randomId {
Subroutine randomId: called CC=1 SCAR=0.0
Pod undocumented
51 1 my $tmp = {};
52 1 &Lemonldap::NG::Common::Apache::Session::Generate::SHA256::generate($tmp);
53 1 return $tmp->{data}->{_session_id};
54 }
55  
56 has id => (
57 is => 'rw',
58 isa => 'Str|Undef',
59 256 ▶ trigger => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
60 $_[0]->{storageId} =
61 256 ▶ ( $_[0]->hashStore && $_[0]->id )
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->hashStore && $_[0]->id ? : 11 245 256
Condition100%3/3
$_[0]->hashStore and $_[0]->id
!ll&&!rl&&r
243211
MC/DC100%2/2
$_[0]->hashStore and $_[0]->id
$_[0]->hashStore $_[0]->id
Truth table100%3/3
A and B
A= $_[0]->hashStoreB= $_[0]->id
ABresult
0X0
100
111
62 ? id2storage( $_[0]->id )
63 : $_[0]->id;
64 }
65 7 );
66  
67 7 has storageId => ( is => 'rw', );
68  
69 7 has force => (
70 is => 'rw',
71 isa => 'Bool',
72 default => 0,
73 );
74  
75 7 has kind => (
76 is => 'rw',
77 isa => 'Str|Undef',
78 );
79  
80 has data => (
81 is => 'rw',
82 isa => 'HashRef',
83 141 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 7 );
85  
86 7 has options => (
87 is => 'rw',
88 isa => 'HashRef',
89 );
90  
91 7 has storageModule => (
92 is => 'ro',
93 isa => 'Str',
94 required => 1,
95 );
96  
97 7 has storageModuleOptions => (
98 is => 'ro',
99 isa => 'HashRef|Undef',
100 );
101  
102 7 has cacheModule => (
103 is => 'rw',
104 isa => 'Str|Undef',
105 );
106  
107 7 has cacheModuleOptions => (
108 is => 'rw',
109 isa => 'HashRef|Undef',
110 );
111  
112 7 has error => (
113 is => 'rw',
114 isa => 'Str|Undef',
115 );
116  
117 7 has info => ( is => 'rw' );
118  
119 7 has timeout => ( is => 'rw', default => 5 );
120  
121 7 has hashStore => ( is => 'rw' );
122  
123 141 ▶sub BUILD {
Subroutine BUILD: called CC=27 SCAR=33.4
Pod documented
124 141 my ($self) = @_;
125  
126 # Load Apache::Session module
127 141 ▶ unless ( $self->storageModule->can('populate') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->storageModule->can("populate")) 5 136 141
128 5 eval "require " . $self->storageModule;
129 5 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
130 }
131  
132 # Register options for common Apache::Session module
133 141 ▶ my $moduleOptions = $self->storageModuleOptions || {};
Condition50%1/2
$self->storageModuleOptions || {}
l!l
1410
MC/DC0%0/1
$self->storageModuleOptions || {}
$self->storageModuleOptions
Truth table50%1/2
A
A= $self->storageModuleOptions
Aresult
11
00
134 $self->timeout( delete $moduleOptions->{timeout} )
135 141 ▶ if $moduleOptions->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $moduleOptions->{"timeout"} 0 141 141
136 141 my %options = (
137 %$moduleOptions,
138 backend => $self->storageModule,
139 localStorage => $self->cacheModule,
140 localStorageOptions => $self->cacheModuleOptions
141 );
142  
143 141 $self->options( \%options );
144  
145 141 my $data = $self->_tie_session;
146  
147 # Is it a session creation request?
148 141 ▶ my $creation = 1
Branch: 1 branch
BranchTrueFalseTotal
if not $self->id or $self->id and not $data and $self->force 57 84 141
Condition88%8/9
not $self->id or $self->id and not $data and $self->force
l!l&&r!l&&!r
451284
$self->id and not $data and $self->force
!ll&&!rl&&r
82212
$self->id and not $data
!ll&&!rl&&r
08214
MC/DC75%3/4
not $self->id or $self->id and not $data and $self->force
not $self->id $self->id not $data $self->force
Truth table80%4/5
not(A) or B and not(C) and D
A= not $self->idB= $self->idC= not $dataD= $self->force
ABCDresult
1XXX1
01111
00XX0
010X0
01100
149 if ( !$self->id or ( $self->id and !$data and $self->force ) );
150  
151 # If session id was submitted but session is not found
152 # And we want to force id
153 # Then use setId to create session
154 141 ▶ if ( $self->id and $creation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id and $creation) 12 129 141
Condition100%3/3
$self->id and $creation
!ll&&!rl&&r
458412
MC/DC100%2/2
$self->id and $creation
$self->id $creation
Truth table100%3/3
A and B
A= $self->idB= $creation
ABresult
0X0
100
111
155 12 $options{setId} = $self->id;
156 12 $self->options( \%options );
157 12 $self->id(undef);
158 12 $self->error(undef);
159 12 $data = $self->_tie_session;
160 }
161  
162 141 ▶ if ( $self->{info} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"info"}) 80 61 141
163 80 foreach ( keys %{ $self->{info} } ) {
164 200 ▶ next if ( $_ eq "_session_id" and $data->{_session_id} );
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_session_id" and $data->{"_session_id"} 7 193 200
Condition66%2/3
$_ eq "_session_id" and $data->{"_session_id"}
!ll&&!rl&&r
19307
MC/DC50%1/2
$_ eq "_session_id" and $data->{"_session_id"}
$_ eq "_session_id" $data->{"_session_id"}
Truth table66%2/3
A and B
A= $_ eq "_session_id"B= $data->{"_session_id"}
ABresult
0X0
100
111
165 193 ▶ next if ( $_ eq "_session_kind" and $data->{_session_kind} );
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_session_kind" and $data->{"_session_kind"} 3 190 193
Condition100%3/3
$_ eq "_session_kind" and $data->{"_session_kind"}
!ll&&!rl&&r
18643
MC/DC100%2/2
$_ eq "_session_kind" and $data->{"_session_kind"}
$_ eq "_session_kind" $data->{"_session_kind"}
Truth table100%3/3
A and B
A= $_ eq "_session_kind"B= $data->{"_session_kind"}
ABresult
0X0
100
111
166 190 ▶ if ( defined $self->{info}->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'info'}{$_}) { } 190 0 190
167 190 $data->{$_} = $self->{info}->{$_};
168 }
169 else {
170 0 delete $data->{$_};
171 }
172 }
173 80 delete $self->{info};
174 }
175  
176 # If session is created
177 # Then set session kind in session
178 141 ▶ if ( $creation and $self->kind ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($creation and $self->kind) 53 88 141
Condition100%3/3
$creation and $self->kind
!ll&&!rl&&r
84453
MC/DC100%2/2
$creation and $self->kind
$creation $self->kind
Truth table100%3/3
A and B
A= $creationB= $self->kind
ABresult
0X0
100
111
179 53 $data->{_session_kind} = $self->kind;
180 }
181  
182 # Load session data into object
183 141 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 139 2 141
184 139 ▶ if ( $self->kind and $data->{_session_kind} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kind and $data->{"_session_kind"}) 78 61 139
Condition66%2/3
$self->kind and $data->{"_session_kind"}
!ll&&!rl&&r
61078
MC/DC50%1/2
$self->kind and $data->{"_session_kind"}
$self->kind $data->{"_session_kind"}
Truth table66%2/3
A and B
A= $self->kindB= $data->{"_session_kind"}
ABresult
0X0
100
111
185 78 ▶ unless ( $data->{_session_kind} eq $self->kind ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"_session_kind"} eq $self->kind) 0 78 78
186 0 $self->error(
187 "Session kind mismatch: $data->{_session_kind} is not "
188 . $self->kind );
189 0 return undef;
190 }
191 }
192 139 $self->_save_data($data);
193 139 $self->kind( $data->{_session_kind} );
194 139 $self->id( $data->{_session_id} );
195 139 ▶ if ( $self->hashStore and $self->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hashStore and $self->id) 6 133 139
Condition66%2/3
$self->hashStore and $self->id
!ll&&!rl&&r
13306
MC/DC50%1/2
$self->hashStore and $self->id
$self->hashStore $self->id
Truth table66%2/3
A and B
A= $self->hashStoreB= $self->id
ABresult
0X0
100
111
196 6 $self->_hashDataSessionId($data);
197 6 ▶ $data->{_session_hashed} ||= 1;
Condition100%2/2
$data->{"_session_hashed"} ||= 1
l!l
42
MC/DC100%1/1
$data->{"_session_hashed"} ||= 1
$data->{"_session_hashed"}
Truth table100%2/2
A
A= $data->{"_session_hashed"}
Aresult
11
00
198 }
199  
200 139 untie(%$data);
201 }
202 }
203  
204 191 ▶sub _tie_session {
Subroutine _tie_session: called CC=13 SCAR=26.1
205 191 my $self = $_[0];
206 191 ▶ my $options = $_[1] || {};
Condition100%2/2
$_[1] || {}
l!l
8183
MC/DC100%1/1
$_[1] || {}
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
207 191 my %h;
208  
209 # Secured storage for new session: generate a new random ID and calculate
210 # the storage ID
211 191 my $securedId = $self->id;
212 191 ▶ if ( $self->hashStore ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hashStore) 7 184 191
213 7 ▶ if ( !$self->id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->id) 2 5 7
214 2 ▶ my $id = $self->options->{setId} || randomId();
Condition66%2/3
$self->options->{'setId'} || randomId()
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->options->{'setId'} || randomId()
$self->options->{'setId'} randomId()
Truth table66%2/3
A || B
A= $self->options->{'setId'}B= randomId()
ABresult
1X1
011
000
215 2 $securedId = $id;
216 2 $self->storageId( id2storage($securedId) );
217 2 $self->options->{setId} = $options->{setId} = $self->storageId;
218 2 $self->error(undef);
219 }
220 }
221  
222 191 eval {
223 0 ▶ local $SIG{ALRM} = sub { die "TIMEOUT\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
224 191 eval {
225 191 alarm $self->timeout;
226  
227 # SOAP/REST session module must be directly tied
228 191 ▶ if ( $self->storageModule =~
Branch: 1 branch
BranchTrueFalseTotal
if ($self->storageModule =~ /^Lemonldap::NG::Common::Apache::Session/) { } 0 191 191
229 /^Lemonldap::NG::Common::Apache::Session/ )
230 {
231 tie %h, $self->storageModule,
232 ( $options->{setId} ? $self->id : $self->storageId ),
233 0 ▶ { %{ $self->options }, %$options, kind => $self->kind };
Branch: 1 branch
BranchTrueFalseTotal
$options->{'setId'} ? : 0 0 0
234 }
235 else {
236 tie %h, 'Lemonldap::NG::Common::Apache::Session',
237 ( $options->{setId} ? $self->id : $self->storageId ),
238 191 ▶ { %{ $self->options }, %$options };
Branch: 1 branch
BranchTrueFalseTotal
$options->{'setId'} ? : 2 189 191
239 }
240 };
241 191 alarm 0;
242 191 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 14 177 191
243  
244 };
245 191 ▶ if ( $@ or not tied(%h) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not tied %h) 14 177 191
Condition66%2/3
$@ or not tied %h
l!l&&r!l&&!r
140177
MC/DC50%1/2
$@ or not tied %h
$@ not tied %h
Truth table66%2/3
A or not(B)
A= $@B= not tied %h
ABresult
1X1
011
000
246 14 my $msg = "Session cannot be tied";
247 14 ▶ $msg .= ": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 14 0 14
248 14 $self->error($msg);
249 14 return undef;
250 }
251 177 ▶ if ( $self->hashStore ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hashStore) 6 171 177
252  
253 # Before returning the session, set here the real cookie value
254 6 my $status = tied(%h)->{status};
255 6 $h{_session_id} = $securedId;
256 6 tied(%h)->{status} = $status;
257 }
258  
259 177 return \%h;
260 }
261  
262 147 ▶sub _save_data {
Subroutine _save_data: called CC=1 SCAR=0.0
263 147 my ( $self, $data ) = @_;
264  
265 147 my %saved_data = %$data;
266 147 $self->data( \%saved_data );
267 }
268  
269 8 ▶sub update {
Subroutine update: called CC=9 SCAR=26.8
Pod undocumented
270 8 my ( $self, $infos, $tieOptions ) = @_;
271  
272 8 ▶ unless ( ref $infos eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $infos eq "HASH") 0 8 8
273 0 $self->error("You need to provide a HASHREF");
274 0 return 0;
275 }
276  
277 8 ▶ my $data = $self->_tie_session(
Branch: 1 branch
BranchTrueFalseTotal
$tieOptions ? : 0 8 8
278 { ( $tieOptions ? %$tieOptions : () ), noCache => 1 } );
279  
280 8 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 8 0 8
281 8 foreach ( keys %$infos ) {
282 45 ▶ if ( defined $infos->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $infos->{$_}) { } 42 3 45
283 42 $data->{$_} = $infos->{$_};
284 }
285 else {
286 3 delete $data->{$_};
287 }
288 }
289  
290 8 $self->_save_data($data);
291 8 $self->id( $data->{_session_id} );
292 8 ▶ if ( $self->hashStore and $self->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hashStore and $self->id) 0 8 8
Condition33%1/3
$self->hashStore and $self->id
!ll&&!rl&&r
800
MC/DC0%0/2
$self->hashStore and $self->id
$self->hashStore $self->id
Truth table33%1/3
A and B
A= $self->hashStoreB= $self->id
ABresult
0X0
100
111
293 0 $self->_hashDataSessionId($data);
294 0 ▶ $data->{_session_hashed} ||= 1;
Condition0%0/2
$data->{"_session_hashed"} ||= 1
l!l
00
MC/DC0%0/1
$data->{"_session_hashed"} ||= 1
$data->{"_session_hashed"}
Truth table0%0/2
A
A= $data->{"_session_hashed"}
Aresult
11
00
295 }
296  
297 8 untie(%$data);
298 8 return 1;
299 }
300  
301 0 $self->error("No data found in session");
302 0 return 0;
303 }
304  
305 30 ▶sub remove {
Subroutine remove: called CC=4 SCAR=16.0
Pod undocumented
306 30 my ( $self, $tieOptions ) = @_;
307  
308 30 my $data = $self->_tie_session($tieOptions);
309 30 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 30 30
310 0 $self->error("Unable to delete session: $@");
311 0 return 0;
312 }
313  
314 # Before saving, hide the real ID and replace it by the storage ID
315 30 ▶ $self->_hashDataSessionId($data) if $self->hashStore;
Branch: 1 branch
BranchTrueFalseTotal
if $self->hashStore 0 30 30
316  
317 30 eval { tied(%$data)->delete(); };
318  
319 30 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 30 30
320 0 $self->error("Unable to delete session: $@");
321 0 return 0;
322 }
323  
324 30 return 1;
325 }
326  
327 6 ▶sub _hashDataSessionId {
Subroutine _hashDataSessionId: called CC=3 SCAR=11.6
328 6 my ( $self, $data, $id ) = @_;
329 6 ▶ my $nid = id2storage( $id || $self->id );
Condition33%1/3
$id || $self->id
l!l&&r!l&&!r
060
MC/DC0%0/2
$id || $self->id
$id $self->id
Truth table33%1/3
A || B
A= $idB= $self->id
ABresult
1X1
011
000
330 6 ▶ if ( $nid ne $data->{_session_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nid ne $data->{"_session_id"}) 6 0 6
331 6 my $status = tied(%$data)->{status};
332 6 $data->{_session_id} = id2storage( $self->id );
333 6 tied(%$data)->{status} = $status;
334 }
335 }
336  
337 7 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
338  
339 7 1;
« blib/lib/Lemonldap/NG/Common/Notifications/DBI.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Conf/SAML/Metadata.pm »