Twilio/Jwt/Validation/ClientValidationJwt.pm

filter: Statementstmt 100% 57 / 57 Branchbran 91.6% 11 / 12 Conditioncond 83.3% 15 / 18 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 93.7% 105 / 112 CC 18 SCAR 14.0
CC
18
Cov
95%
canonical_request
7.0
_lowercase_headers
5.0
new
5.0
CRAP 4.0
« Twilio/Jwt/Grants/VoiceGrant.pm ↑ Summary Twilio/Metadata.pm »
1 package Twilio::Jwt::Validation::ClientValidationJwt;
2  
3 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Twilio::Exceptions::ConfigurationException;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Twilio::Jwt::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '2.001';
12  
13 3 my $CONTENT_TYPE = 'twilio-pkrv;v=1';
14 3 my $ALGORITHM = 'RS256';
15 3 my $TTL = 300;
16  
17 21 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
18 21 my ($class, $account_sid, $api_key_sid, $credential_sid, $private_key, $validation_payload) = @_;
19  
20 21 my %given = (
21 account_sid => $account_sid,
22 api_key_sid => $api_key_sid,
23 credential_sid => $credential_sid,
24 private_key => $private_key,
25 );
26 21 for my $name (qw(account_sid api_key_sid credential_sid private_key)) {
27 78 ▶ next if length($given{$name} // q{});
Branch: 1 branch
BranchTrueFalseTotal
if length($given{$name} // "") 74 4 78
Condition100%2/2
$given{$name} // ""
l!l
744
MC/DC100%1/1
$given{$name} // ""
$given{$name}
Truth table100%2/2
A
A= $given{$name}
Aresult
11
00
28 4 Twilio::Exceptions::ConfigurationException->throw("$name is required for client validation");
29 }
30 17 ▶ if (ref $validation_payload ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $validation_payload ne "HASH") 1 16 17
31 1 Twilio::Exceptions::ConfigurationException->throw(
32 'Client validation needs the request to sign, as a hash reference');
33 }
34  
35 16 return bless { %given, validation_payload => $validation_payload }, $class;
36 }
37  
38 6 ▶sub to_jwt {
Subroutine to_jwt: called CC=1 SCAR=0.0
Pod documented
39 6 my ($self) = @_;
40 return Twilio::Jwt::JWT->encode($self->get_payload, $self->{private_key}, $ALGORITHM,
41 6 { cty => $CONTENT_TYPE, kid => $self->{credential_sid} });
42 }
43  
44 16 ▶sub get_payload {
Subroutine get_payload: called CC=2 SCAR=6.9
Pod documented
45 16 my ($self, $now) = @_;
46 16 ▶ $now //= time;
Condition100%2/2
$now //= time
l!l
115
MC/DC100%1/1
$now //= time
$now
Truth table100%2/2
A
A= $now
Aresult
11
00
47  
48 return {
49 iss => $self->{api_key_sid},
50 sub => $self->{account_sid},
51 16 nbf => $now,
52 exp => $now + $TTL,
53 hrh => join(q{;}, $self->_signed_headers),
54 rqh => sha256_hex(_bytes($self->canonical_request)),
55 };
56 }
57  
58 # The request in the fixed form Twilio rebuilds on its side, as in twilio-python: method, path,
59 # sorted query, the signed headers it has, a blank line, their names, and the body's hash.
60 23 ▶sub canonical_request {
Subroutine canonical_request: called CC=7 SCAR=19.5
Pod documented
61 23 my ($self) = @_;
62 23 my $payload = $self->{validation_payload};
63  
64 23 my %headers = _lowercase_headers($payload->{all_headers});
65 23 my @signed = $self->_signed_headers;
66 my @header_lines =
67 23 map { "$_:$headers{$_}" } grep { exists $headers{$_} } @signed;
68  
69 23 ▶ my $body = _bytes($payload->{body} // q{});
Condition100%2/2
$payload->{"body"} // ""
l!l
212
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
Aresult
11
00
70 return join "\n",
71 $payload->{method} // q{},
72 $payload->{path} // q{},
73 23 ▶ join(q{&}, sort split /&/, $payload->{query_string} // q{}),
Branch: 2 branches
BranchTrueFalseTotal
@header_lines ? : 20 3 23
length $body ? : 15 8 23
Condition66%4/6
$payload->{"method"} // ""
l!l
230
$payload->{"path"} // ""
l!l
230
$payload->{"query_string"} // ""
l!l
212
MC/DC0%0/1
$payload->{"method"} // ""
$payload->{"method"}
MC/DC0%0/1
$payload->{"path"} // ""
$payload->{"path"}
MC/DC100%1/1
$payload->{"query_string"} // ""
$payload->{"query_string"}
Truth table50%1/2
A
A= $payload->{"method"}
Aresult
11
00
Truth table50%1/2
A
A= $payload->{"path"}
Aresult
11
00
Truth table100%2/2
A
A= $payload->{"query_string"}
Aresult
11
00
74 (@header_lines ? join("\n", @header_lines) : ()),
75 q{},
76 join(q{;}, @signed),
77 length $body ? sha256_hex($body) : q{};
78 }
79  
80 39 ▶sub _signed_headers {
Subroutine _signed_headers: called CC=2 SCAR=6.9
81 39 my ($self) = @_;
82 39 ▶ my @signed = sort @{ $self->{validation_payload}{signed_headers} // [qw(authorization host)] };
Condition100%2/2
$self->{"validation_payload"}{"signed_headers"} // ["authorization", "host"]
l!l
2910
MC/DC100%1/1
$self->{"validation_payload"}{"signed_headers"} // ["authorization", "host"]
$self->{"validation_payload"}{"signed_headers"}
Truth table100%2/2
A
A= $self->{"validation_payload"}{"signed_headers"}
Aresult
11
00
83 39 return @signed;
84 }
85  
86 # A header given as a list is sorted and joined with commas, as twilio-python does.
87 23 ▶sub _lowercase_headers {
Subroutine _lowercase_headers: called CC=5 SCAR=16.1
88 23 my ($headers) = @_;
89 23 my %lowercase;
90 23 ▶ for my $name (keys %{ $headers // {} }) {
Condition100%2/2
$headers // {}
l!l
221
MC/DC100%1/1
$headers // {}
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
91 61 my $value = $headers->{$name};
92 61 ▶ $lowercase{ lc $name } = ref $value eq 'ARRAY' ? join(q{,}, sort @{$value}) : $value // q{};
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 60 61
Condition50%1/2
$value // ""
l!l
600
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
93 }
94 23 return %lowercase;
95 }
96  
97 39 ▶sub _bytes {
Subroutine _bytes: called CC=2 SCAR=7.0
98 39 my ($text) = @_;
99 39 my $bytes = "$text";
100 39 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 0 39 39
101 39 return $bytes;
102 }
103  
104 3 1;
105  
106 __END__
« Twilio/Jwt/Grants/VoiceGrant.pm ↑ Summary Twilio/Metadata.pm »