blib/lib/Paws/Net/V2Signature.pm

filter: Statementstmt 87.0% 67 / 77 Branchbran 55.0% 11 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 3 totaltotal 75.0% 93 / 124 CC 13 SCAR 19.2
CC
13
Cov
78%
_split_url
16.3
www_form_urlencode
3.1
sign
3.1
CRAP 6.8
« blib/lib/Paws/Net/RestXmlCaller.pm ↑ Summary blib/lib/Paws/Organizations.pm »
1 package Paws::Net::V2Signature;
2 3 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶ use Digest::SHA qw(hmac_sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶ use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶ use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
10 20 my $self = shift;
11  
12 # These calls are here so that when you construct
13 # the object the endpoint information and the _region_for_signature
14 # are calculated during construction. This is to avoid the fact that
15 # these attributes are lazy (because they depend on other attributes)
16 # and they don't get used until the first method is called, so if
17 # they are incorrect, they don't throw until the first method is called.
18 # It's much better to have them throw when $paws->service('...') is called
19 # as this is the point where the user had specified "incorrect" information,
20 # instead of the problem happening in the first method call.
21 20 $self->endpoint;
22 20 $self->_region_for_signature;
23 }
24  
25 has 'base_url' => (
26 is => 'ro',
27 required => 1,
28 lazy => 1,
29 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 sprintf 'https://%s', $_[0]->endpoint_host;
31 }
32 3 );
33  
34 has '_base_url_host' => (
35 is => 'ro',
36 required => 1,
37 lazy => 1,
38 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 2 ($_[0]->_split_url($_[0]->base_url))[1]
40 }
41 3 );
42  
43 # Lifted off HTTP::Tiny
44 2 ▶sub _split_url {
Subroutine _split_url: called CC=9 SCAR=27.9
45 2 my $url = pop;
46  
47 # URI regex adapted from the URI module
48 2 ▶ my ($scheme, $host, $path_query) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#]*)>
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ m[\A([^:/?#]+)://([^/?#]*)([^#]*)] or die "Cannot parse URL: '${url}'\n" 2 0 2
49 or die(qq/Cannot parse URL: '$url'\n/);
50  
51 2 $scheme = lc $scheme;
52 2 ▶ $path_query = "/$path_query" unless $path_query =~ m<\A/>;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_query =~ m[\A/] 2 0 2
53  
54 2 my $auth = '';
55 2 ▶ if ( (my $i = index $host, '@') != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $i = index($host, "\@")) != -1) 0 2 2
56 # user:pass@host
57 0 $auth = substr $host, 0, $i, ''; # take up to the @ for auth
58 0 substr $host, 0, 1, ''; # knock the @ off the host
59  
60 # userinfo might be percent escaped, so recover real auth info
61 0 $auth =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
62 }
63 2 ▶ my $port = $host =~ s/:(\d*)\z// && length $1 ? $1
Branch: 3 branches
BranchTrueFalseTotal
$host =~ s/:(\d*)\z// && length $1 ? : 0 2 2
$scheme eq 'http' ? : 0 2 2
$scheme eq 'https' ? : 2 0 2
Condition33%1/3
$host =~ s/:(\d*)\z// and length $1
!ll&&!rl&&r
200
MC/DC0%0/2
$host =~ s/:(\d*)\z// and length $1
$host =~ s/:(\d*)\z// length $1
Truth table33%1/3
A and B
A= $host =~ s/:(\d*)\z//B= length $1
ABresult
0X0
100
111
64 : $scheme eq 'http' ? 80
65 : $scheme eq 'https' ? 443
66 : undef;
67  
68 2 ▶ return ($scheme, (length $host ? lc $host : "localhost") , $port, $path_query, $auth);
Branch: 1 branch
BranchTrueFalseTotal
length $host ? : 2 0 2
69 }
70  
71 2 ▶sub sign {
Subroutine sign: called CC=3 SCAR=11.2
Pod undocumented
72 2 my ($self, $request) = @_;
73  
74 2 $request->parameters->{ SignatureVersion } = "2";
75 2 $request->parameters->{ SignatureMethod } = "HmacSHA256";
76 2 ▶ $request->parameters->{ Timestamp } //= strftime("%Y-%m-%dT%H:%M:%SZ",gmtime);
Condition33%1/3
$request->parameters->{"Timestamp"} //= strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
l!l&&r!l&&!r
020
MC/DC0%0/2
$request->parameters->{"Timestamp"} //= strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
$request->parameters->{"Timestamp"} strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
Truth table33%1/3
A //= B
A= $request->parameters->{"Timestamp"}B= strftime("%Y-%m-%dT%H:%M:%SZ", gmtime)
ABresult
1X1
011
000
77 2 $request->parameters->{ AWSAccessKeyId } = $self->access_key;
78  
79 2 ▶ if ($self->session_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->session_token) 0 2 2
80 0 $request->parameters->{ SecurityToken } = $self->session_token;
81 }
82  
83 2 my %sign_hash = %{ $request->parameters };
84 2 my $sign_this = "POST\n";
85 2 $sign_this .= $self->_base_url_host . "\n";
86 2 $sign_this .= "/\n";
87  
88  
89 2 $sign_this .= $self->www_form_urlencode(\%sign_hash);
90  
91 2 my $encoded = encode_base64(hmac_sha256($sign_this, $self->secret_key), '');
92  
93 2 $request->parameters->{ Signature } = $encoded;
94  
95 2 $request->content($self->generate_content_from_parameters($request));
96 }
97  
98  
99 2 ▶sub www_form_urlencode {
Subroutine www_form_urlencode: called CC=3 SCAR=11.2
Pod undocumented
100 2 my ($self, $data) = @_;
101  
102 2 my @params = %$data;
103  
104 2 my @terms;
105 2 ▶ while( @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 12 2 14
106 12 my ($key, $value) = splice(@params, 0, 2);
107 12 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 12 12
108 0 unshift @params, map { $key => $_ } @$value;
109 }
110 else {
111 12 push @terms, join("=", map { $self->_uri_escape($_) } $key, $value);
112 }
113 }
114  
115 2 return join("&", sort @terms );
116 }
117  
118 # URI escaping adapted from URI::Escape
119 # c.f. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
120 # perl 5.6 ready UTF-8 encoding adapted from JSON::PP
121 3 our %escapes = map { chr($_) => sprintf("%%%02X", $_) } 0..255;
122 3 our $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;
123  
124 24 ▶sub _uri_escape {
Subroutine _uri_escape: called CC=1 SCAR=0.0
125 24 my ($self, $str) = @_;
126 24 utf8::encode($str);
127 24 $str =~ s/($unsafe_char)/$escapes{$1}/ge;
128 24 $str =~ s/ /+/go;
129 24 return $str;
130 }
131  
132 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
133 0 my $self = shift;
134 0 my $params = shift;
135  
136 0 return $self->ua->post_form( $self->base_url, $params );
137 }
138  
139 3 1;
« blib/lib/Paws/Net/RestXmlCaller.pm ↑ Summary blib/lib/Paws/Organizations.pm »