« blib/lib/Lemonldap/NG/Common/UserAgent.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/Logger/_Duplicate.pm »
| 1 | ############################################################################# | ||||||||||
| 2 | # | ||||||||||
| 3 | # Lemonldap::NG::Common::Apache::Session::Generate::SHA256 | ||||||||||
| 4 | # Generates session identifier tokens using SHA-256 | ||||||||||
| 5 | # Distribute under the Perl License | ||||||||||
| 6 | # | ||||||||||
| 7 | ############################################################################ | ||||||||||
| 8 | |||||||||||
| 9 | package Lemonldap::NG::Common::Apache::Session::Generate::SHA256; | ||||||||||
| 10 | |||||||||||
| 11 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 7 | ▶ | use Crypt::URandom; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 7 | our $VERSION = '2.0.2'; | |||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | sub generate { | ||||||||
|
Subroutine
generate: called CC=3 SCAR=13.1
Pod
undocumented
| |||||||||||
| 17 | 2 | my $session = shift; | |||||||||
| 18 | 2 | my $length = 64; | |||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | if ( exists $session->{args}->{IDLength} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | $length = $session->{args}->{IDLength}; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 2 | eval { | |||||||||
| 25 | $session->{data}->{_session_id} = | ||||||||||
| 26 | 2 | unpack( 'H*', Crypt::URandom::urandom( int( $length / 2 ) ) ); | |||||||||
| 27 | }; | ||||||||||
| 28 | 2 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | print STDERR "Crypt::URandom::urandom failed: $@\n"; | |||||||||
| 30 | 0 | require Digest::SHA; | |||||||||
| 31 | $session->{data}->{_session_id} = | ||||||||||
| 32 | 0 | substr( Digest::SHA::sha256_hex( time() . {} . rand() . $$ ), | |||||||||
| 33 | 0, $length ); | ||||||||||
| 34 | } | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | sub validate { | ||||||||
|
Subroutine
validate: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | #This routine checks to ensure that the session ID is in the form | ||||||||||
| 40 | #we expect. This must be called before we start diddling around | ||||||||||
| 41 | #in the database or the disk. | ||||||||||
| 42 | |||||||||||
| 43 | 1 | my $session = shift; | |||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | if ( $session->{data}->{_session_id} =~ /^([a-fA-F0-9]+)$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | $session->{data}->{_session_id} = $1; | |||||||||
| 47 | } | ||||||||||
| 48 | else { | ||||||||||
| 49 | 0 | die "Invalid session ID: " . $session->{data}->{_session_id}; | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 7 | 1; | |||||||||