Twilio/Jwt/ClientToken.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 6 / 6 Conditioncond 70.0% 7 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 92.7% 89 / 96 CC 8 SCAR 8.8
CC
8
Cov
96%
allow_client_incoming
5.0
_with_client_name
3.0
generate_token
2.0
CRAP 2.4
« Twilio/Http/BearerToken/ApiTokenManager.pm ↑ Summary Twilio/Jwt/Grants/Grant.pm »
1 package Twilio::Jwt::ClientToken;
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::Client::ScopeURI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Twilio::Jwt::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '2.001';
11  
12 2 my $DEFAULT_TTL = 3600;
13 2 my $EVENTS_PATH = '/2010-04-01/Events';
14  
15 18 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 18 my ($class, $account_sid, $auth_token) = @_;
17  
18 18 return bless {
19 account_sid => $account_sid,
20 auth_token => $auth_token,
21 scopes => [],
22 claims => {},
23 }, $class;
24 }
25  
26 9 ▶sub allow_client_incoming {
Subroutine allow_client_incoming: called CC=5 SCAR=16.2
Pod documented
27 9 my ($self, $client_name) = @_;
28  
29 9 ▶ if (($client_name // q{}) =~ /[^A-Za-z0-9_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($client_name // "") =~ /[^A-Za-z0-9_]/u) 3 6 9
Condition50%1/2
$client_name // ""
l!l
90
MC/DC0%0/1
$client_name // ""
$client_name
Truth table50%1/2
A
A= $client_name
Aresult
11
00
30 3 Twilio::Exceptions::TwilioException->throw(
31 'Only alphanumeric characters allowed in client name.');
32 }
33 6 ▶ if (!length($client_name // q{})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length($client_name // "")) 1 5 6
Condition50%1/2
$client_name // ""
l!l
60
MC/DC0%0/1
$client_name // ""
$client_name
Truth table50%1/2
A
A= $client_name
Aresult
11
00
34 1 Twilio::Exceptions::TwilioException->throw('Client name must not be a zero length string.');
35 }
36  
37 5 $self->{client_name} = $client_name;
38 5 $self->_allow('client', 'incoming', [[clientName => $client_name]]);
39 5 return;
40 }
41  
42 5 ▶sub allow_client_outgoing {
Subroutine allow_client_outgoing: called CC=1 SCAR=0.0
Pod documented
43 5 my ($self, $application_sid, $params) = @_;
44  
45 5 my $query = Twilio::Jwt::Client::ScopeURI->build_query($params);
46 5 $self->_allow('client', 'outgoing', [[appSid => $application_sid], [appParams => $query]]);
47 5 return;
48 }
49  
50 3 ▶sub allow_event_stream {
Subroutine allow_event_stream: called CC=1 SCAR=0.0
Pod documented
51 3 my ($self, $filters) = @_;
52  
53 3 my $query = Twilio::Jwt::Client::ScopeURI->build_query($filters);
54 3 $self->_allow('stream', 'subscribe', [[path => $EVENTS_PATH], [params => $query]]);
55 3 return;
56 }
57  
58 2 ▶sub add_claim {
Subroutine add_claim: called CC=1 SCAR=0.0
Pod documented
59 2 my ($self, $name, $value) = @_;
60 2 $self->{claims}{$name} = $value;
61 2 return;
62 }
63  
64 14 ▶sub generate_token {
Subroutine generate_token: called CC=2 SCAR=6.9
Pod documented
65 14 my ($self, $ttl) = @_;
66  
67 14 my $now = time;
68  
69 # An outgoing call has to say who it comes from, and that may be set after it is allowed.
70 14 my @scopes = map { $self->_with_client_name($_)->to_string } @{ $self->{scopes} };
71  
72 my %payload = (
73 14 ▶ %{ $self->{claims} },
Condition66%2/3
$ttl // $DEFAULT_TTL
l!l&&r!l&&!r
1130
MC/DC0%0/2
$ttl // $DEFAULT_TTL
$ttl $DEFAULT_TTL
Truth table66%2/3
A // B
A= $ttlB= $DEFAULT_TTL
ABresult
1X1
011
000
74 scope => join(q{ }, @scopes),
75 iss => $self->{account_sid},
76 14 exp => $now + ($ttl // $DEFAULT_TTL),
77 );
78 14 return Twilio::Jwt::JWT->encode(\%payload, $self->{auth_token});
79 }
80  
81 13 ▶sub _allow {
Subroutine _allow: called CC=1 SCAR=0.0
82 13 my ($self, $service, $privilege, $params) = @_;
83 13 push @{ $self->{scopes} }, Twilio::Jwt::Client::ScopeURI->new($service, $privilege, $params);
84 13 return;
85 }
86  
87 13 ▶sub _with_client_name {
Subroutine _with_client_name: called CC=3 SCAR=11.0
88 13 my ($self, $scope) = @_;
89  
90 13 ▶ if ($scope->get_privilege eq 'outgoing' && defined $self->{client_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->get_privilege eq "outgoing" and defined $self->{"client_name"}) 2 11 13
Condition100%3/3
$scope->get_privilege eq "outgoing" and defined $self->{"client_name"}
!ll&&!rl&&r
832
MC/DC100%2/2
$scope->get_privilege eq "outgoing" and defined $self->{"client_name"}
$scope->get_privilege eq "outgoing" defined $self->{"client_name"}
Truth table100%3/3
A and B
A= $scope->get_privilege eq "outgoing"B= defined $self->{"client_name"}
ABresult
0X0
100
111
91 2 $scope->add_param(clientName => $self->{client_name});
92 }
93 13 return $scope;
94 }
95  
96 2 1;
97  
98 __END__
« Twilio/Http/BearerToken/ApiTokenManager.pm ↑ Summary Twilio/Jwt/Grants/Grant.pm »