Twilio/AuthStrategy/TokenAuthStrategy.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 12 / 12 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 100% 83 / 83 CC 10 SCAR 11.3
CC
10
Cov
100%
_expires_at
6.0
fetch_token
3.0
is_token_expired
3.0
CRAP 3.1
« Twilio/TokenPaginationPage.pm ↑ Summary Twilio.pm »
1 package Twilio::AuthStrategy::TokenAuthStrategy;
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 parent 'Twilio::AuthStrategy::AuthStrategy';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use MIME::Base64 qw(decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Twilio::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '2.001';
13  
14 # A token that is about to expire could expire on its way to Twilio, so it is replaced this
15 # many seconds early, as the official libraries do.
16 3 my $EXPIRY_BUFFER_SECONDS = 30;
17  
18 19 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 19 my ($class, $token_manager) = @_;
20 19 my $self = $class->SUPER::new('token');
21 19 $self->{token_manager} = $token_manager;
22 19 $self->{token} = undef;
23 19 return $self;
24 }
25  
26 23 ▶sub get_auth_string {
Subroutine get_auth_string: called CC=1 SCAR=0.0
Pod documented
27 23 my ($self, $client) = @_;
28 23 return 'Bearer ' . $self->fetch_token($client);
29 }
30  
31 25 ▶sub requires_authentication { return 1 }
Subroutine requires_authentication: called CC=1 SCAR=0.0
Pod documented
32  
33 23 ▶sub fetch_token {
Subroutine fetch_token: called CC=3 SCAR=11.0
Pod documented
34 23 my ($self, $client) = @_;
35 23 ▶ if (!defined $self->{token} || $self->is_token_expired($self->{token})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"token"} or $self->is_token_expired($self->{"token"})) 19 4 23
Condition100%3/3
not defined $self->{"token"} or $self->is_token_expired($self->{"token"})
l!l&&r!l&&!r
1724
MC/DC100%2/2
not defined $self->{"token"} or $self->is_token_expired($self->{"token"})
not defined $self->{"token"} $self->is_token_expired($self->{"token"})
Truth table100%3/3
not(A) or B
A= not defined $self->{"token"}B= $self->is_token_expired($self->{"token"})
ABresult
1X1
011
000
36 19 $self->{token} = $self->{token_manager}->fetch_token($client);
37 }
38 20 return $self->{token};
39 }
40  
41 # twilio-php keeps a token whose expiry it can't read until Twilio refuses it. Like
42 # twilio-python, this asks for a new one instead, which costs a request but never fails one.
43 19 ▶sub is_token_expired {
Subroutine is_token_expired: called CC=3 SCAR=11.0
Pod documented
44 19 my ($self, $token) = @_;
45 19 my $expires_at = _expires_at($token);
46 19 ▶ return 1 if !defined $expires_at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expires_at 9 10 19
47 10 ▶ return $expires_at - $EXPIRY_BUFFER_SECONDS <= time ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$expires_at - $EXPIRY_BUFFER_SECONDS <= time ? : 3 7 10
48 }
49  
50 19 ▶sub _expires_at {
Subroutine _expires_at: called CC=6 SCAR=17.9
51 19 my ($token) = @_;
52 19 ▶ my (undef, $payload) = split /[.]/, $token // q{};
Condition100%2/2
$token // ""
l!l
181
MC/DC100%1/1
$token // ""
$token
Truth table100%2/2
A
A= $token
Aresult
11
00
53 19 ▶ return if !defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 4 15 19
54  
55 15 my $claims = eval { Twilio::Json::decode(decode_base64url($payload)) };
56 15 ▶ return if ref $claims ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $claims ne "HASH" 2 13 15
57  
58 13 my $exp = $claims->{exp};
59 13 ▶ return if !defined $exp || $exp !~ /\A[0-9]+(?:[.][0-9]+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exp and $exp =~ /\A[0-9]+(?:[.][0-9]+)?\z/u 3 10 13
Condition100%3/3
defined $exp and $exp =~ /\A[0-9]+(?:[.][0-9]+)?\z/u
!ll&&!rl&&r
1210
MC/DC100%2/2
defined $exp and $exp =~ /\A[0-9]+(?:[.][0-9]+)?\z/u
defined $exp $exp =~ /\A[0-9]+(?:[.][0-9]+)?\z/u
Truth table100%3/3
A and B
A= defined $expB= $exp =~ /\A[0-9]+(?:[.][0-9]+)?\z/u
ABresult
0X0
100
111
60 10 return $exp;
61 }
62  
63 3 1;
64  
65 __END__
« Twilio/TokenPaginationPage.pm ↑ Summary Twilio.pm »