Util.pm

filter: Statementstmt 60.0% 66 / 110 Branchbran 17.3% 8 / 46 Conditioncond 36.3% 4 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 68.1% 15 / 22 Podpod 7.1% 1 / 14 totaltotal 45.2% 95 / 210 CC 31 SCAR 26.9
CC
31
Cov
47%
checkTrustRoot
56.0
__ANON__
30.0
decodeParams
12.0
CRAP 14.8
↑ Summary Stores/FileStore.pm »
1 package Net::OpenID::JanRain::Util;
2  
3 5 $VERSION = "1.1.0";
4  
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use CGI qw(-oldstyle_urls);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 our @ISA = qw(Exporter);
17 5 our @EXPORT_OK = qw(
18 log
19 urlencode
20 decodeParams
21 appendArgs
22 toBase64
23 fromBase64
24 kvToHash
25 pairsToKV
26 hashToPairs
27 hashToKV
28 findAgent
29 normalizeUrl
30 checkTrustRoot
31 );
32  
33 0 ▶sub log { #currently most places just use warn
Subroutine log: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my ($message, $level) = @_;
35 0 ▶ $level = (defined($level) ? $level : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $level ? : 0 0 0
36 0 warn $message;
37 } # end log
38 ########################################################################
39 2 ▶sub urlencode { # wrapper for the non-intuitive functionality of CGI.pm
Subroutine urlencode: called CC=1 SCAR=0.0
Pod undocumented
40 2 my %args = @_;
41 2 return(CGI->new({%args})->query_string);
42 } # end subroutine urlencode definition
43 ########################################################################
44 0 ▶sub decodeParams { # turns a query string into a hash
Subroutine decodeParams: not called CC=3 SCAR=24.8
Pod undocumented
45 0 my ($s) = @_;
46 # okay for full urls
47 0 ▶ my $c = CGI->new( ($s =~ m/^http:|\?/) ? URI->new($s)->query : $s);
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^http:|\?/ ? : 0 0 0
48 0 ▶ $c->param or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$c->param or return undef 0 0 0
49 0 return $c->Vars;
50 } # end subroutine decodeParams definition
51 ########################################################################
52 2 ▶sub appendArgs {
Subroutine appendArgs: called CC=6 SCAR=21.4
Pod undocumented
53 2 my ($url, $args) = @_;
54 2 ▶ carp "appendArgs called with improper args: url($url) args($args)" unless ($url && $args);
Branch: 1 branch
BranchTrueFalseTotal
unless $url and $args 0 2 2
Condition33%1/3
$url and $args
!ll&&!rl&&r
002
MC/DC0%0/2
$url and $args
$url $args
Truth table33%1/3
A and B
A= $urlB= $args
ABresult
0X0
100
111
55 2 ▶ if($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 2 0 2
56 2 ▶ UNIVERSAL::isa($args, 'HASH') or
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($args, "HASH") or croak("second arg to appendArgs must be hash ref") 2 0 2
57 croak "second arg to appendArgs must be hash ref";
58 return(
59 2 ▶ $url . (($url =~ m/\?/) ? '&' : '?') . urlencode(%$args)
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/ ? : 0 2 2
60 );
61 }
62 else {
63 0 return($url);
64 }
65 } # end appendArgs
66 ########################################################################
67 21 ▶sub toBase64 {
Subroutine toBase64: called CC=1 SCAR=0.0
Pod undocumented
68 21 my ($s) = @_;
69 21 my $r = encode_base64($s);
70 21 $r =~ s/\n//g;
71 21 return $r;
72 } # end toBase64
73 ########################################################################
74 8 ▶sub fromBase64 {
Subroutine fromBase64: called CC=2 SCAR=7.1
Pod undocumented
75 8 my ($s) = @_;
76 8 ▶ carp "fromBase64 called on nothing" unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 8 8
77 8 return(MIME::Base64::decode_base64($s));
78 } # end fromBase64
79 ########################################################################
80 7 ▶sub kvToHash { # parses k:v\n responses
Subroutine kvToHash: called CC=2 SCAR=6.9
Pod undocumented
81 7 my ($s) = @_;
82 7 $s =~ s/^\s+|\s+$//g;
83 7 my %form;
84 7 foreach my $line (split(/\s*\n\s*/, $s)) {
85 42 my ($k, $v) = split(/\s*:\s*/, $line, 2);
86 42 $form{$k} = $v;
87 }
88 7 return(\%form);
89 }
90 ########################################################################
91 6 ▶sub pairsToKV { # Put a list of pairs into KVform
Subroutine pairsToKV: called CC=4 SCAR=15.5
Pod undocumented
92 6 my ($pairs) = @_;
93
94 6 my $kv = "";
95 6 my $pair;
96 6 foreach $pair (@$pairs) {
97 14 ▶ unless (defined($pair->[0]) and defined($pair->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pair->[0] and defined $pair->[1]) 0 14 14
Condition33%1/3
defined $pair->[0] and defined $pair->[1]
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $pair->[0] and defined $pair->[1]
defined $pair->[0] defined $pair->[1]
Truth table33%1/3
A and B
A= defined $pair->[0]B= defined $pair->[1]
ABresult
0X0
100
111
98 0 warn "pairstoKV not passed pairs";
99 0 next;
100 }
101 14 $kv = "${kv}$pair->[0]:$pair->[1]\n";
102 }
103 6 return $kv;
104 }
105 ########################################################################
106 # Take a hash ref and a list of keys (ref) and return a list of pairs (ref)
107 # the third argument is a prefix to prepend to the keys when doing
108 # hash lookup.
109 10 ▶sub hashToPairs {
Subroutine hashToPairs: called CC=4 SCAR=13.9
Pod undocumented
110 10 my $hash = shift;
111 10 my $keys = shift;
112 10 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
82
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
113
114 10 my @pairs = ();
115 10 foreach my $key (@$keys) {
116 25 my $realkey = $prefix . $key;
117 25 my @pair = ($key, $hash->{$realkey});
118 25 ▶ return undef unless $hash->{$realkey};
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{$realkey} 1 24 25
119 24 push @pairs, \@pair;
120 }
121 9 return \@pairs;
122 }
123 ########################################################################
124 1 ▶sub hashToKV {
Subroutine hashToKV: called CC=1 SCAR=0.0
Pod undocumented
125 1 my ($hashref) = @_;
126 1 return pairsToKV(hashToPairs($hashref, [keys(%$hashref)], ''));
127 }
128 ########################################################################
129 5 our $AGENT; # Save what we found for later
130 0 ▶sub setAgent { # Mainly for testing
Subroutine setAgent: not called CC=1 SCAR=6.9
Pod undocumented
131 0 $AGENT = shift;
132 }
133 0 ▶sub findAgent {
Subroutine findAgent: not called CC=3 SCAR=24.8
Pod undocumented
134 0 ▶ $AGENT and return($AGENT);
Branch: 1 branch
BranchTrueFalseTotal
$Net::OpenID::JanRain::Util::AGENT and return $Net::OpenID::JanRain::Util::AGENT 0 0 0
135 # try to find LWPx::ParanoidAgent
136 # fall back on LWP::UserAgent
137 0 my @agents = qw(
138 LWPx::ParanoidAgent
139 LWP::UserAgent
140 );
141 0 ▶ my $chooser = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
142 0 my @agents = @_;
143 0 ▶ for(my $i = 0; $i < @agents; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @agents) 0 0 0
144 0 eval("use $agents[$i];");
145 0 ▶ $@ or return($agents[$i]); # got one
Branch: 1 branch
BranchTrueFalseTotal
$@ or return $agents[$i] 0 0 0
146 0 ▶ if($i < $#agents) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#agents) { } 0 0 0
147 0 ▶ if($@ =~ m/^Can't locate/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Can't locate/) { } 0 0 0
148 0 warn("$0: consider installing more secure $agents[$i]");
149 }
150 else {
151 0 warn("problem loading $agents[$i]: ($@)");
152 }
153 }
154 else {
155 0 warn("cannot choose an agent ($@)");
156 }
157 }
158 0 return();
159 0 };
160 0 $AGENT = $chooser->(@agents);
161 0 ▶ $AGENT or die "No HTTP User agent found";
Branch: 1 branch
BranchTrueFalseTotal
$Net::OpenID::JanRain::Util::AGENT or die "No HTTP User agent found" 0 0 0
162 0 return $AGENT;
163 }
164  
165 0 ▶sub normalizeUrl {
Subroutine normalizeUrl: not called CC=3 SCAR=24.8
Pod undocumented
166 0 my $url = shift;
167 0 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
168 0 carp "normalizeUrl falsely called";
169 0 return undef;
170 }
171 0 ▶ $url = "http://$url" unless($url =~ m#^\w+://#);
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^\w+://] 0 0 0
172 0 return(URI->new($url)->canonical);
173 } # end normalizeUrl
174  
175 =head3 checkTrustRoot
176  
177 $is_return_to_valid_against_trust_root = checkTrustRoot($trust_root, $return_to);
178  
179 =cut
180  
181 0 ▶sub checkTrustRoot {
Subroutine checkTrustRoot: not called CC=7 SCAR=40.3
Pod documented
182 0 my ($trust_root, $return_to) = @_;
183  
184 0 my $rt = URI->new($return_to);
185 0 my $tr = URI->new($trust_root);
186
187 0 ▶ return 0, "return_to URL invalid against trust_root: scheme"
Branch: 1 branch
BranchTrueFalseTotal
unless $rt->scheme eq $tr->scheme 0 0 0
188 unless $rt->scheme eq $tr->scheme;
189  
190 # Check the host
191 0 my $trh = $tr->host;
192 0 ▶ if($trh =~ s/^\*\.//) { # wildcard trust root
Branch: 1 branch
BranchTrueFalseTotal
if ($trh =~ s/^\*\.//) { } 0 0 0
193 0 ▶ return 0, "return_to URL invalid against trust_root: wchost"
Branch: 1 branch
BranchTrueFalseTotal
unless $rt->host =~ /\w*\.?$trh/ and $rt->port == $tr->port 0 0 0
Condition0%0/3
$rt->host =~ /\w*\.?$trh/ and $rt->port == $tr->port
!ll&&!rl&&r
000
MC/DC0%0/2
$rt->host =~ /\w*\.?$trh/ and $rt->port == $tr->port
$rt->host =~ /\w*\.?$trh/ $rt->port == $tr->port
Truth table0%0/3
A and B
A= $rt->host =~ /\w*\.?$trh/B= $rt->port == $tr->port
ABresult
0X0
100
111
194 unless ($rt->host =~ /\w*\.?$trh/ and $rt->port == $tr->port);
195 }
196 else { # no wildcard
197 0 ▶ return 0, "return_to URL invalid against trust_root: host"
Branch: 1 branch
BranchTrueFalseTotal
unless $tr->host_port eq $rt->host_port 0 0 0
198 unless $tr->host_port eq $rt->host_port;
199 }
200
201 # Check the path and query
202 0 my $trp = $tr->path_query;
203 0 ▶ return 0, "return_to URL invalid against trust_root: path"
Branch: 1 branch
BranchTrueFalseTotal
unless $rt->path_query =~ /^$trp/ 0 0 0
204 unless $rt->path_query =~ /^$trp/;
205  
206 # success
207 0 return 1, "return_to URL valid against trust_root";
208 }
209 5 1;
↑ Summary Stores/FileStore.pm »