File Coverage

lib/Net/OAuth2/AccessToken.pm
Criterion Covered Total %
statement 15 88 17.0
branch 0 18 0.0
condition 0 12 0.0
subroutine 5 34 14.7
pod 28 29 96.5
total 48 181 26.5


line stmt bran cond sub pod time code
1             # Copyrights 2013-2019 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution Net-OAuth2. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Net::OAuth2::AccessToken;
10 4     4   467 use vars '$VERSION';
  4         8  
  4         285  
11             $VERSION = '0.67';
12              
13 4     4   25 use warnings;
  4         8  
  4         113  
14 4     4   18 use strict;
  4         8  
  4         116  
15              
16             our $VERSION; # to be able to test in devel environment
17              
18 4     4   18 use Carp qw(croak);
  4         8  
  4         244  
19 4     4   28 use JSON::MaybeXS qw/encode_json/;
  4         28  
  4         4973  
20              
21             # Attributes to be saved to preserve the session.
22             my @session = qw/access_token token_type refresh_token expires_at
23             scope state auto_refresh/;
24              
25             # This class name is kept for backwards compatibility: a better name
26             # would have been: Net::OAuth2::Session, with a ::Token::Bearer split-off.
27              
28             # In the future, most of this functionality will probably need to be
29             # split-off in a base class ::Token, to be shared with a new extension
30             # which supports HTTP-MAC tokens as proposed by ietf dragt
31             # http://datatracker.ietf.org/doc/draft-ietf-oauth-v2-http-mac/
32              
33              
34 0     0 1   sub new(@) { my $class = shift; (bless {}, $class)->init({@_}) }
  0            
35              
36             sub init($)
37 0     0 0   { my ($self, $args) = @_;
38              
39             $self->{NOA_expires_at} = $args->{expires_at}
40 0   0       || ($args->{expires_in} ? time()+$args->{expires_in} : undef);
41              
42             # client is the pre-v0.50 name
43             my $profile = $self->{NOA_profile} = $args->{profile} || $args->{client}
44 0 0 0       or croak "::AccessToken needs profile object";
45              
46 0           $self->{NOA_access_token} = $args->{access_token};
47 0           $self->{NOA_refresh_token} = $args->{refresh_token};
48 0           $self->{NOA_refresh_always}= $args->{refresh_always};
49 0           $self->{NOA_scope} = $args->{scope};
50 0           $self->{NOA_state} = $args->{state};
51 0           $self->{NOA_hd} = $args->{hd};
52 0           $self->{NOA_token_type} = $args->{token_type};
53 0           $self->{NOA_auto_refresh} = $args->{auto_refresh};
54 0           $self->{NOA_changed} = $args->{changed};
55              
56 0           $self->{NOA_error} = $args->{error};
57 0           $self->{NOA_error_uri} = $args->{error_uri};
58 0   0       $self->{NOA_error_descr} = $args->{error_description} || $args->{error};
59              
60 0           $self->{NOA_attr} = $args;
61 0           $self;
62             }
63              
64              
65             sub session_thaw($%)
66 0     0 1   { my ($class, $session) = (shift, shift);
67             # we can use $session->{net_oauth2_version} to upgrade the info
68 0           $class->new(%$session, @_);
69             }
70              
71             #--------------
72              
73 0     0 1   sub token_type() {shift->{NOA_token_type}}
74 0     0 1   sub scope() {shift->{NOA_scope}}
75 0     0 1   sub state() {shift->{NOA_state}}
76 0     0 1   sub hd() {shift->{NOA_hd}}
77 0     0 1   sub profile() {shift->{NOA_profile}}
78              
79              
80 0     0 1   sub attribute($) { $_[0]->{NOA_attr}{$_[1]} }
81              
82              
83             sub changed(;$)
84 0 0   0 1   { my $s = shift; @_ ? $s->{NOA_changed} = shift : $s->{NOA_changed} }
  0            
85              
86              
87             sub access_token()
88 0     0 1   { my $self = shift;
89              
90 0 0         if($self->expired)
    0          
91 0           { delete $self->{NOA_access_token};
92 0           $self->{NOA_changed} = 1;
93 0 0         $self->refresh if $self->auto_refresh;
94             }
95             elsif($self->refresh_always)
96 0           { $self->refresh;
97             }
98              
99 0           $self->{NOA_access_token};
100             }
101              
102             #---------------
103              
104 0     0 1   sub error() {shift->{NOA_error}}
105 0     0 1   sub error_uri() {shift->{NOA_error_uri}}
106 0     0 1   sub error_description() {shift->{NOA_error_descr}}
107              
108             #---------------
109              
110 0     0 1   sub refresh_token() {shift->{NOA_refresh_token}}
111 0     0 1   sub refresh_always() {shift->{NOA_refresh_always}}
112 0     0 1   sub auto_refresh() {shift->{NOA_auto_refresh}}
113              
114              
115 0     0 1   sub expires_at() { shift->{NOA_expires_at} }
116              
117              
118 0     0 1   sub expires_in() { shift->expires_at - time() }
119              
120              
121             sub expired(;$)
122 0     0 1   { my ($self, $after) = @_;
123 0 0         my $when = $self->expires_at or return;
124 0 0         $after = 15 unless defined $after;
125 0           $when < time() + $after;
126             }
127              
128              
129             sub update_token($$$;$)
130 0     0 1   { my ($self, $token, $type, $exp, $refresh) = @_;
131 0           $self->{NOA_access_token} = $token;
132 0 0         $self->{NOA_token_type} = $type if $type;
133 0           $self->{NOA_expires_at} = $exp;
134              
135 0 0         $self->{NOA_refresh_token} = $refresh
136             if defined $refresh;
137              
138 0           $token;
139             }
140              
141             #--------------
142              
143             sub to_json()
144 0     0 1   { my $self = shift;
145 0           encode_json $self->session_freeze;
146             }
147             *to_string = \&to_json; # until v0.50
148              
149              
150             sub session_freeze(%)
151 0     0 1   { my ($self, %args) = @_;
152 0           my %data = (net_oauth2_version => $VERSION);
153 0   0       defined $self->{"NOA_$_"} && ($data{$_} = $self->{"NOA_$_"}) for @session;
154 0           $self->changed(0);
155 0           \%data;
156             }
157              
158              
159             sub refresh()
160 0     0 1   { my $self = shift;
161 0           $self->profile->update_access_token($self);
162             }
163              
164             #--------------
165              
166 0     0 1   sub request{ my $s = shift; $s->profile->request_auth($s, @_) }
  0            
167 0     0 1   sub get { my $s = shift; $s->profile->request_auth($s, 'GET', @_) }
  0            
168 0     0 1   sub post { my $s = shift; $s->profile->request_auth($s, 'POST', @_) }
  0            
169 0     0 1   sub delete { my $s = shift; $s->profile->request_auth($s, 'DELETE', @_) }
  0            
170 0     0 1   sub put { my $s = shift; $s->profile->request_auth($s, 'PUT', @_) }
  0            
171              
172             1;