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.18'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
58367
|
use strict; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
71
|
|
7
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
65
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
12
|
use Carp qw(confess); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1447
|
|
10
|
|
|
|
|
|
|
our @CARP_NOT = qw(LWP::Authen::OAuth2); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub from_ref { |
14
|
3
|
|
|
3
|
1
|
668
|
my ($class, $data) = @_; |
15
|
|
|
|
|
|
|
# If create_time is passed, then that will overwrite this default. |
16
|
3
|
|
|
|
|
22
|
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
|
19
|
my $self = shift; |
28
|
14
|
|
50
|
|
|
27
|
my $initial_expires_in = $self->{expires_in} || 3600; |
29
|
14
|
|
|
|
|
36
|
return $self->{create_time} + $initial_expires_in; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub expires_in { |
34
|
11
|
|
|
11
|
1
|
14
|
my $self = shift; |
35
|
11
|
|
|
|
|
20
|
return $self->expires_time - time(); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub should_refresh { |
40
|
4
|
|
|
4
|
1
|
8
|
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
|
|
|
|
10
|
if ($self->expires_in/2 < $early_refresh_time) { |
44
|
3
|
|
|
|
|
7
|
$early_refresh_time = $self->expires_in/2; |
45
|
|
|
|
|
|
|
} |
46
|
4
|
|
|
|
|
9
|
my $expires_in = $self->expires_in(); |
47
|
4
|
100
|
|
|
|
11
|
if ($expires_in < $early_refresh_time) { |
48
|
3
|
|
|
|
|
8
|
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__ |