Twilio/Jwt/TaskRouter/CapabilityToken.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 100% 16 / 16 Conditioncond 82.3% 14 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 94.7% 18 / 19 Podpod 100% 11 / 11 totaltotal 92.1% 140 / 152 CC 16 SCAR 12.5
CC
16
Cov
96%
_validate
7.0
new
5.0
generate_token
5.0
CRAP 3.5
« Twilio/Exceptions/TwilioServiceException.pm ↑ Summary Twilio/Version.pm »
1 package Twilio::Jwt::TaskRouter::CapabilityToken;
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 Twilio::Exceptions::TwilioException;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Twilio::Jwt::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Twilio::Jwt::TaskRouter::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '2.001';
11  
12 2 my $BASE_URL = 'https://taskrouter.twilio.com/v1';
13 2 my $BASE_WS_URL = 'https://event-bridge.twilio.com/v1/wschannels';
14 2 my $API_VERSION = 'v1';
15 2 my $DEFAULT_TTL = 3600;
16  
17 20 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
18 my (
19 20 $class, $account_sid, $auth_token, $workspace_sid,
20 $channel_id, $resource_url, $base_url, $base_ws_url
21 ) = @_;
22  
23 20 ▶ my $self = bless {
Condition75%6/8
$base_url // $BASE_URL
l!l&&r!l&&!r
1190
$workspace_sid // ""
l!l
191
$base_ws_url // $BASE_WS_URL
l!l&&r!l&&!r
1190
MC/DC0%0/2
$base_url // $BASE_URL
$base_url $BASE_URL
MC/DC100%1/1
$workspace_sid // ""
$workspace_sid
MC/DC0%0/2
$base_ws_url // $BASE_WS_URL
$base_ws_url $BASE_WS_URL
Truth table66%2/3
A // B
A= $base_urlB= $BASE_URL
ABresult
1X1
011
000
Truth table100%2/2
A
A= $workspace_sid
Aresult
11
00
Truth table66%2/3
A // B
A= $base_ws_urlB= $BASE_WS_URL
ABresult
1X1
011
000
24 account_sid => $account_sid,
25 auth_token => $auth_token,
26 workspace_sid => $workspace_sid,
27 channel_id => $channel_id,
28 friendly_name => $channel_id,
29 resource_url => $resource_url,
30 base_url => ($base_url // $BASE_URL) . '/Workspaces/' . ($workspace_sid // q{}),
31 base_ws_url => $base_ws_url // $BASE_WS_URL,
32 policies => [],
33 }, $class;
34  
35 20 $self->_validate;
36 14 ▶ $self->setup_resource if !defined $resource_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resource_url 12 2 14
37  
38 # Every token can use its own event channel, and read its own resource.
39 14 my $channel = join q{/}, $self->{base_ws_url}, $account_sid, $channel_id;
40 14 $self->allow($channel, 'GET', undef, undef);
41 14 $self->allow($channel, 'POST', undef, undef);
42 14 $self->allow($self->{resource_url}, 'GET', undef, undef);
43 14 return $self;
44 }
45  
46 0 ▶sub setup_resource { return }
Subroutine setup_resource: not called CC=1 SCAR=6.9
Pod documented
47  
48 65 ▶sub add_policy_deconstructed {
Subroutine add_policy_deconstructed: called CC=1 SCAR=0.0
Pod documented
49 65 my ($self, $url, $method, @options) = @_;
50  
51 65 my $policy = Twilio::Jwt::TaskRouter::Policy->new($url, $method, @options);
52 65 push @{ $self->{policies} }, $policy;
53 65 return $policy;
54 }
55  
56 # Leaving out a filter means an empty one, and passing undef leaves it out of the token.
57 62 ▶sub allow {
Subroutine allow: called CC=1 SCAR=0.0
Pod documented
58 62 my ($self, $url, $method, @filters) = @_;
59 62 $self->add_policy_deconstructed($url, $method, _filter(0, @filters), _filter(1, @filters), 1);
60 62 return;
61 }
62  
63 1 ▶sub deny {
Subroutine deny: called CC=1 SCAR=0.0
Pod documented
64 1 my ($self, $url, $method, @filters) = @_;
65 1 $self->add_policy_deconstructed($url, $method, _filter(0, @filters), _filter(1, @filters), 0);
66 1 return;
67 }
68  
69 1 ▶sub allow_fetch_subresources {
Subroutine allow_fetch_subresources: called CC=1 SCAR=0.0
Pod documented
70 1 my ($self) = @_;
71 1 $self->allow("$self->{resource_url}/**", 'GET');
72 1 return;
73 }
74  
75 2 ▶sub allow_updates {
Subroutine allow_updates: called CC=1 SCAR=0.0
Pod documented
76 2 my ($self) = @_;
77 2 $self->allow($self->{resource_url}, 'POST');
78 2 return;
79 }
80  
81 1 ▶sub allow_updates_subresources {
Subroutine allow_updates_subresources: called CC=1 SCAR=0.0
Pod documented
82 1 my ($self) = @_;
83 1 $self->allow("$self->{resource_url}/**", 'POST');
84 1 return;
85 }
86  
87 1 ▶sub allow_delete {
Subroutine allow_delete: called CC=1 SCAR=0.0
Pod documented
88 1 my ($self) = @_;
89 1 $self->allow($self->{resource_url}, 'DELETE');
90 1 return;
91 }
92  
93 1 ▶sub allow_delete_subresources {
Subroutine allow_delete_subresources: called CC=1 SCAR=0.0
Pod documented
94 1 my ($self) = @_;
95 1 $self->allow("$self->{resource_url}/**", 'DELETE');
96 1 return;
97 }
98  
99 12 ▶sub generate_token {
Subroutine generate_token: called CC=5 SCAR=16.1
Pod documented
100 12 my ($self, $ttl, $extra_attributes) = @_;
101  
102 12 my $now = time;
103 12 my $channel = $self->{channel_id};
104 my %payload = (
105 version => $API_VERSION,
106 friendly_name => $self->{friendly_name},
107 iss => $self->{account_sid},
108 exp => $now + ($ttl // $DEFAULT_TTL),
109 account_sid => $self->{account_sid},
110 channel => $channel,
111 workspace_sid => $self->{workspace_sid},
112 ($channel =~ /\AWK/ ? (worker_sid => $channel) : ()),
113 ($channel =~ /\AWQ/ ? (taskqueue_sid => $channel) : ()),
114 12 ▶ %{ $extra_attributes // {} },
Branch: 2 branches
BranchTrueFalseTotal
$channel =~ /\AWK/u ? : 3 9 12
$channel =~ /\AWQ/u ? : 2 10 12
Condition80%4/5
$ttl // $DEFAULT_TTL
l!l&&r!l&&!r
1110
$extra_attributes // {}
l!l
210
MC/DC0%0/2
$ttl // $DEFAULT_TTL
$ttl $DEFAULT_TTL
MC/DC100%1/1
$extra_attributes // {}
$extra_attributes
Truth table66%2/3
A // B
A= $ttlB= $DEFAULT_TTL
ABresult
1X1
011
000
Truth table100%2/2
A
A= $extra_attributes
Aresult
11
00
115 );
116 12 $payload{policies} = [map { $_->to_hash } @{ $self->{policies} }];
117  
118 12 return Twilio::Jwt::JWT->encode(\%payload, $self->{auth_token});
119 }
120  
121 126 ▶sub _filter {
Subroutine _filter: called CC=2 SCAR=6.9
122 126 my ($position, @filters) = @_;
123 126 ▶ return $position < @filters ? $filters[$position] : {};
Branch: 1 branch
BranchTrueFalseTotal
$position < @filters ? : 106 20 126
124 }
125  
126 20 ▶sub _validate {
Subroutine _validate: called CC=7 SCAR=19.5
127 20 my ($self) = @_;
128  
129 20 ▶ my $account = $self->{account_sid} // q{};
Condition100%2/2
$self->{"account_sid"} // ""
l!l
191
MC/DC100%1/1
$self->{"account_sid"} // ""
$self->{"account_sid"}
Truth table100%2/2
A
A= $self->{"account_sid"}
Aresult
11
00
130 20 ▶ my $workspace = $self->{workspace_sid} // q{};
Condition100%2/2
$self->{"workspace_sid"} // ""
l!l
191
MC/DC100%1/1
$self->{"workspace_sid"} // ""
$self->{"workspace_sid"}
Truth table100%2/2
A
A= $self->{"workspace_sid"}
Aresult
11
00
131 20 my $channel = $self->{channel_id};
132  
133 20 ▶ _fail("Invalid AccountSid provided: $account") if $account !~ /\AAC/;
Branch: 1 branch
BranchTrueFalseTotal
unless $account =~ /\AAC/u 2 18 20
134 18 ▶ _fail("Invalid WorkspaceSid provided: $workspace") if $workspace !~ /\AWS/;
Branch: 1 branch
BranchTrueFalseTotal
unless $workspace =~ /\AWS/u 2 16 18
135 16 ▶ _fail('ChannelId not provided') if !defined $channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel 1 15 16
136 15 ▶ _fail("Invalid ChannelId provided: $channel") if $channel !~ /\AW[SKQ]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $channel =~ /\AW[SKQ]/u 1 14 15
137 14 return;
138 }
139  
140 6 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.4
141 6 my ($message) = @_;
142 6 Twilio::Exceptions::TwilioException->throw($message);
143 0 return;
144 }
145  
146 2 1;
147  
148 __END__
« Twilio/Exceptions/TwilioServiceException.pm ↑ Summary Twilio/Version.pm »