| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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__ | ||||||||||