OAuth2.pm

filter: Statementstmt 33.5% 64 / 191 Branchbran 5.5% 3 / 54 Conditioncond 4.7% 1 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 34.1% 14 / 41 Podpod 76.6% 23 / 30 totaltotal 30.0% 105 / 350 CC 38 SCAR 32.6
CC
38
Cov
26%
make_api_call
90.0
refresh_access_token
90.0
load_token_string
30.0
CRAP 26.0
« OAuth2/ServiceProvider.pm ↑ Summary OAuth2/AccessToken.pm »
1 package LWP::Authen::OAuth2;
2  
3 # ABSTRACT: Make requests to OAuth2 APIs.
4 9 our $VERSION = '0.20'; # VERSION
5  
6 9 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # LWP::UserAgent lazyloads these, but we always need it.
12 9 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 our @CARP_NOT = map "LWP::Authen::OAUth2::$_", qw(Args ServiceProvider);
18 9 ▶use LWP::Authen::OAuth2::Args qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 extract_option copy_option assert_options_empty
20 9 );
21 9 ▶use LWP::Authen::OAuth2::ServiceProvider;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
24 7 my ($class, %opts) = @_;
25  
26 # Constructing the service provider can consume my options.
27 7 my $service_provider = LWP::Authen::OAuth2::ServiceProvider->new(\%opts);
28 7 my $self
29 = bless {
30 service_provider => $service_provider
31 }, $service_provider->oauth2_class();
32 7 $self->init(%opts, service_provider => $service_provider);
33 7 return $self;
34 }
35  
36 7 ▶sub init {
Subroutine init: called CC=5 SCAR=16.1
Pod undocumented
37 7 my ($self , %opts) = @_;
38  
39 # Collect arguments for the service providers.
40 7 my $service_provider = $self->{service_provider};
41 7 my $for_service_provider = LWP::Authen::OAuth2::Args->new();
42 7 my %is_seen;
43 7 for my $opt (@{ $service_provider->{required_init} }) {
44 13 $is_seen{$opt}++;
45 13 $for_service_provider->copy_option(\%opts, $opt);
46 }
47 7 for my $opt (@{ $service_provider->{optional_init} }) {
48 16 ▶ if (not $is_seen{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_seen{$opt}) 15 1 16
49 15 $is_seen{$opt}++;
50 15 $for_service_provider->copy_option(\%opts, $opt, undef);
51 }
52 }
53 7 $self->{for_service_provider} = $for_service_provider;
54  
55 7 $self->copy_option(\%opts, "early_refresh_time", 300);
56 7 $self->copy_option(\%opts, "error_handler", undef);
57 7 $self->copy_option(\%opts, "is_strict", 1);
58 7 $self->copy_option(\%opts, "prerefresh", undef);
59 7 $self->copy_option(\%opts, "save_tokens", undef);
60 7 $self->copy_option(\%opts, "save_tokens_args", undef);
61 7 $self->copy_option(\%opts, "token_string", undef);
62 7 $self->copy_option(\%opts, "user_agent", undef);
63  
64 7 ▶ if ($self->{token_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_string"}) 0 7 7
65 0 $self->load_token_string();
66 }
67 }
68  
69 # Standard shortcut request methods.
70 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
71 0 my ($self, @parameters) = @_;
72 0 my @rest = $self->user_agent->_process_colonic_headers(\@parameters,1);
73 0 my $request = HTTP::Request::Common::DELETE(@parameters);
74 0 return $self->request($request, @rest);
75 }
76  
77 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
78 0 my ($self, @parameters) = @_;
79 0 my @rest = $self->user_agent->_process_colonic_headers(\@parameters,1);
80 0 my $request = HTTP::Request::Common::GET(@parameters);
81 0 return $self->request($request, @rest);
82 }
83  
84 0 ▶sub head {
Subroutine head: not called CC=1 SCAR=6.9
Pod documented
85 0 my ($self, @parameters) = @_;
86 0 my @rest = $self->user_agent->_process_colonic_headers(\@parameters,1);
87 0 my $request = HTTP::Request::Common::HEAD(@parameters);
88 0 return $self->request($request, @rest);
89 }
90  
91 0 ▶sub post {
Subroutine post: not called CC=2 SCAR=17.9
Pod documented
92 0 my ($self, @parameters) = @_;
93 0 ▶ my @rest = $self->user_agent->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
Branch: 1 branch
BranchTrueFalseTotal
ref $parameters[1] ? : 0 0 0
94 0 my $request = HTTP::Request::Common::POST(@parameters);
95 0 return $self->request($request, @rest);
96 }
97  
98 0 ▶sub put {
Subroutine put: not called CC=2 SCAR=17.9
Pod documented
99 0 my ($self, @parameters) = @_;
100 0 ▶ my @rest = $self->user_agent->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
Branch: 1 branch
BranchTrueFalseTotal
ref $parameters[1] ? : 0 0 0
101 0 my $request = HTTP::Request::Common::PUT(@parameters);
102 0 return $self->request($request, @rest);
103 }
104  
105 0 ▶sub request {
Subroutine request: not called CC=1 SCAR=6.9
Pod documented
106 0 my ($self, $request, @rest) = @_;
107 0 return $self->access_token->request($self, $request, @rest);
108 }
109  
110 # Now all of the methods that I need.
111 0 ▶sub token_string {
Subroutine token_string: not called CC=2 SCAR=17.9
Pod documented
112 0 my $self = shift;
113 0 ▶ if ($self->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'access_token'}) { } 0 0 0
114 0 my $ref = $self->{access_token}->to_ref;
115 0 $ref->{_class} = ref($self->{access_token});
116 0 return encode_json($ref);
117 }
118 else {
119 0 return undef;
120 }
121 }
122  
123 # This does the actual saving.
124 0 ▶sub _set_tokens {
Subroutine _set_tokens: not called CC=4 SCAR=30.0
125 0 my ($self, %opts) = @_;
126  
127 0 my $tokens = $self->extract_option(\%opts, "tokens");
128 0 my $skip_save = $self->extract_option(\%opts, "skip_save", 0);
129 0 assert_options_empty(\%opts);
130  
131 0 ▶ if (ref($tokens)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tokens) { } 0 0 0
132 # Assume we have tokens.
133 0 $self->{access_token} = $tokens;
134 0 ▶ if ($self->{save_tokens} and not $skip_save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_tokens"} and not $skip_save) 0 0 0
Condition0%0/3
$self->{"save_tokens"} and not $skip_save
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"save_tokens"} and not $skip_save
$self->{"save_tokens"} not $skip_save
Truth table0%0/3
A and not(B)
A= $self->{"save_tokens"}B= not $skip_save
ABresult
0X0
100
111
135 0 my $as_string = $self->token_string;
136 0 $self->{save_tokens}->($as_string, @{$self->{save_tokens_args}});
137 }
138 0 return;
139 }
140 else {
141 # Assume we have an error message.
142 0 return $self->error($tokens);
143 }
144 }
145  
146 0 ▶sub authorization_url {
Subroutine authorization_url: not called CC=2 SCAR=17.9
Pod documented
147 0 my ($self, %opts) = @_;
148  
149 # If we get here, the service provider does it.
150 0 my $url = $self->{service_provider}->authorization_url($self, %opts);
151 0 ▶ if ($url =~ / /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ / /) { } 0 0 0
152 # Assume an error.
153 0 return $self->error($url);
154 }
155 else {
156 0 return $url;
157 }
158 }
159  
160 0 ▶sub api_url_base {
Subroutine api_url_base: not called CC=2 SCAR=17.9
Pod documented
161 0 my $self = shift;
162 0 ▶ return $self->{service_provider}->api_url_base || '';
Condition0%0/2
$self->{'service_provider'}->api_url_base || ''
l!l
00
MC/DC0%0/1
$self->{'service_provider'}->api_url_base || ''
$self->{'service_provider'}->api_url_base
Truth table0%0/2
A
A= $self->{'service_provider'}->api_url_base
Aresult
11
00
163 }
164  
165 0 ▶sub make_api_call {
Subroutine make_api_call: not called CC=9 SCAR=45.0
Pod documented
166 0 my ($self, $uri, $params, $headers) = @_;
167 0 ▶ my $url = $uri =~ m|^http| ? $uri : $self->api_url_base.$uri;
Branch: 1 branch
BranchTrueFalseTotal
$uri =~ /^http/ ? : 0 0 0
168 0 ▶ if ($self->{service_provider}->can('default_api_headers')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"service_provider"}->can("default_api_headers")) 0 0 0
169 0 my $service_provider_headers = $self->{service_provider}->default_api_headers;
170 0 ▶ $headers = ref $headers eq 'HASH' ? { %$headers, %$service_provider_headers } : $service_provider_headers || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'HASH' ? : 0 0 0
Condition0%0/2
$service_provider_headers or {}
l!l
00
MC/DC0%0/1
$service_provider_headers or {}
$service_provider_headers
Truth table0%0/2
A
A= $service_provider_headers
Aresult
11
00
171 }
172  
173 0 ▶ my $response = $params ? $self->post($url, Content => encode_json($params), %$headers) : $self->get($url, %$headers);
Branch: 1 branch
BranchTrueFalseTotal
$params ? : 0 0 0
174  
175 0 ▶ if (! $response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
176 #$self->error('failed call to: '.$url.'; status_line='.$response->status_line.'; full error='.$response->error_as_HTML.'; content='.$response->content);
177 0 ▶ $self->{'_api_call_error'} = $response->error_as_HTML || $response->status_line;
Condition0%0/3
$response->error_as_HTML || $response->status_line
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->error_as_HTML || $response->status_line
$response->error_as_HTML $response->status_line
Truth table0%0/3
A || B
A= $response->error_as_HTMLB= $response->status_line
ABresult
1X1
011
000
178 0 return undef;
179 }
180  
181 0 my $content = $response->content;
182 0 ▶ return 1 if ! $content; # success
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
183 0 return eval { decode_json($content) }; # return decoded JSON if response has a body
184 }
185  
186 0 ▶sub api_call_error { return shift->{'_api_call_error'}; }
Subroutine api_call_error: not called CC=1 SCAR=6.9
Pod documented
187  
188 0 ▶sub request_tokens {
Subroutine request_tokens: not called CC=1 SCAR=6.9
Pod documented
189 0 my ($self, %opts) = @_;
190  
191 # If we get here, the service provider does it.
192 0 my $tokens = $self->{service_provider}->request_tokens($self, %opts);
193 # _set_tokens will set an error if needed.
194 0 return $self->_set_tokens(tokens => $tokens);
195 }
196  
197 0 ▶sub can_refresh_tokens {
Subroutine can_refresh_tokens: not called CC=2 SCAR=17.9
Pod documented
198 0 my $self = shift;
199 0 ▶ if (not $self->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'access_token'}) { } 0 0 0
200 0 return 0;
201 }
202 else {
203 0 my %opts = ($self->{access_token}->for_refresh(), @_);
204 0 return $self->{service_provider}->can_refresh_tokens($self, %opts);
205 }
206 }
207  
208 0 ▶sub refresh_access_token {
Subroutine refresh_access_token: not called CC=9 SCAR=45.0
Pod undocumented
209 0 my $self = shift;
210 0 ▶ if (not $self->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"access_token"}) 0 0 0
211 0 croak("Cannot try to refresh access token without tokens");
212 }
213 0 my %opts = ($self->{access_token}->for_refresh(), @_);
214  
215 # Give a chance for the hook to do it.
216 0 ▶ if ($self->{prerefresh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prerefresh"}) 0 0 0
217 0 my $tokens = $self->{prerefresh}->($self, %opts);
218 0 ▶ if ($tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens) 0 0 0
219 0 ▶ if (not (ref($tokens))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $tokens) 0 0 0
220 # Did I get JSON?
221 0 my $data = eval {decode_json($tokens)};
222  
223 0 ▶ if ($data and not $@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and not $@) 0 0 0
Condition0%0/3
$data and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
$data and not $@
$data not $@
Truth table0%0/3
A and not(B)
A= $dataB= not $@
ABresult
0X0
100
111
224 0 ▶ my $class = $data->{_class} or croak("No _class in token_string '$tokens'");
Branch: 1 branch
BranchTrueFalseTotal
my $class = $data->{"_class"} or croak("No _class in token_string '${tokens}'") 0 0 0
225 0 eval {load($class)};
226 0 ▶ if ($@) { croak("Can't load access token class '$class': $@"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
227 0 $tokens = $class->from_ref($data);
228 }
229 }
230 0 return $self->_set_tokens(tokens => $tokens, skip_save => 1);
231 }
232 }
233  
234 0 my $tokens = $self->{service_provider}->refreshed_tokens($self, %opts);
235 # _set_tokens will set an error if needed.
236 0 return $self->_set_tokens(tokens => $tokens);
237 }
238  
239 0 ▶sub access_token {
Subroutine access_token: not called CC=1 SCAR=6.9
Pod undocumented
240 0 my $self = shift;
241  
242 0 return $self->{access_token};
243 }
244  
245 0 ▶sub should_refresh {
Subroutine should_refresh: not called CC=1 SCAR=6.9
Pod documented
246 0 my $self = shift;
247  
248 0 return $self->access_token->should_refresh($self->{early_refresh_time});
249 }
250  
251 0 ▶sub expires_time {
Subroutine expires_time: not called CC=2 SCAR=17.9
Pod documented
252 0 my $self = shift;
253 0 ▶ return 0 if ! $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"access_token"} 0 0 0
254 0 return $self->access_token->expires_time;
255 }
256  
257 0 ▶sub set_early_refresh_time {
Subroutine set_early_refresh_time: not called CC=1 SCAR=6.9
Pod documented
258 0 my ($self, $early_refresh_time) = @_;
259 0 $self->{early_refresh_time} = $early_refresh_time;
260 }
261  
262 0 ▶sub set_is_strict {
Subroutine set_is_strict: not called CC=1 SCAR=6.9
Pod documented
263 0 my ($self, $strict) = @_;
264 0 $self->{is_strict} = $strict;
265 }
266  
267 1 ▶sub is_strict {
Subroutine is_strict: called CC=1 SCAR=0.0
Pod documented
268 1 my $self = shift;
269 1 return $self->{is_strict};
270 }
271  
272 0 ▶sub set_error_handler {
Subroutine set_error_handler: not called CC=1 SCAR=6.9
Pod documented
273 0 my ($self, $handler) = @_;
274 0 $self->{error_handler} = @_;
275 }
276  
277 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
278 0 my $self = shift;
279 0 ▶ if ($self->{error_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_handler'}) { } 0 0 0
280 0 return $self->{error_handler}->(@_);
281 }
282 else {
283 0 croak(@_);
284 }
285 }
286  
287 1 ▶sub for_service_provider {
Subroutine for_service_provider: called CC=2 SCAR=7.2
Pod undocumented
288 1 my $self = shift;
289 1 ▶ return $self->{for_service_provider} ||= {};
Condition50%1/2
$self->{"for_service_provider"} ||= {}
l!l
10
MC/DC0%0/1
$self->{"for_service_provider"} ||= {}
$self->{"for_service_provider"}
Truth table50%1/2
A
A= $self->{"for_service_provider"}
Aresult
11
00
290 }
291  
292 0 ▶sub set_prerefresh {
Subroutine set_prerefresh: not called CC=1 SCAR=6.9
Pod documented
293 0 my ($self, $prerefresh) = @_;
294 0 $self->{prerefresh} = $prerefresh;
295 }
296  
297 0 ▶sub set_save_tokens {
Subroutine set_save_tokens: not called CC=1 SCAR=6.9
Pod documented
298 0 my ($self, $save_tokens) = @_;
299 0 $self->{save_tokens} = $save_tokens;
300 }
301  
302 0 ▶sub set_user_agent {
Subroutine set_user_agent: not called CC=1 SCAR=6.9
Pod documented
303 0 my ($self, $agent) = @_;
304 0 $self->{user_agent} = $agent;
305 }
306  
307 0 ▶sub load_token_string {
Subroutine load_token_string: not called CC=5 SCAR=34.0
Pod undocumented
308 0 my ($self, $token_string) = @_;
309 0 ▶ $token_string ||= $self->{token_string};
Condition0%0/3
$token_string ||= $self->{"token_string"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_string ||= $self->{"token_string"}
$token_string $self->{"token_string"}
Truth table0%0/3
A ||= B
A= $token_stringB= $self->{"token_string"}
ABresult
1X1
011
000
310  
311 # Probably not the object that I need in access_token.
312 0 my $tokens = eval{ decode_json($token_string) };
313 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
314 0 croak("While decoding token_string: $@");
315 }
316  
317 my $class = $tokens->{_class}
318 0 ▶ or croak("No _class in token_string '$token_string'");
Branch: 1 branch
BranchTrueFalseTotal
my $class = $tokens->{"_class"} or croak("No _class in token_string '${token_string}'") 0 0 0
319  
320 0 eval {load($class)};
321 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
322 0 croak("Can't load access token class '$class': $@");
323 }
324  
325 # I will assume this works.
326 0 $self->{access_token} = $class->from_ref($tokens);
327 }
328  
329 0 ▶sub user_agent {
Subroutine user_agent: not called CC=2 SCAR=17.9
Pod documented
330 0 my $self = shift;
331 0 ▶ return $self->{user_agent} ||= LWP::UserAgent->new();
Condition0%0/3
$self->{"user_agent"} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"user_agent"} ||= "LWP::UserAgent"->new
$self->{"user_agent"} "LWP::UserAgent"->new
Truth table0%0/3
A ||= B
A= $self->{"user_agent"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
332 }
333  
334  
335 9 1; # End of LWP::Authen::OAuth2
336  
337 __END__
« OAuth2/ServiceProvider.pm ↑ Summary OAuth2/AccessToken.pm »