| 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
Condition100%2/2
$given{$name} // ""
MC/DC100%1/1
$given{$name} // ""
$given{$name}
Truth table100%2/2
A
A= $given{$name}
| |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | Twilio::Exceptions::ConfigurationException->throw("$name is required for client validation"); | |||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 30 | 17 | ▶ | if (ref $validation_payload ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$now //= time
$now
Truth table100%2/2
A
A= $now
| |||||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
MC/DC100%1/1
$payload->{"body"} // ""
$payload->{"body"}
Truth table100%2/2
A
A= $payload->{"body"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$payload->{"method"} // ""
$payload->{"path"} // ""
$payload->{"query_string"} // ""
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"}
Truth table50%1/2
A
A= $payload->{"path"}
Truth table100%2/2
A
A= $payload->{"query_string"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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"]
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"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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 // {}
MC/DC100%1/1
$headers // {}
$headers
Truth table100%2/2
A
A= $headers
| |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 61 | my $value = $headers->{$name}; | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 61 | ▶ | $lowercase{ lc $name } = ref $value eq 'ARRAY' ? join(q{,}, sort @{$value}) : $value // q{}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$value // ""
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 39 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||