Twilio/Http/ValidationClient.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 2 / 2 Conditioncond 68.7% 11 / 16 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 87.5% 70 / 80 CC 11 SCAR 14.8
CC
11
Cov
92%
validation_token
7.3
new
5.0
BEGIN
1.0
CRAP 4.4
« Twilio/Security/RequestValidator.pm ↑ Summary Twilio/ApiV1Version.pm »
1 package Twilio::Http::ValidationClient;
2  
3 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use parent 'Twilio::Http::TinyClient';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Twilio::Exceptions::ConfigurationException;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Twilio::Jwt::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Twilio::Jwt::Validation::ClientValidationJwt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '2.001';
13  
14 2 my @REQUIRED = qw(account_sid api_key_sid credential_sid private_key);
15 2 my @SIGNED_HEADERS = qw(authorization host);
16  
17 9 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
18 9 my ($class, $options) = @_;
19 9 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
90
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
20  
21 9 for my $name (@REQUIRED) {
22 30 ▶ next if length($options->{$name} // q{});
Branch: 1 branch
BranchTrueFalseTotal
if length($options->{$name} // "") 26 4 30
Condition100%2/2
$options->{$name} // ""
l!l
264
MC/DC100%1/1
$options->{$name} // ""
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
23 4 Twilio::Exceptions::ConfigurationException->throw("$name is required for client validation");
24 }
25  
26 # A key that can't sign fails here, at start-up, rather than on the first request.
27 5 Twilio::Jwt::JWT->sign('key check', $options->{private_key}, 'RS256');
28  
29 4 return $class->SUPER::new($options);
30 }
31  
32 # Signed after the parent class has added Authorization, so that header is covered too.
33 4 ▶sub prepare_request {
Subroutine prepare_request: called CC=1 SCAR=0.0
Pod documented
34 4 my ($self, @arguments) = @_;
35 4 my $request = $self->SUPER::prepare_request(@arguments);
36 4 $request->{headers}{'Twilio-Client-Validation'} = $self->validation_token($request);
37 4 return $request;
38 }
39  
40 5 ▶sub validation_token {
Subroutine validation_token: called CC=7 SCAR=19.9
Pod documented
41 5 my ($self, $request) = @_;
42  
43 my ($authority, $path, $query) =
44 5 ▶ ($request->{url} // q{}) =~ m{\A[^:/?#]+://([^/?#]*)([^?#]*)(?:[?]([^#]*))?};
Condition50%1/2
$request->{"url"} // ""
l!l
50
MC/DC0%0/1
$request->{"url"} // ""
$request->{"url"}
Truth table50%1/2
A
A= $request->{"url"}
Aresult
11
00
45 5 ▶ my $host = ($authority // q{}) =~ s/\A.*@//r;
Condition50%1/2
$authority // ""
l!l
50
MC/DC0%0/1
$authority // ""
$authority
Truth table50%1/2
A
A= $authority
Aresult
11
00
46  
47 # HTTP::Tiny sends the Host of the URL, so that is the one to sign, whatever a caller set.
48 5 ▶ my %headers = %{ $request->{headers} // {} };
Condition50%1/2
$request->{"headers"} // {}
l!l
50
MC/DC0%0/1
$request->{"headers"} // {}
$request->{"headers"}
Truth table50%1/2
A
A= $request->{"headers"}
Aresult
11
00
49 5 delete @headers{ grep { lc eq 'host' } keys %headers };
50  
51 my $jwt = Twilio::Jwt::Validation::ClientValidationJwt->new(
52 5 ▶ @{$self}{@REQUIRED},
Condition83%5/6
$path // ""
l!l
50
$query // ""
l!l
14
$request->{"content"} // ""
l!l
14
MC/DC0%0/1
$path // ""
$path
MC/DC100%1/1
$query // ""
$query
MC/DC100%1/1
$request->{"content"} // ""
$request->{"content"}
Truth table50%1/2
A
A= $path
Aresult
11
00
Truth table100%2/2
A
A= $query
Aresult
11
00
Truth table100%2/2
A
A= $request->{"content"}
Aresult
11
00
53 {
54 method => $request->{method},
55 path => $path // q{},
56 query_string => $query // q{},
57 all_headers => { %headers, host => $host },
58 signed_headers => [@SIGNED_HEADERS],
59 5 body => $request->{content} // q{},
60 }
61 );
62 5 return $jwt->to_jwt;
63 }
64  
65 2 1;
66  
67 __END__
« Twilio/Security/RequestValidator.pm ↑ Summary Twilio/ApiV1Version.pm »