AccessToken.pm

filter: Statementstmt 80.0% 60 / 75 Branchbran 50.0% 14 / 28 Conditioncond 35.0% 7 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 81.8% 9 / 11 Podpod 100% 3 / 3 totaltotal 63.2% 93 / 147 CC 25 SCAR 29.9
CC
25
Cov
66%
sync_with_store
36.8
valid_access_token
13.7
to_string
12.0
CRAP 19.9
« Client.pm ↑ Summary
1 package Net::OAuth2::Moosey::AccessToken;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 =head1 NAME
4  
5 Net::OAuth2::Moosey::AccessToken - OAuth 2.0 Access token object
6  
7 =head1 VERSION
8  
9 0.01
10  
11 =cut
12  
13 2 our $VERSION = '0.01';
14  
15 =head1 SYNOPSIS
16  
17 Used by Net::OAuth2::Moosey::Client.
18
19 my $client = Net::OAuth2::Moosey::Client->new( %client_params );
20 my $token_object = $client->access_token_object();
21 my $access_token = $token_object->valid_access_token();
22  
23 =cut
24  
25 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use YAML qw/LoadFile DumpFile Dump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use MooseX::Types::URI qw(Uri FileUri DataUri);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =head1 METHODS
33  
34 =head2 new
35  
36 =head3 ATTRIBUTES
37  
38 =over 2
39  
40 =item user_agent <LWP::UserAgent>
41  
42 See L<Net::OAuth2::Moosey::Client>
43  
44 =item client_id <Str>
45  
46 See L<Net::OAuth2::Moosey::Client>
47  
48 =item client_secret <Str>
49  
50 See L<Net::OAuth2::Moosey::Client>
51  
52 =item access_token_url <Uri>
53  
54 See L<Net::OAuth2::Moosey::Client>
55  
56 =item access_token <Str>
57  
58 The access token
59  
60 =item refresh_token <Str>
61  
62 The refresh token
63  
64 =item token_store <Str>
65  
66 See L<Net::OAuth2::Moosey::Client>
67  
68 =item token_type <Str>
69  
70 The token type as returned by your service provider.
71  
72 =item expires_at <Int>
73  
74 The unix timestamp when the access token will expire
75  
76 =item expires_in <Int>
77  
78 Seconds until the access token will expire. Only really useful for setting an expiry time in the future,
79 then use expires_at to test against.
80  
81 =back
82  
83 =cut
84 2 has 'user_agent' => ( is => 'ro', isa => 'LWP::UserAgent', required => 1 );
85 2 has 'client_id' => ( is => 'ro', isa => 'Str', );
86 2 has 'client_secret' => ( is => 'ro', isa => 'Str', );
87 2 has 'access_token_url' => ( is => 'ro', isa => Uri, required => 1 );
88 #TODO: RCL 2011-10-03 has client_id, client_secret, access_token_url
89 2 has 'access_token' => ( is => 'rw', isa => 'Str' );
90 2 has 'refresh_token' => ( is => 'rw', isa => 'Str' );
91 2 has 'token_store' => ( is => 'rw', isa => 'Str' );
92 2 has 'token_type' => ( is => 'rw', isa => 'Str' );
93 2 has 'expires_at' => ( is => 'rw', isa => 'Int' );
94 has 'expires_in' => ( is => 'rw', isa => 'Int',
95 0 ▶ trigger => sub{ $_[0]->expires_at( time() + $_[1] ) }, # Trust the expires_in more than expires_at
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 # TODO: RCL 2011-09-05 Consider subtracting a safety-buffer here for data transfer time so that
97 # we always refresh the token before it expires
98 2 );
99  
100  
101 =head2 valid_access_token
102  
103 Returns a valid access token (refreshing if necessary)
104  
105 =cut
106 9 ▶sub valid_access_token {
Subroutine valid_access_token: called CC=10 SCAR=26.2
Pod documented
107 9 my $self = shift;
108 9 ▶ if( $self->access_token and $self->expires_at and $self->expires_at > time() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token and $self->expires_at and $self->expires_at > time) 0 9 9
Condition50%3/6
$self->access_token and $self->expires_at and $self->expires_at > time
!ll&&!rl&&r
900
$self->access_token and $self->expires_at
!ll&&!rl&&r
360
MC/DC0%0/3
$self->access_token and $self->expires_at and $self->expires_at > time
$self->access_token $self->expires_at $self->expires_at > time
Truth table50%2/4
A and B and C
A= $self->access_tokenB= $self->expires_atC= $self->expires_at > time
ABCresult
0XX0
10X0
1100
1111
109 0 return $self->access_token;
110 }
111 9 ▶ if( not $self->refresh_token ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->refresh_token) 0 9 9
112 0 croak( "Cannot refresh access_token without refresh_token" );
113 }
114  
115 # This is knitted specifically to Googles OAuth2 implementation - is it universal?
116 9 my $headers = HTTP::Headers->new( Content_Type => 'application/x-www-form-urlencoded' );
117 9 my $content = sprintf( "client_id=%s&" .
118 "client_secret=%s&" .
119 "refresh_token=%s&" .
120 "grant_type=refresh_token",
121 $self->client_id,
122 $self->client_secret,
123 $self->refresh_token,
124 );
125  
126 9 my $request = HTTP::Request->new(
127 'POST' => $self->access_token_url,
128 $headers,
129 $content,
130 );
131 9 my $response = $self->user_agent->request( $request );
132 9 ▶ if( not $response->is_success() ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 9 9
133 0 print Dump( $response );
134 0 croak( sprintf "Could not refresh access token: %s/%s", $response->code, $response->title );
135 }
136 9 ▶ my $obj = eval{local $SIG{__DIE__}; decode_json($response->decoded_content)} || {};
Condition50%1/2
eval { do { local $SIG{'__DIE__'}; &decode_json(scalar $response->decoded_content) } } || {}
l!l
90
MC/DC0%0/1
eval { do { local $SIG{'__DIE__'}; &decode_json(scalar $response->decoded_content) } } || {}
eval { do { local $SIG{'__DIE__'}; &decode_json(scalar $response->decoded_content) } }
Truth table50%1/2
A
A= eval { do { local $SIG{'__DIE__'}; &decode_json(scalar $response->decoded_content) } }
Aresult
11
00
137 9 ▶ if( not $obj->{access_token} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->{"access_token"}) 0 9 9
138 0 croak( "No access token found in data...\n" . $response->decoded_content );
139 }
140 9 foreach( qw/access_token token_type expires_in/ ){
141 27 ▶ $self->$_( $obj->{$_} ) if $obj->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$_} 18 9 27
142 }
143 9 $self->sync_with_store();
144 9 return $self->access_token;
145 }
146  
147  
148 =head2 to_string
149  
150 Create a string representation of this token
151  
152 =cut
153 0 ▶sub to_string {
Subroutine to_string: not called CC=3 SCAR=24.8
Pod documented
154 0 my $self = shift;
155 0 my %hash;
156 0 for (qw/access_token token_type refresh_token expires_in expires_at error error_desription error_uri state/) {
157 0 ▶ $hash{$_} = $self->{$_} if defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$_} 0 0 0
158 }
159 0 return encode_json(\%hash);
160 }
161  
162 =head2 sync_with_store
163  
164 Sync with the store file.
165 First reads out the current data from the file, and then compares the current known data with that found
166 in the file to identify which is the freshest.
167 Writes the resulting freshest back to the store again for reuse next time.
168  
169 =cut
170 12 ▶sub sync_with_store {
Subroutine sync_with_store: called CC=14 SCAR=36.1
Pod documented
171 12 my $self = shift;
172 12 ▶ if( not $self->token_store ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token_store) 0 12 12
173 0 return;
174 }
175 12 my $data;
176 12 ▶ if( -f $self->token_store ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->token_store) 12 0 12
177 12 $data = LoadFile( $self->token_store );
178 }
179 12 ▶ if( my $old_node = $data->{ $self->client_id } ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $old_node = $$data{$self->client_id}) 9 3 12
180 9 ▶ if( $old_node->{expires_at}
Branch: 1 branch
BranchTrueFalseTotal
if ($old_node->{"expires_at"} and time < $old_node->{"expires_at"} and !$self->expires_at || $self->expires_at < $old_node->{"expires_at"}) 0 9 9
Condition22%2/9
$old_node->{"expires_at"} and time < $old_node->{"expires_at"} and !$self->expires_at || $self->expires_at < $old_node->{"expires_at"}
!ll&&!rl&&r
900
$old_node->{"expires_at"} and time < $old_node->{"expires_at"}
!ll&&!rl&&r
900
not $self->expires_at or $self->expires_at < $old_node->{"expires_at"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$old_node->{"expires_at"} and time < $old_node->{"expires_at"} and !$self->expires_at || $self->expires_at < $old_node->{"expires_at"}
$old_node->{"expires_at"} time < $old_node->{"expires_at"} not $self->expires_at $self->expires_at < $old_node->{"expires_at"}
Truth table20%1/5
A and B and not(C) or D
A= $old_node->{"expires_at"}B= time < $old_node->{"expires_at"}C= not $self->expires_atD= $self->expires_at < $old_node->{"expires_at"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
181 and time() < $old_node->{expires_at}
182 and (
183 not $self->expires_at or $self->expires_at < $old_node->{expires_at} ) )
184 {
185 0 $self->expires_at( $old_node->{expires_at} );
186 0 ▶ $self->access_token( $old_node->{access_token} ) if $old_node->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
if $old_node->{"access_token"} 0 0 0
187 0 ▶ $self->token_type( $old_node->{token_type} ) if $old_node->{token_type};
Branch: 1 branch
BranchTrueFalseTotal
if $old_node->{"token_type"} 0 0 0
188 }
189 9 ▶ $self->refresh_token( $old_node->{refresh_token} ) if( not $self->refresh_token and $old_node->{refresh_token} );
Branch: 1 branch
BranchTrueFalseTotal
if not $self->refresh_token and $old_node->{"refresh_token"} 0 9 9
Condition33%1/3
not $self->refresh_token and $old_node->{"refresh_token"}
!ll&&!rl&&r
900
MC/DC0%0/2
not $self->refresh_token and $old_node->{"refresh_token"}
not $self->refresh_token $old_node->{"refresh_token"}
Truth table33%1/3
not(A) and B
A= not $self->refresh_tokenB= $old_node->{"refresh_token"}
ABresult
0X0
100
111
190 }
191 12 foreach( qw/refresh_token access_token expires_at token_type/ ){
192 48 ▶ $data->{ $self->client_id }->{$_} = $self->$_ if $self->$_;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_ 30 18 48
193 }
194 # TODO: RCL 2011-11-03 Only write to file if the content is different to what was in the file
195 12 DumpFile( $self->token_store, $data );
196 }
197  
198 =head1 NAME
199  
200 Net::OAuth2::Moosey::AccessToken - OAuth Access Token
201  
202 =head1 SEE ALSO
203  
204 L<Net::OAuth::Moosey::Client>
205  
206 =head1 LICENSE AND COPYRIGHT
207  
208 Copyright 2011 Robin Clarke
209  
210 This program is free software; you can redistribute it and/or modify it
211 under the terms of either: the GNU General Public License as published
212 by the Free Software Foundation; or the Artistic License.
213  
214 See http://dev.perl.org/licenses/ for more information.
215  
216 =cut
217  
218  
219 2 1;
« Client.pm ↑ Summary