Auth/Utils.pm

filter: Statementstmt 37.0% 20 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.7% 5 / 14 Podpod n/a totaltotal 29.0% 25 / 86 CC 8 SCAR 20.9
CC
8
Cov
29%
_validate
20.0
_decode
12.0
_split
12.0
CRAP 8.0
« Auth.pm ↑ Summary Auth/Offer.pm »
1 # Copyright 2008 Andrew M. Jenkinson, all rights reserved
2 # Use and/or distribution is permitted only with prior consent
3 package WWW::Live::Auth::Utils;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Crypt::Rijndael;
11 1 require Digest::SHA;
12 1 require MIME::Base64;
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use vars qw(@EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 @EXPORT = @EXPORT_OK = qw(_unescape _escape _decode _encode _decrypt _validate _sign _timestamp _split);
17  
18 0 ▶sub _unescape {
Subroutine _unescape: not called CC=1 SCAR=6.9
19 0 return URI::Escape::uri_unescape( shift );
20 }
21  
22 0 ▶sub _escape {
Subroutine _escape: not called CC=1 SCAR=6.9
23 0 return URI::Escape::uri_escape( shift );
24 }
25  
26 0 ▶sub _decode {
Subroutine _decode: not called CC=3 SCAR=24.8
27 0 my $decoded = MIME::Base64::decode_base64( shift );
28 0 ▶ if ( length $decoded <= 16 || (length $decoded) % 16 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $decoded <= 16 or length($decoded) % 16 != 0) 0 0 0
Condition0%0/3
length $decoded <= 16 or length($decoded) % 16 != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
length $decoded <= 16 or length($decoded) % 16 != 0
length $decoded <= 16 length($decoded) % 16 != 0
Truth table0%0/3
A or B
A= length $decoded <= 16B= length($decoded) % 16 != 0
ABresult
1X1
011
000
29 0 croak('Unable to decode token');
30 }
31 0 return $decoded;
32 }
33  
34 0 ▶sub _encode {
Subroutine _encode: not called CC=1 SCAR=6.9
35 0 return MIME::Base64::encode_base64( shift );
36 }
37  
38 0 ▶sub _decrypt {
Subroutine _decrypt: not called CC=1 SCAR=6.9
39 0 my $decoded = shift;
40 0 my $encryption_key = shift;
41 0 my $iv = substr($decoded, 0, 16);
42 0 my $encrypted = substr($decoded, 16);
43 0 my $cipher = Crypt::Rijndael->new( $encryption_key,
44 Crypt::Rijndael::MODE_CBC() );
45 0 $cipher->set_iv($iv);
46 0 return $cipher->decrypt( $encrypted );
47 }
48  
49 0 ▶sub _validate {
Subroutine _validate: not called CC=4 SCAR=30.0
50 0 my $decrypted = shift;
51 0 my $signature_key = shift;
52
53 0 my ( $token, $signature ) = split /&sig=/, $decrypted;
54 0 ▶ if ( !$token || !$signature ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token and $signature) 0 0 0
Condition0%0/3
$token and $signature
!ll&&!rl&&r
000
MC/DC0%0/2
$token and $signature
$token $signature
Truth table0%0/3
A and B
A= $tokenB= $signature
ABresult
0X0
100
111
55 0 croak('Unable to validate decrypted token');
56 }
57 0 $signature = _decode( _unescape( $signature ) );
58
59 0 my $compare_signature = _sign( $token, $signature_key );
60 0 ▶ if ( $signature ne $compare_signature ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature ne $compare_signature) 0 0 0
61 0 croak('Decrypted token does not match signature');
62 }
63
64 0 return $token;
65 }
66  
67 0 ▶sub _sign {
Subroutine _sign: not called CC=1 SCAR=6.9
68 0 my ( $token, $signature_key ) = @_;
69 0 return Digest::SHA::hmac_sha256( $token, $signature_key );
70 }
71  
72 0 ▶sub _timestamp {
Subroutine _timestamp: not called CC=1 SCAR=6.9
73 0 return time();
74 }
75  
76 0 ▶sub _split {
Subroutine _split: not called CC=3 SCAR=24.8
77 0 my ( $s ) = @_;
78 0 my %split = ();
79 0 my @pairs = split /&/, $s;
80 0 for ( @pairs ) {
81 0 my ( $key, $val ) = split /=/, $_;
82 0 $split{$key} = $val;
83 }
84 0 ▶ return wantarray ? %split : \%split;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
85 }
86  
87 1 1;
88 __END__
« Auth.pm ↑ Summary Auth/Offer.pm »