| 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
Condition100%3/3
not defined $self->{"token"} or $self->is_token_expired($self->{"token"})
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"})
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | 10 | ▶ | return $expires_at - $EXPIRY_BUFFER_SECONDS <= time ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 // ""
MC/DC100%1/1
$token // ""
$token
Truth table100%2/2
A
A= $token
| |||||||||||||||||||||||||||||
| 53 | 19 | ▶ | return if !defined $payload; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 15 | my $claims = eval { Twilio::Json::decode(decode_base64url($payload)) }; | |||||||||||||||||||||||||||
| 56 | 15 | ▶ | return if ref $claims ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | my $exp = $claims->{exp}; | |||||||||||||||||||||||||||
| 59 | 13 | ▶ | return if !defined $exp || $exp !~ /\A[0-9]+(?:[.][0-9]+)?\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $exp and $exp =~ /\A[0-9]+(?:[.][0-9]+)?\z/u
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
| |||||||||||||||||||||||||||||
| 60 | 10 | return $exp; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | 1; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||