lib/Apache/AppSamurai/Session/Generate/HMAC_SHA.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 78.8% 41 / 52 CC 7 SCAR 11.2
CC
7
Cov
81%
generate
5.4
validate
2.3
checkhash
2.1
CRAP 3.1
« lib/Apache/AppSamurai/Session/Serialize/CryptBase64.pm ↑ Summary examples/auth/AuthTest.pm »
1 # Apache::AppSamurai::Session::Generate::HMAC_SHA - Apache::Session generator
2 # module. Replaces MD5 generator with one that
3 # takes input server key and client session key
4 # and returns the SHAx HMAC of the two.
5  
6 # $Id: HMAC_SHA.pm,v 1.9 2008/04/30 21:40:10 pauldoom Exp $
7  
8 ##
9 # Copyright (c) 2008 Paul M. Hirsch (paul@voltagenoir.org).
10 # All rights reserved.
11 #
12 # This program is free software; you can redistribute it and/or modify it under
13 # the same terms as Perl itself.
14 ##
15  
16 package Apache::AppSamurai::Session::Generate::HMAC_SHA;
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 $VERSION = substr(q$Revision: 1.9 $, 10, -1);
22  
23 1 ▶use Digest::SHA qw(sha256_hex hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # Instead of adding even more options, I decided to just use SHA-256.
26 # This is the length in hex digits.
27 1 my $length = 64;
28  
29 1 ▶sub generate {
Subroutine generate: called CC=5 SCAR=16.8
Pod undocumented
30 1 my $session = shift;
31 1 my $server_key = '';
32  
33 1 ▶ (exists $session->{args}->{ServerKey}) or die "HMAC session support requires a ServerKey";
Branch: 1 branch
BranchTrueFalseTotal
exists $session->{"args"}{"ServerKey"} or die "HMAC session support requires a ServerKey" 1 0 1
34  
35 # ServerKey should already be hashed for us
36 1 ▶ (&checkhash($session->{args}->{ServerKey})) or die "Invalid ServerKey";
Branch: 1 branch
BranchTrueFalseTotal
&checkhash($session->{"args"}{"ServerKey"}) or die "Invalid ServerKey" 1 0 1
37
38 1 ▶ (exists $session->{args}->{key}) or die "HMAC session support requires a per-session Authentication Key (key)";
Branch: 1 branch
BranchTrueFalseTotal
exists $session->{"args"}{"key"} or die "HMAC session support requires a per-session Authentication Key (key)" 1 0 1
39 1 ▶ (&checkhash($session->{args}->{key})) or die "Invalid Session Authentication Key";
Branch: 1 branch
BranchTrueFalseTotal
&checkhash($session->{"args"}{"key"}) or die "Invalid Session Authentication Key" 1 0 1
40 1 $session->{data}->{_session_id} = hmac_sha256_hex($session->{args}->{key},$session->{args}->{ServerKey});
41  
42 1 return $session->{data}->{_session_id};
43 }
44  
45 2 ▶sub validate {
Subroutine validate: called CC=2 SCAR=8.1
Pod undocumented
46 #This routine checks to ensure that the session ID is in the form
47 #we expect. This must be called before we start diddling around
48 #in the database or the disk.
49  
50 2 my $session = shift;
51
52 2 ▶ unless (&checkhash($session->{data}->{_session_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&checkhash($session->{"data"}{"_session_id"})) 0 1 1
53 0 die "Invalid Session ID Value";
54 }
55 }
56  
57 # Check for a hex encoded hash of $length
58 3 ▶sub checkhash {
Subroutine checkhash: called CC=2 SCAR=7.6
Pod undocumented
59 3 my $hash = shift;
60  
61 3 ▶ if ($hash =~ /^[a-fA-F0-9]{$length}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash =~ /^[a-fA-F0-9]{$length}$/) 3 0 3
62 3 return 1;
63 }
64 0 return 0;
65 }
66  
67 1 1; # End of Apache::AppSamurai::Session::Generate::HMAC_SHA
68  
69 __END__
« lib/Apache/AppSamurai/Session/Serialize/CryptBase64.pm ↑ Summary examples/auth/AuthTest.pm »