Twilio/Jwt/JWT.pm

filter: Statementstmt 97.9% 96 / 98 Branchbran 96.4% 27 / 28 Conditioncond 81.4% 22 / 27 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 93.1% 177 / 190 CC 27 SCAR 16.2
CC
27
Cov
95%
decode
10.0
_rsa_key
7.1
sign
6.1
CRAP 5.1
« Twilio/Http/Client.pm ↑ Summary Twilio/Security/RequestValidator.pm »
1 package Twilio::Jwt::JWT;
2  
3 5 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Digest::SHA qw(hmac_sha256 hmac_sha384 hmac_sha512);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Twilio::Exceptions::TwilioException;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Twilio::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Twilio::Security::RequestValidator;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '2.001';
14  
15 5 my $SEGMENTS = 3;
16  
17 # All of these need a key, so a token cannot choose a method that does not. "none" is the
18 # classic way to forge a JWT, and it is refused here for not being in either list.
19 5 my %METHOD = (HS256 => \&hmac_sha256, HS384 => \&hmac_sha384, HS512 => \&hmac_sha512);
20  
21 # RS256 is RSA with PKCS#1 v1.5 padding and SHA-256, signed with a private key and checked with
22 # the public one. Only client validation needs it, so its module is optional and loaded then.
23 5 my %RSA_HASH = (RS256 => 'use_sha256_hash');
24  
25 79 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
26 79 my ($class, $payload, $key, $algorithm, $headers) = @_;
27 79 ▶ $algorithm //= 'HS256';
Condition100%2/2
$algorithm //= "HS256"
l!l
1168
MC/DC100%1/1
$algorithm //= "HS256"
$algorithm
Truth table100%2/2
A
A= $algorithm
Aresult
11
00
28  
29 79 ▶ my %header = (%{ $headers // {} }, typ => 'JWT', alg => $algorithm);
Condition100%2/2
$headers // {}
l!l
4930
MC/DC100%1/1
$headers // {}
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
30 79 my $signed = join q{.}, $class->urlsafe_b64_encode(Twilio::Json::encode(\%header)),
31 $class->urlsafe_b64_encode(Twilio::Json::encode($payload));
32  
33 79 return join q{.}, $signed, $class->urlsafe_b64_encode($class->sign($signed, $key, $algorithm));
34 }
35  
36 79 ▶sub decode {
Subroutine decode: called CC=10 SCAR=23.0
Pod documented
37 79 my ($class, $token, $key, $verify) = @_;
38 79 ▶ $verify //= 1;
Condition100%2/2
$verify //= 1
l!l
277
MC/DC100%1/1
$verify //= 1
$verify
Truth table100%2/2
A
A= $verify
Aresult
11
00
39  
40 79 my ($header_text, $payload_text, $signature_text) = _segments($token);
41 75 my $header = _json($class->urlsafe_b64_decode($header_text));
42 74 my $payload = _json($class->urlsafe_b64_decode($payload_text));
43 74 ▶ return $payload if !$verify;
Branch: 1 branch
BranchTrueFalseTotal
unless $verify 2 72 74
44  
45 72 ▶ _fail('Empty algorithm') if ref $header ne 'HASH' || !length($header->{alg} // q{});
Branch: 1 branch
BranchTrueFalseTotal
if ref $header ne "HASH" or not length($header->{"alg"} // "") 1 71 72
Condition80%4/5
ref $header ne "HASH" or not length($header->{"alg"} // "")
l!l&&r!l&&!r
0171
$header->{"alg"} // ""
l!l
711
MC/DC50%1/2
ref $header ne "HASH" or not length($header->{"alg"} // "")
ref $header ne "HASH" $header->{"alg"}
Truth table66%2/3
A or not(B)
A= ref $header ne "HASH"B= $header->{"alg"}
ABresult
1X1
001
010
46 71 ▶ _fail('A key is needed to verify a token') if !length($key // q{});
Branch: 1 branch
BranchTrueFalseTotal
unless length($key // "") 1 70 71
Condition100%2/2
$key // ""
l!l
701
MC/DC100%1/1
$key // ""
$key
Truth table100%2/2
A
A= $key
Aresult
11
00
47  
48 70 my $signed = "$header_text.$payload_text";
49 70 my $given = $class->urlsafe_b64_decode($signature_text);
50 70 my $valid;
51 70 ▶ if ($RSA_HASH{ $header->{alg} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RSA_HASH{$header->{'alg'}}) { } 7 63 70
52 7 my $public = _rsa_key($key, 'public', $header->{alg});
53 6 $valid = eval { $public->verify($signed, $given) };
54 }
55 else {
56 63 my $expected = $class->sign($signed, $key, $header->{alg});
57 59 $valid = Twilio::Security::RequestValidator->compare($given, $expected);
58 }
59 65 ▶ _fail('Signature verification failed') if !$valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 6 59 65
60 59 return $payload;
61 }
62  
63 45 ▶sub get_header {
Subroutine get_header: called CC=1 SCAR=0.0
Pod documented
64 45 my ($class, $token) = @_;
65  
66 45 my ($header_text) = _segments($token);
67 45 return _json($class->urlsafe_b64_decode($header_text));
68 }
69  
70 157 ▶sub sign {
Subroutine sign: called CC=6 SCAR=18.0
Pod documented
71 157 my ($class, $message, $key, $algorithm) = @_;
72 157 ▶ $algorithm //= 'HS256';
Condition100%2/2
$algorithm //= "HS256"
l!l
1552
MC/DC100%1/1
$algorithm //= "HS256"
$algorithm
Truth table100%2/2
A
A= $algorithm
Aresult
11
00
73  
74 157 ▶ return _rsa_key($key, 'private', $algorithm)->sign(_bytes($message)) if $RSA_HASH{$algorithm};
Branch: 1 branch
BranchTrueFalseTotal
if $RSA_HASH{$algorithm} 16 141 157
75  
76 141 ▶ my $method = $METHOD{$algorithm} // _fail('Algorithm not supported');
Condition66%2/3
$METHOD{$algorithm} // _fail("Algorithm not supported")
l!l&&r!l&&!r
13740
MC/DC0%0/2
$METHOD{$algorithm} // _fail("Algorithm not supported")
$METHOD{$algorithm} _fail("Algorithm not supported")
Truth table33%1/3
A // B
A= $METHOD{$algorithm}B= _fail("Algorithm not supported")
ABresult
1X1
011
000
77  
78 # A token that names HS256 must not be checked with the text of a public key as its secret:
79 # anyone could sign that, since the key is public.
80 137 ▶ _fail("$algorithm takes a shared secret, not a key in PEM form")
Branch: 1 branch
BranchTrueFalseTotal
if ($key // "") =~ /-----BEGIN /u 1 136 137
Condition50%1/2
$key // ""
l!l
1370
MC/DC0%0/1
$key // ""
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
81 if ($key // q{}) =~ /-----BEGIN /;
82 136 return $method->($message, _bytes($key));
83 }
84  
85 254 ▶sub urlsafe_b64_encode {
Subroutine urlsafe_b64_encode: called CC=1 SCAR=0.0
Pod documented
86 254 my ($class, $text) = @_;
87  
88 254 my $encoded = MIME::Base64::encode_base64($text, q{});
89 254 $encoded =~ tr{+/}{-_};
90 254 $encoded =~ tr/=//d;
91 254 return $encoded;
92 }
93  
94 272 ▶sub urlsafe_b64_decode {
Subroutine urlsafe_b64_decode: called CC=1 SCAR=0.0
Pod documented
95 272 my ($class, $text) = @_;
96  
97 272 my $standard = $text =~ tr{-_}{+/}r;
98 272 return MIME::Base64::decode_base64($standard);
99 }
100  
101 124 ▶sub _segments {
Subroutine _segments: called CC=3 SCAR=11.0
102 124 my ($token) = @_;
103  
104 124 ▶ my @segments = split /[.]/, $token // q{}, -1;
Condition100%2/2
$token // ""
l!l
1231
MC/DC100%1/1
$token // ""
$token
Truth table100%2/2
A
A= $token
Aresult
11
00
105 124 ▶ _fail('Wrong number of segments') if @segments != $SEGMENTS;
Branch: 1 branch
BranchTrueFalseTotal
if @segments != $SEGMENTS 4 120 124
106 120 return @segments;
107 }
108  
109 194 ▶sub _json {
Subroutine _json: called CC=2 SCAR=6.9
110 194 my ($text) = @_;
111  
112 194 my $value = eval { Twilio::Json::decode($text) };
113 194 ▶ _fail('Invalid segment encoding') if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 193 194
114 193 return $value;
115 }
116  
117 23 ▶sub _rsa_key {
Subroutine _rsa_key: called CC=7 SCAR=19.7
118 23 my ($pem, $kind, $algorithm) = @_;
119  
120 23 ▶ if (!eval { require Crypt::OpenSSL::RSA; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Crypt::OpenSSL::RSA; 1 } }) 1 22 23
121 1 _fail("$algorithm needs the Perl module Crypt::OpenSSL::RSA, which is not installed");
122 }
123 my $key = length($pem // q{})
124 22 ▶ && eval {
Condition60%3/5
length($pem // '') && eval { do { $kind eq 'private' ? 'Crypt::OpenSSL::RSA'->new_private_key(_bytes($pem)) : 'Crypt::OpenSSL::RSA'->new_public_key(_bytes($pem)) } }
!ll&&!rl&&r
0319
$pem // ""
l!l
220
MC/DC50%1/2
length($pem // '') && eval { do { $kind eq 'private' ? 'Crypt::OpenSSL::RSA'->new_private_key(_bytes($pem)) : 'Crypt::OpenSSL::RSA'->new_public_key(_bytes($pem)) } }
$pem eval { do { $kind eq 'private' ? 'Crypt::OpenSSL::RSA'->new_private_key(_bytes($pem)) : 'Crypt::OpenSSL::RSA'->new_public_key(_bytes($pem)) } }
Truth table66%2/3
A && B
A= $pemB= eval { do { $kind eq 'private' ? 'Crypt::OpenSSL::RSA'->new_private_key(_bytes($pem)) : 'Crypt::OpenSSL::RSA'->new_public_key(_bytes($pem)) } }
ABresult
0X0
100
111
125 22 ▶ $kind eq 'private'
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'private' ? : 15 7 22
126 ? Crypt::OpenSSL::RSA->new_private_key(_bytes($pem))
127 : Crypt::OpenSSL::RSA->new_public_key(_bytes($pem));
128 };
129 22 ▶ _fail("$algorithm needs an RSA $kind key in PEM form") if !$key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 3 19 22
130  
131 19 my $hash = $RSA_HASH{$algorithm};
132 19 $key->$hash;
133  
134 # Crypt::OpenSSL::RSA 0.35 to 0.37 refused this padding for signatures too, not only for
135 # encryption; 0.38 allows it again.
136 19 ▶ if (!eval { $key->use_pkcs1_padding; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $key->use_pkcs1_padding; 1 } }) 0 19 19
137 0 _fail("$algorithm needs Crypt::OpenSSL::RSA 0.38 or later (or before 0.35), which allow "
138 . 'PKCS#1 v1.5 signatures');
139 }
140 19 return $key;
141 }
142  
143 171 ▶sub _bytes {
Subroutine _bytes: called CC=2 SCAR=6.9
144 171 my ($text) = @_;
145 171 my $bytes = "$text";
146 171 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 1 170 171
147 171 return $bytes;
148 }
149  
150 22 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.4
151 22 my ($message) = @_;
152 22 Twilio::Exceptions::TwilioException->throw($message);
153 0 return;
154 }
155  
156 5 1;
157  
158 __END__
« Twilio/Http/Client.pm ↑ Summary Twilio/Security/RequestValidator.pm »