OAuth2/AccessToken.pm

filter: Statementstmt 54.1% 26 / 48 Branchbran 25.0% 4 / 16 Conditioncond 7.6% 1 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 58.3% 7 / 12 Podpod 100% 8 / 8 totaltotal 43.8% 46 / 105 CC 14 SCAR 32.5
CC
14
Cov
40%
request
72.0
copy_refresh_from
12.0
for_refresh
6.0
CRAP 25.8
« OAuth2.pm ↑ Summary OAuth2/AccessToken/Bearer.pm »
1 package LWP::Authen::OAuth2::AccessToken;
2  
3 # ABSTRACT: Access tokens for OAuth2.
4 6 our $VERSION = '0.20'; # VERSION
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 our @CARP_NOT = qw(LWP::Authen::OAuth2);
11  
12  
13 3 ▶sub from_ref {
Subroutine from_ref: called CC=1 SCAR=0.0
Pod documented
14 3 my ($class, $data) = @_;
15 # If create_time is passed, then that will overwrite this default.
16 3 return bless {create_time => time(), %$data}, $class;
17 }
18  
19  
20 0 ▶sub to_ref {
Subroutine to_ref: not called CC=1 SCAR=6.9
Pod documented
21 0 my $self = shift;
22 0 return { %$self };
23 }
24  
25  
26 14 ▶sub expires_time {
Subroutine expires_time: called CC=2 SCAR=7.1
Pod documented
27 14 my $self = shift;
28 14 ▶ my $initial_expires_in = $self->{expires_in} || 3600;
Condition50%1/2
$self->{'expires_in'} || 3600
l!l
140
MC/DC0%0/1
$self->{'expires_in'} || 3600
$self->{'expires_in'}
Truth table50%1/2
A
A= $self->{'expires_in'}
Aresult
11
00
29 14 return $self->{create_time} + $initial_expires_in;
30 }
31  
32  
33 11 ▶sub expires_in {
Subroutine expires_in: called CC=1 SCAR=0.0
Pod documented
34 11 my $self = shift;
35 11 return $self->expires_time - time();
36 }
37  
38  
39 4 ▶sub should_refresh {
Subroutine should_refresh: called CC=3 SCAR=11.0
Pod documented
40 4 my ($self, $early_refresh_time) = @_;
41 # If the access tokens are short lived relative to $early_refresh_time
42 # we cheat to avoid refreshing TOO often....
43 4 ▶ if ($self->expires_in/2 < $early_refresh_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expires_in / 2 < $early_refresh_time) 3 1 4
44 3 $early_refresh_time = $self->expires_in/2;
45 }
46 4 my $expires_in = $self->expires_in();
47 4 ▶ if ($expires_in < $early_refresh_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires_in < $early_refresh_time) { } 3 1 4
48 3 return 1;
49 }
50 else {
51 1 return 0;
52 }
53 }
54  
55  
56 0 ▶sub for_refresh {
Subroutine for_refresh: not called CC=2 SCAR=17.9
Pod documented
57 0 my $self = shift;
58 0 ▶ if ($self->{refresh_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'refresh_token'}) { } 0 0 0
59 0 return refresh_token => $self->{refresh_token};
60 }
61 else {
62 0 return ();
63 }
64 }
65  
66  
67 0 ▶sub copy_refresh_from {
Subroutine copy_refresh_from: not called CC=3 SCAR=24.8
Pod documented
68 0 my ($self, $other) = @_;
69 0 ▶ if ($other->{refresh_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other->{"refresh_token"}) 0 0 0
70 0 ▶ $self->{refresh_token} ||= $other->{refresh_token};
Condition0%0/3
$self->{"refresh_token"} ||= $other->{"refresh_token"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"refresh_token"} ||= $other->{"refresh_token"}
$self->{"refresh_token"} $other->{"refresh_token"}
Truth table0%0/3
A ||= B
A= $self->{"refresh_token"}B= $other->{"refresh_token"}
ABresult
1X1
011
000
71 }
72 }
73  
74  
75 0 ▶sub request {
Subroutine request: not called CC=8 SCAR=42.8
Pod documented
76 # Shift off one for easy redispatch to _request.
77 0 my $self = shift;
78 0 my ($oauth2, $request, @rest) = @_;
79 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_refresh($oauth2->{"early_refresh_time"} || 300) and $oauth2->can_refresh_tokens) 0 0 0
Condition0%0/5
$self->should_refresh($oauth2->{"early_refresh_time"} || 300) and $oauth2->can_refresh_tokens
!ll&&!rl&&r
000
$oauth2->{'early_refresh_time'} || 300
l!l
00
MC/DC0%0/2
$self->should_refresh($oauth2->{"early_refresh_time"} || 300) and $oauth2->can_refresh_tokens
$self->should_refresh($oauth2->{"early_refresh_time"} || 300) $oauth2->can_refresh_tokens
MC/DC0%0/1
$oauth2->{'early_refresh_time'} || 300
$oauth2->{'early_refresh_time'}
Truth table0%0/3
A and B
A= $self->should_refresh($oauth2->{"early_refresh_time"} || 300)B= $oauth2->can_refresh_tokens
ABresult
0X0
100
111
Truth table0%0/2
A
A= $oauth2->{'early_refresh_time'}
Aresult
11
00
80 $self->should_refresh($oauth2->{early_refresh_time} || 300) and
81 $oauth2->can_refresh_tokens()
82 ) {
83 0 $oauth2->refresh_access_token();
84 0 ▶ $self = $oauth2->access_token if ref($oauth2->access_token);
Branch: 1 branch
BranchTrueFalseTotal
if ref $oauth2->access_token 0 0 0
85 }
86 0 my ($response, $try_refresh) = $self->_request(@_);
87 0 ▶ if ($try_refresh and $oauth2->can_refresh_tokens()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try_refresh and $oauth2->can_refresh_tokens) 0 0 0
Condition0%0/3
$try_refresh and $oauth2->can_refresh_tokens
!ll&&!rl&&r
000
MC/DC0%0/2
$try_refresh and $oauth2->can_refresh_tokens
$try_refresh $oauth2->can_refresh_tokens
Truth table0%0/3
A and B
A= $try_refreshB= $oauth2->can_refresh_tokens
ABresult
0X0
100
111
88 # Someone's clock is wrong? Try to refresh.
89 0 $oauth2->refresh_access_token();
90 0 ▶ if ($self->expires_in < $oauth2->access_token->expires_in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expires_in < $oauth2->access_token->expires_in) 0 0 0
91 # We seem to have renewed, try again.
92 0 ($response, $try_refresh) = $oauth2->access_token->_request(@_);
93 }
94 }
95 0 return $response;
96 }
97  
98  
99 0 ▶sub _request {
Subroutine _request: not called CC=1 SCAR=6.9
100 0 my ($self, $oauth2, $request, @rest) = @_;
101 # ...
102 # return ($response, $try_refresh);
103 0 confess("Method _request needs to be overwritten.");
104 }
105  
106 6 1;
107  
108 __END__
« OAuth2.pm ↑ Summary OAuth2/AccessToken/Bearer.pm »