Session/Generate/SHA256.pm

filter: Statementstmt 100% 22 / 22 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 2 totaltotal 94.1% 32 / 34 CC 4 SCAR 7.6
CC
4
Cov
100%
generate
3.0
validate
2.0
BEGIN
1.0
CRAP 2.1
« Session/Store/File.pm ↑ Summary Session/Store/MySQL.pm »
1 package Web::Session::Generate::SHA256;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 our $VERSION = '1.2.0';
7  
8 148 ▶sub generate {
Subroutine generate: called CC=3 SCAR=11.0
Pod undocumented
9 148 my $session = shift;
10 148 my $length = 64;
11  
12 148 ▶ if ( exists $session->{args}->{IDLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $session->{"args"}{"IDLength"}) 2 146 148
13 2 $length = $session->{args}->{IDLength};
14 }
15  
16 # No fallback: an ID derived from time(), rand() and the PID could be
17 # guessed, failing to create the session is safer.
18 my $bytes =
19 148 eval { Crypt::URandom::urandom( int( ( $length + 1 ) / 2 ) ) };
20 148 ▶ unless ( defined $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bytes) 3 145 148
21  
22 # Apache::Session::TIEHASH has already flagged the session as new and
23 # its DESTROY saves new sessions: without this, the session would be
24 # written without ID (lock file "Apache-Session-.lock", SQL row with
25 # a NULL id).
26 3 $session->{status} = 0;
27 3 die "Unable to generate a session ID, no secure random source: $@";
28 }
29 $session->{data}->{_session_id} =
30 145 substr( unpack( 'H*', $bytes ), 0, $length );
31 }
32  
33 90 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod undocumented
34  
35 #This routine checks to ensure that the session ID is in the form
36 #we expect. This must be called before we start diddling around
37 #in the database or the disk.
38  
39 90 my $session = shift;
40  
41 90 ▶ if ( $session->{data}->{_session_id} =~ /^([a-fA-F0-9]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->{'data'}{'_session_id'} =~ /^([a-fA-F0-9]+)$/) { } 86 4 90
42 86 $session->{data}->{_session_id} = $1;
43 }
44 else {
45 4 die "Invalid session ID: " . $session->{data}->{_session_id};
46 }
47 }
48  
49 39 1;
50  
51 __END__
« Session/Store/File.pm ↑ Summary Session/Store/MySQL.pm »