blib/lib/OpenSocialX/Shindig/Crypter.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 55.0% 11 / 20 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 81.8% 104 / 127 CC 15 SCAR 15.9
CC
15
Cov
84%
new
10.7
checkTimestamp
3.4
create_token
3.2
CRAP 4.9
↑ Summary
1 package OpenSocialX::Shindig::Crypter;
2 2 our $VERSION = '0.03';
3  
4 # ABSTRACT: OpenSocial Shindig Crypter
5  
6 2 ▶use URI::Escape qw/uri_escape uri_unescape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Base64 qw/decode_base64 encode_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Key used for time stamp (in seconds) of data
12 2 my $TIMESTAMP_KEY = 't';
13  
14 # allow three minutes for clock skew
15 2 my $CLOCK_SKEW_ALLOWANCE = 180;
16  
17 1 ▶sub new {
Subroutine new: called CC=8 SCAR=23.7
Pod documented
18 1 my $class = shift;
19  
20 1 ▶ my $cfg = defined $_[0] && ref( $_[0] ) eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 1 0 1
Condition33%1/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
21  
22 # validate
23 1 ▶ $cfg->{cipher} or die 'cipher key is required';
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{"cipher"} or die "cipher key is required" 1 0 1
24 1 ▶ $cfg->{hmac} or die 'hmac key is required';
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{"hmac"} or die "hmac key is required" 1 0 1
25 1 ▶ $cfg->{iv} or die 'iv key is required';
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{"iv"} or die "iv key is required" 1 0 1
26  
27 1 ▶ ( length( $cfg->{cipher} ) == 16 ) or die 'cipher key must be 16 chars';
Branch: 1 branch
BranchTrueFalseTotal
length $cfg->{"cipher"} == 16 or die "cipher key must be 16 chars" 1 0 1
28 1 ▶ ( length( $cfg->{iv} ) == 16 ) or die 'iv key must be 16 chars';
Branch: 1 branch
BranchTrueFalseTotal
length $cfg->{"iv"} == 16 or die "iv key must be 16 chars" 1 0 1
29  
30 1 return bless $cfg, $class;
31 }
32  
33 2 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
34 2 my ( $self, $in ) = @_;
35  
36 2 my $encoded = _serializeAndTimestamp($in);
37 my $cipher = Crypt::CBC->new(
38 {
39 'key' => $self->{cipher},
40 'cipher' => 'Rijndael',
41 'iv' => $self->{iv},
42 2 'literal_key' => 1,
43 'padding' => 'null',
44 'header' => 'none',
45 keysize => 128 / 8,
46 }
47 );
48 2 my $cipherText = $cipher->encrypt($encoded);
49 2 my $hmac = Digest::SHA::hmac_sha1( $cipherText, $self->{hmac} );
50 2 my $b64 = encode_base64( $cipherText . $hmac );
51 2 return $b64;
52 }
53  
54 2 ▶sub _serializeAndTimestamp {
Subroutine _serializeAndTimestamp: called CC=2 SCAR=6.9
55 2 my ($in) = @_;
56  
57 2 my $encoded;
58 2 foreach my $key ( keys %$in ) {
59 9 $encoded .= uri_escape($key) . "=" . uri_escape( $in->{$key} ) . "&";
60 }
61 2 $encoded .= $TIMESTAMP_KEY . "=" . time();
62 2 return $encoded;
63 }
64  
65 2 ▶sub unwrap {
Subroutine unwrap: called CC=2 SCAR=7.0
Pod documented
66 2 my ( $self, $in, $max_age ) = @_;
67  
68 2 my $bin = decode_base64($in);
69 2 my $cipherText = substr( $bin, 0, -20 );
70 2 my $hmac = substr( $bin, length($cipherText) );
71  
72 # verify
73 2 my $v_hmac = Digest::SHA::hmac_sha1( $cipherText, $self->{hmac} );
74 2 ▶ if ( $v_hmac ne $hmac ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v_hmac ne $hmac) 0 2 2
75 0 die 'HMAC verification failure';
76 }
77 my $cipher = Crypt::CBC->new(
78 {
79 'key' => $self->{cipher},
80 'cipher' => 'Rijndael',
81 'iv' => $self->{iv},
82 2 'literal_key' => 1,
83 'padding' => 'null',
84 'header' => 'none',
85 keysize => 128 / 8,
86 }
87 );
88 2 my $plain = $cipher->decrypt($cipherText);
89 2 my $out = $self->deserialize($plain);
90  
91 2 $self->checkTimestamp( $out, $max_age );
92  
93 2 return $out;
94 }
95  
96 2 ▶sub deserialize {
Subroutine deserialize: called CC=2 SCAR=6.9
Pod documented
97 2 my ( $self, $plain ) = @_;
98  
99 2 my $h;
100 2 my @items = split( /[\&\=]/, $plain );
101 2 my $i;
102 2 ▶ for ( $i = 0 ; $i < scalar(@items) ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @items) 11 2 13
103 11 my $key = uri_unescape( $items[ $i++ ] );
104 11 my $value = uri_unescape( $items[ $i++ ] );
105 11 $h->{$key} = $value;
106 }
107 2 return $h;
108 }
109  
110 2 ▶sub checkTimestamp {
Subroutine checkTimestamp: called CC=3 SCAR=12.3
Pod documented
111 2 my ( $self, $out, $max_age ) = @_;
112  
113 2 my $minTime = $out->{$TIMESTAMP_KEY} - $CLOCK_SKEW_ALLOWANCE;
114 2 my $maxTime = $out->{$TIMESTAMP_KEY} + $max_age + $CLOCK_SKEW_ALLOWANCE;
115 2 my $now = time();
116 2 ▶ if ( !( $minTime < $now && $now < $maxTime ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $minTime < $now && $now < $maxTime) 0 2 2
Condition33%1/3
$minTime < $now && $now < $maxTime
!ll&&!rl&&r
002
MC/DC0%0/2
$minTime < $now && $now < $maxTime
$minTime < $now $now < $maxTime
Truth table33%1/3
A && B
A= $minTime < $nowB= $now < $maxTime
ABresult
0X0
100
111
117 0 die "Security token expired";
118 }
119 }
120  
121 2 my $OWNER_KEY = "o";
122 2 my $APP_KEY = "a";
123 2 my $VIEWER_KEY = "v";
124 2 my $DOMAIN_KEY = "d";
125 2 my $APPURL_KEY = "u";
126 2 my $MODULE_KEY = "m";
127  
128 1 ▶sub create_token {
Subroutine create_token: called CC=3 SCAR=11.8
Pod documented
129 1 my $self = shift;
130  
131 1 ▶ my $data = defined $_[0] && ref( $_[0] ) eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && ref $_[0] eq 'HASH' ? : 1 0 1
Condition33%1/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
132 my $token_data = {
133 $OWNER_KEY => $data->{owner},
134 $APP_KEY => $data->{app},
135 $VIEWER_KEY => $data->{viewer},
136 $DOMAIN_KEY => $data->{domain},
137 $APPURL_KEY => $data->{app_url},
138 $MODULE_KEY => $data->{module_id},
139 1 };
140 1 my $token = $self->wrap($token_data);
141 1 return uri_escape($token);
142 }
143  
144 2 1;
145 __END__
↑ Summary