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