File Coverage

lib/LWP/Authen/OAuth2/AccessToken.pm
Criterion Covered Total %
statement 23 45 51.1
branch 4 16 25.0
condition 1 13 7.6
subroutine 7 12 58.3
pod 8 8 100.0
total 43 94 45.7


line stmt bran cond sub pod time code
1             package LWP::Authen::OAuth2::AccessToken;
2              
3             # ABSTRACT: Access tokens for OAuth2.
4             our $VERSION = '0.20'; # VERSION
5              
6 3     3   72334 use strict;
  3         19  
  3         89  
7 3     3   32 use warnings;
  3         7  
  3         87  
8              
9 3     3   15 use Carp qw(confess);
  3         9  
  3         1814  
10             our @CARP_NOT = qw(LWP::Authen::OAuth2);
11              
12              
13             sub from_ref {
14 3     3 1 949 my ($class, $data) = @_;
15             # If create_time is passed, then that will overwrite this default.
16 3         27 return bless {create_time => time(), %$data}, $class;
17             }
18              
19              
20             sub to_ref {
21 0     0 1 0 my $self = shift;
22 0         0 return { %$self };
23             }
24              
25              
26             sub expires_time {
27 14     14 1 23 my $self = shift;
28 14   50     34 my $initial_expires_in = $self->{expires_in} || 3600;
29 14         45 return $self->{create_time} + $initial_expires_in;
30             }
31              
32              
33             sub expires_in {
34 11     11 1 21 my $self = shift;
35 11         24 return $self->expires_time - time();
36             }
37              
38              
39             sub should_refresh {
40 4     4 1 11 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 100       13 if ($self->expires_in/2 < $early_refresh_time) {
44 3         8 $early_refresh_time = $self->expires_in/2;
45             }
46 4         11 my $expires_in = $self->expires_in();
47 4 100       19 if ($expires_in < $early_refresh_time) {
48 3         10 return 1;
49             }
50             else {
51 1         7 return 0;
52             }
53             }
54              
55              
56             sub for_refresh {
57 0     0 1   my $self = shift;
58 0 0         if ($self->{refresh_token}) {
59 0           return refresh_token => $self->{refresh_token};
60             }
61             else {
62 0           return ();
63             }
64             }
65              
66              
67             sub copy_refresh_from {
68 0     0 1   my ($self, $other) = @_;
69 0 0         if ($other->{refresh_token}) {
70 0   0       $self->{refresh_token} ||= $other->{refresh_token};
71             }
72             }
73              
74              
75             sub request {
76             # Shift off one for easy redispatch to _request.
77 0     0 1   my $self = shift;
78 0           my ($oauth2, $request, @rest) = @_;
79 0 0 0       if (
      0        
80             $self->should_refresh($oauth2->{early_refresh_time} || 300) and
81             $oauth2->can_refresh_tokens()
82             ) {
83 0           $oauth2->refresh_access_token();
84 0 0         $self = $oauth2->access_token if ref($oauth2->access_token);
85             }
86 0           my ($response, $try_refresh) = $self->_request(@_);
87 0 0 0       if ($try_refresh and $oauth2->can_refresh_tokens()) {
88             # Someone's clock is wrong? Try to refresh.
89 0           $oauth2->refresh_access_token();
90 0 0         if ($self->expires_in < $oauth2->access_token->expires_in) {
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             sub _request {
100 0     0     my ($self, $oauth2, $request, @rest) = @_;
101             # ...
102             # return ($response, $try_refresh);
103 0           confess("Method _request needs to be overwritten.");
104             }
105              
106             1;
107              
108             __END__