Twilio/Jwt/AccessToken.pm

filter: Statementstmt 100% 61 / 61 Branchbran 100% 10 / 10 Conditioncond 86.6% 13 / 15 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 95.9% 118 / 123 CC 13 SCAR 10.3
CC
13
Cov
98%
add_grant
6.0
to_jwt
4.0
_grants
4.0
CRAP 2.8
« Twilio/Stream.pm ↑ Summary Twilio/Deserialize.pm »
1 package Twilio::Jwt::AccessToken;
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 use overload
7 9 ▶ q{""} => sub { $_[0]->to_jwt },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 1 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
9 2 fallback => 1;
10  
11 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Twilio::Jwt::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '2.001';
17  
18 2 my $DEFAULT_TTL = 3600;
19 2 my $CONTENT_TYPE = 'twilio-fpa;v=1';
20  
21 30 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
22 30 my ($class, $account_sid, $signing_key_sid, $secret, $ttl, $identity, $region) = @_;
23  
24 30 ▶ return bless {
Condition66%2/3
$ttl // $DEFAULT_TTL
l!l&&r!l&&!r
1290
MC/DC0%0/2
$ttl // $DEFAULT_TTL
$ttl $DEFAULT_TTL
Truth table66%2/3
A // B
A= $ttlB= $DEFAULT_TTL
ABresult
1X1
011
000
25 account_sid => $account_sid,
26 signing_key_sid => $signing_key_sid,
27 secret => $secret,
28 ttl => $ttl // $DEFAULT_TTL,
29 identity => $identity,
30 region => $region,
31 grants => [],
32 claims => {},
33 }, $class;
34 }
35  
36 1 ▶sub get_identity { return $_[0]{identity} }
Subroutine get_identity: called CC=1 SCAR=0.0
Pod documented
37 1 ▶sub get_nbf { return $_[0]{nbf} }
Subroutine get_nbf: called CC=1 SCAR=0.0
Pod documented
38 1 ▶sub get_region { return $_[0]{region} }
Subroutine get_region: called CC=1 SCAR=0.0
Pod documented
39  
40 1 ▶sub set_identity {
Subroutine set_identity: called CC=1 SCAR=0.0
Pod documented
41 1 my ($self, $identity) = @_;
42 1 $self->{identity} = $identity;
43 1 return $self;
44 }
45  
46 2 ▶sub set_nbf {
Subroutine set_nbf: called CC=1 SCAR=0.0
Pod documented
47 2 my ($self, $nbf) = @_;
48 2 $self->{nbf} = $nbf;
49 2 return $self;
50 }
51  
52 1 ▶sub set_region {
Subroutine set_region: called CC=1 SCAR=0.0
Pod documented
53 1 my ($self, $region) = @_;
54 1 $self->{region} = $region;
55 1 return $self;
56 }
57  
58 17 ▶sub add_grant {
Subroutine add_grant: called CC=6 SCAR=17.9
Pod documented
59 17 my ($self, $grant) = @_;
60  
61 # Checked here, as twilio-python does: otherwise the mistake surfaces only in to_jwt, far from
62 # where it was made and as a missing method.
63 17 ▶ if (!blessed($grant) || !$grant->can('get_grant_key') || !$grant->can('get_payload')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($grant) and $grant->can("get_grant_key") and $grant->can("get_payload")) 5 12 17
Condition83%5/6
builtin::blessed($grant) and $grant->can("get_grant_key") and $grant->can("get_payload")
!ll&&!rl&&r
5012
builtin::blessed($grant) and $grant->can("get_grant_key")
!ll&&!rl&&r
4112
MC/DC66%2/3
builtin::blessed($grant) and $grant->can("get_grant_key") and $grant->can("get_payload")
builtin::blessed($grant) $grant->can("get_grant_key") $grant->can("get_payload")
Truth table75%3/4
A and B and C
A= builtin::blessed($grant)B= $grant->can("get_grant_key")C= $grant->can("get_payload")
ABCresult
0XX0
10X0
1100
1111
64 5 ▶ croak 'add_grant takes a grant object, such as Twilio::Jwt::Grants::VideoGrant->new, not '
Branch: 1 branch
BranchTrueFalseTotal
defined $grant ? : 4 1 5
Condition100%2/2
ref $grant or "'${grant}'"
l!l
22
MC/DC100%1/1
ref $grant or "'${grant}'"
ref $grant
Truth table100%2/2
A
A= ref $grant
Aresult
11
00
65 . (defined $grant ? (ref $grant || "'$grant'") : 'undef');
66 }
67 12 push @{ $self->{grants} }, $grant;
68 12 return $self;
69 }
70  
71 9 ▶sub add_claim {
Subroutine add_claim: called CC=1 SCAR=0.0
Pod documented
72 9 my ($self, $name, $value) = @_;
73 9 $self->{claims}{$name} = $value;
74 9 return;
75 }
76  
77 42 ▶sub to_jwt {
Subroutine to_jwt: called CC=4 SCAR=13.9
Pod documented
78 42 my ($self, $algorithm) = @_;
79  
80 42 my %header = (cty => $CONTENT_TYPE);
81 42 ▶ $header{twr} = $self->{region} if length($self->{region} // q{});
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"region"} // "") 11 31 42
Condition100%2/2
$self->{"region"} // ""
l!l
1131
MC/DC100%1/1
$self->{"region"} // ""
$self->{"region"}
Truth table100%2/2
A
A= $self->{"region"}
Aresult
11
00
82  
83 42 my $key = $self->{signing_key_sid};
84 42 my $now = time;
85  
86 # Custom claims come first, so that one can never replace the claims that identify the token.
87 my %payload = (
88 42 %{ $self->{claims} },
89 jti => "$key-$now",
90 iss => $key,
91 sub => $self->{account_sid},
92 exp => $now + $self->{ttl},
93 42 grants => $self->_grants,
94 );
95 42 ▶ $payload{nbf} = 0 + $self->{nbf} if defined $self->{nbf};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"nbf"} 12 30 42
96  
97 42 return Twilio::Jwt::JWT->encode(\%payload, $self->{secret}, $algorithm, \%header);
98 }
99  
100 42 ▶sub _grants {
Subroutine _grants: called CC=4 SCAR=13.9
101 42 my ($self) = @_;
102  
103 42 my %grants;
104 42 ▶ $grants{identity} = $self->{identity} if length($self->{identity} // q{});
Branch: 1 branch
BranchTrueFalseTotal
if length($self->{"identity"} // "") 18 24 42
Condition100%2/2
$self->{"identity"} // ""
l!l
1824
MC/DC100%1/1
$self->{"identity"} // ""
$self->{"identity"}
Truth table100%2/2
A
A= $self->{"identity"}
Aresult
11
00
105 42 $grants{ $_->get_grant_key } = $_->get_payload for @{ $self->{grants} };
106 42 return \%grants;
107 }
108  
109 2 1;
110  
111 __END__
« Twilio/Stream.pm ↑ Summary Twilio/Deserialize.pm »