Lite/Consumer.pm

filter: Statementstmt 62.9% 180 / 286 Branchbran 43.4% 40 / 92 Conditioncond 27.4% 34 / 124 MC/DCmcdc 13.2% 9 / 68 Subroutinesub 70.7% 29 / 41 Podpod 100% 21 / 21 totaltotal 49.5% 313 / 632 SCAR 71.4
CC
98
Cov
51%
get_access_token
110.0
obtain_access_token
90.0
gen_oauth_request
73.7
CRAP 1256.0
« Lite/Server/mod_perl2.pm ↑ Summary Lite/Server/Test/Echo.pm »
1 package OAuth::Lite::Consumer;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 use base qw(
7 Class::ErrorHandler
8 Class::Accessor::Fast
9 4 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 __PACKAGE__->mk_accessors(qw(
12 consumer_key
13 consumer_secret
14 oauth_request
15 oauth_response
16 request_token
17 access_token
18 ));
19  
20 4 *oauth_req = \&oauth_request;
21 4 *oauth_res = \&oauth_response;
22  
23 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 ▶use OAuth::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use OAuth::Lite::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use OAuth::Lite::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use OAuth::Lite::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use OAuth::Lite::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use OAuth::Lite::AuthMethod qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =head1 NAME
39  
40 OAuth::Lite::Consumer - consumer agent
41  
42 =head1 SYNOPSIS
43  
44 my $consumer = OAuth::Lite::Consumer->new(
45 consumer_key => $consumer_key,
46 consumer_secret => $consumer_secret,
47 site => q{http://api.example.org},
48 request_token_path => q{/request_token},
49 access_token_path => q{/access_token},
50 authorize_path => q{http://example.org/authorize},
51 );
52  
53 # At first you have to publish request-token, and
54 # with it, redirect end-user to authorization-url that Service Provider tell you beforehand.
55  
56 my $request_token = $consumer->get_request_token(
57 callback_url => q{http://yourservice/callback},
58 );
59  
60 $your_app->session->set( request_token => $request_token );
61  
62 $your_app->redirect( $consumer->url_to_authorize(
63 token => $request_token,
64 ) );
65  
66 # After user authorize the request on a Service Provider side web application.
67  
68 my $verifier = $your_app->request->param('oauth_verifier');
69 my $request_token = $your_app->session->get('request_token');
70  
71 my $access_token = $consumer->get_access_token(
72 token => $request_token,
73 verifier => $verifier,
74 );
75  
76 $your_app->session->set( access_token => $access_token );
77 $your_app->session->remove('request_token');
78  
79 # After all, you can request protected-resources with access token
80  
81 my $access_token = $your_app->session->get('access_token');
82  
83 my $res = $consumer->request(
84 method => 'GET',
85 url => q{http://api.example.org/picture},
86 token => $access_token,
87 params => { file => 'mypic.jpg', size => 'small' },
88 );
89  
90 unless ($res->is_success) {
91 if ($res->status == 400 || $res->status == 401) {
92 my $auth_header = $res->header('WWW-Authenticate');
93 if ($auth_header && $auth_header =~ /^OAuth/) {
94 # access token may be expired,
95 # get request-token and authorize again
96 } else {
97 # another auth error.
98 }
99 }
100 # another error.
101 }
102  
103 # OAuth::Lite::Agent automatically adds Accept-Encoding gzip header to
104 # request, so, when you use default agent, call decoded_content.
105 my $resource = $res->decoded_content || $res->content;
106  
107 $your_app->handle_resource($resource);
108  
109  
110 =head1 DESCRIPTION
111  
112 This module helps you to build OAuth Consumer application.
113  
114 =head1 PAY ATTENTION
115  
116 If you use OAuth 1.31 or older version, its has invalid way to normalize params.
117 (when there are two or more same key and they contain ASCII and non ASCII value)
118  
119 But the many services have already supported deprecated version,
120 and the correct way breaks backward compatibility.
121 So, from 1.32, supported both correct and deprecated method.
122  
123 use $OAuth::Lite::USE_DEPRECATED_NORMALIZER to switch behaviour.
124 Currently 1 is set by default to keep backward compatibility.
125  
126 use OAuth::Lite::Consumer;
127 use OAuth::Lite;
128  
129 $OAuth::Lite::USE_DEPRECATED_NORMALIZER = 0;
130 ...
131  
132 =head1 METHODS
133  
134 =head2 new(%args)
135  
136 =head3 parameters
137  
138 =over 4
139  
140 =item consumer_key
141  
142 consumer_key value
143  
144 =item consumer_secret
145  
146 consumer_secret value
147  
148 =item signature_method
149  
150 Signature method you can choose from 'HMAC-SHA1', 'PLAINTEXT', and 'RSA-SHA1' (optional, 'HMAC-SHA1' is set by default)
151  
152 =item http_method
153  
154 HTTP method (GET or POST) when the request is for request token or access token. (optional, 'POST' is set by default)
155  
156 =item auth_method
157  
158 L<OAuth::Lite::AuthMethod>'s value you can choose from AUTH_HEADER, POST_BODY and URL_QUERY (optional, AUTH_HEADER is set by default)
159  
160 =item realm
161  
162 The OAuth realm value for a protected-resource you wanto to access to. (optional. empty-string is set by default)
163  
164 =item use_request_body_hash
165  
166 If you use Request Body Hash extension, set 1.
167 See Also L<http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/drafts/4/spec.html>
168  
169 =item site
170  
171 The base site url of Service Provider
172  
173 =item request_token_path
174  
175 =item access_token_path
176  
177 =item authorize_path
178  
179 =item callback_url
180  
181 =back
182  
183 Site and other paths, simple usage.
184  
185 my $consumer = OAuth::Lite::Consumer->new(
186 ...
187 site => q{http://example.org},
188 request_token_path => q{/request_token},
189 access_token_path => q{/access_token},
190 authorize_path => q{/authorize},
191 );
192  
193 say $consumer->request_token_url; # http://example.org/request_token
194 say $consumer->access_token_url; # http://example.org/access_token
195 say $consumer->authorization_url; # http://example.org/authorize
196  
197 If the authorization_url is run under another domain, for example.
198  
199 my $consumer = OAuth::Lite::Consumer->new(
200 ...
201 site => q{http://api.example.org},
202 request_token_path => q{/request_token},
203 access_token_path => q{/access_token},
204 authorize_path => q{http://www.example.org/authorize},
205 );
206 say $consumer->request_token_url; # http://api.example.org/request_token
207 say $consumer->access_token_url; # http://api.example.org/access_token
208 say $consumer->authorization_url; # http://www.example.org/authorize
209  
210 Like this, if you pass absolute url, consumer uses them as it is.
211  
212 You can omit site param, if you pass all paths as absolute url.
213  
214 my $consumer = OAuth::Lite::Consumer->new(
215 ...
216 request_token_path => q{http://api.example.org/request_token},
217 access_token_path => q{http://api.example.org/access_token},
218 authorize_path => q{http://www.example.org/authorize},
219 );
220  
221  
222 And there is a flexible way.
223  
224 # don't set each paths here.
225 my $consumer = OAuth::Lite::Consumer->new(
226 consumer_key => $consumer_key,
227 consumer_secret => $consumer_secret,
228 );
229  
230 # set request token url here directly
231 my $rtoken = $consumer->get_request_token(
232 url => q{http://api.example.org/request_token},
233 callback_url => q{http://www.yourservice/callback},
234 );
235  
236 # set authorize path here directly
237 my $url = $consumer->url_to_authorize(
238 token => $rtoken,
239 url => q{http://www.example.org/authorize},
240 );
241  
242 # set access token url here directly
243 my $atoken = $consumer->get_access_token(
244 url => q{http://api.example.org/access_token},
245 verifier => $verfication_code,
246 );
247  
248 So does callback_url. You can set it on consutructor or get_request_token method directly.
249  
250 my $consumer = OAuth::Lite::Consumer->new(
251 ...
252 callback_url => q{http://www.yourservice/callback},
253 );
254 ...
255 my $url = $consumer->get_request_token();
256  
257 Or
258  
259 my $consumer = OAuth::Lite::Consumer->new(
260 ...
261 );
262 ...
263 my $url = $consumer->get_request_token(
264 callback_url => q{http://www.yourservice/callback},
265 );
266  
267 =cut
268  
269 sub new {
270 8 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
271 8 my $ua = delete $args{ua};
272 8 ▶ unless ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 8 0 8
273 8 $ua = OAuth::Lite::Agent->new;
274 8 $ua->agent(join "/", __PACKAGE__, $OAuth::Lite::VERSION);
275 }
276 8 my $self = bless {
277 ua => $ua,
278 }, $class;
279 8 $self->_init(%args);
280 8 $self;
281 }
282  
283 sub _init {
284 8 ▶ my ($self, %args) = @_;
Subroutine _init: called CC=9 SCAR=22.2
285  
286 my $signature_method_class = exists $args{signature_method}
287 ? $args{signature_method}
288 8 ▶ : 'HMAC_SHA1';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'signature_method'} ? : 0 8 8
289 8 $signature_method_class =~ s/-/_/g;
290 8 $signature_method_class = join('::',
291 'OAuth::Lite::SignatureMethod',
292 $signature_method_class
293 );
294 8 ▶ $signature_method_class->require
Branch: 1 branch
BranchTrueFalseTotal
$signature_method_class->require or Carp::croak(sprintf(q[Could't find signature method class, %s], $signature_method_class)) 0 8 8
295 or Carp::croak(
296 sprintf
297 qq/Could't find signature method class, %s/,
298 $signature_method_class
299 );
300  
301 8 ▶ $self->{consumer_key} = $args{consumer_key} || '';
Condition100%2/2
$args{'consumer_key'} || ''
l!l
71
MC/DC100%1/1
$args{'consumer_key'} || ''
$args{'consumer_key'}
Truth table100%2/2
A
A= $args{'consumer_key'}
Aresult
11
00
302 8 ▶ $self->{consumer_secret} = $args{consumer_secret} || '';
Condition100%2/2
$args{'consumer_secret'} || ''
l!l
71
MC/DC100%1/1
$args{'consumer_secret'} || ''
$args{'consumer_secret'}
Truth table100%2/2
A
A= $args{'consumer_secret'}
Aresult
11
00
303 8 ▶ $self->{http_method} = $args{http_method} || 'POST';
Condition100%2/2
$args{'http_method'} || 'POST'
l!l
17
MC/DC100%1/1
$args{'http_method'} || 'POST'
$args{'http_method'}
Truth table100%2/2
A
A= $args{'http_method'}
Aresult
11
00
304 8 ▶ $self->{auth_method} = $args{auth_method} || AUTH_HEADER;
Condition100%2/2
$args{'auth_method'} || 'auth_header'
l!l
17
MC/DC100%1/1
$args{'auth_method'} || 'auth_header'
$args{'auth_method'}
Truth table100%2/2
A
A= $args{'auth_method'}
Aresult
11
00
305 8 ▶ unless ( OAuth::Lite::AuthMethod->validate_method( $self->{auth_method} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("OAuth::Lite::AuthMethod"->validate_method($self->{"auth_method"})) 0 8 8
306 Carp::croak( sprintf
307 0 qq/Invalid auth method "%s"./, $self->{auth_method} );
308 }
309 8 $self->{signature_method} = $signature_method_class;
310 8 $self->{realm} = $args{realm};
311 8 $self->{site} = $args{site};
312 8 $self->{request_token_path} = $args{request_token_path};
313 8 $self->{access_token_path} = $args{access_token_path};
314 8 $self->{authorize_path} = $args{authorize_path};
315 8 $self->{callback_url} = $args{callback_url};
316 8 $self->{oauth_request} = undef;
317 8 $self->{oauth_response} = undef;
318 8 ▶ $self->{use_request_body_hash} = $args{use_request_body_hash} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'use_request_body_hash'} ? : 0 8 8
319 8 $self->{_nonce} = $args{_nonce};
320 8 $self->{_timestamp} = $args{_timestamp};
321 }
322  
323 =head2 request_token_url
324  
325 =cut
326  
327 sub request_token_url {
328 3 ▶ my $self = shift;
Subroutine request_token_url: called CC=2 SCAR=6.9
Pod documented
329 $self->{request_token_path} =~ m!^http(?:s)?\://!
330 ? $self->{request_token_path}
331 3 ▶ : sprintf q{%s%s}, $self->{site}, $self->{request_token_path};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'request_token_path'} =~ m[^http(?:s)?\://] ? : 1 2 3
332 }
333  
334  
335 =head2 access_token_url
336  
337 =cut
338  
339 sub access_token_url {
340 3 ▶ my $self = shift;
Subroutine access_token_url: called CC=2 SCAR=6.9
Pod documented
341 $self->{access_token_path} =~ m!^http(?:s)?\://!
342 ? $self->{access_token_path}
343 3 ▶ : sprintf q{%s%s}, $self->{site}, $self->{access_token_path};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'access_token_path'} =~ m[^http(?:s)?\://] ? : 1 2 3
344 }
345  
346 =head2 authorization_url
347  
348 =cut
349  
350 sub authorization_url {
351 8 ▶ my $self = shift;
Subroutine authorization_url: called CC=2 SCAR=6.9
Pod documented
352 $self->{authorize_path} =~ m!^http(?:s)?\://!
353 ? $self->{authorize_path}
354 8 ▶ : sprintf q{%s%s}, $self->{site}, $self->{authorize_path};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'authorize_path'} =~ m[^http(?:s)?\://] ? : 7 1 8
355 }
356  
357  
358 =head2 url_to_authorize(%params)
359  
360 =head3 parameters
361  
362 =over 4
363  
364 =item url
365  
366 authorization url, you can omit this if you set authorization_path on constructor.
367  
368 =item token
369  
370 request token value
371  
372 =back
373  
374 my $url = $consumer->url_to_authorize(
375 url => q{http://example.org/authorize},
376 token => $request_token,
377 callback_url => q{http://www.yousrservice/callback},
378 );
379  
380 =cut
381  
382 sub url_to_authorize {
383 5 ▶ my ($self, %args) = @_;
Subroutine url_to_authorize: called CC=5 SCAR=16.3
Pod documented
384 5 ▶ $args{url} ||= $self->authorization_url;
Condition33%1/3
$args{"url"} ||= $self->authorization_url
l!l&&r!l&&!r
050
MC/DC0%0/2
$args{"url"} ||= $self->authorization_url
$args{"url"} $self->authorization_url
Truth table33%1/3
A ||= B
A= $args{"url"}B= $self->authorization_url
ABresult
1X1
011
000
385 my $url = $args{url}
386 5 ▶ or Carp::croak qq/url_to_authorize needs url./;
Branch: 1 branch
BranchTrueFalseTotal
my $url = $args{"url"} or Carp::croak("url_to_authorize needs url.") 0 5 5
387 5 my %params = ();
388 5 ▶ if (defined $args{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"token"}) 3 2 5
389 3 my $token = $args{token};
390 3 ▶ $params{oauth_token} = ( eval { $token->isa('OAuth::Lite::Token') } )
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $token->isa('OAuth::Lite::Token') } } ? : 2 1 3
391 ? $token->token
392 : $token;
393 }
394 5 $url = URI->new($url);
395 5 $url->query_form(%params);
396 5 $url->as_string;
397 }
398  
399 =head2 obtain_request_token(%params)
400  
401 Returns a request token as an L<OAuth::Lite::Response> object.
402 Except for that, this method behaves same as get_request_token.
403  
404 =cut
405  
406 sub obtain_request_token {
407 0 ▶ my $self = shift;
Subroutine obtain_request_token: not called CC=6 SCAR=37.4
Pod documented
408 0 my $res = $self->_get_request_token(@_);
409 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
410 0 return $self->error($res->status_line);
411 }
412 0 ▶ my $resp = OAuth::Lite::Response->from_encoded($res->decoded_content||$res->content);
Condition0%0/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table0%0/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
413 0 ▶ return $self->error(qq/oauth_callback_confirmed is not true/)
Branch: 1 branch
BranchTrueFalseTotal
unless $resp and $resp->token and $resp->token->callback_confirmed 0 0 0
Condition0%0/6
$resp and $resp->token and $resp->token->callback_confirmed
!ll&&!rl&&r
000
$resp and $resp->token
!ll&&!rl&&r
000
MC/DC0%0/3
$resp and $resp->token and $resp->token->callback_confirmed
$resp $resp->token $resp->token->callback_confirmed
Truth table0%0/4
A and B and C
A= $respB= $resp->tokenC= $resp->token->callback_confirmed
ABCresult
0XX0
10X0
1100
1111
414 unless $resp && $resp->token && $resp->token->callback_confirmed;
415 0 $self->request_token($resp->token);
416 0 $resp;
417 }
418  
419 =head2 get_request_token(%params)
420  
421 Returns a request token as an L<OAuth::Lite::Token> object.
422  
423 =head3 parameters
424  
425 =over 4
426  
427 =item url
428  
429 Request token url. You can omit this if you set request_token_path on constructor
430  
431 =item realm
432  
433 Realm for the resource you want to access to.
434 You can omit this if you set realm on constructor.
435  
436 =item callback_url
437  
438 Url which service provider redirect end-user to after authorization.
439 You can omit this if you set callback_url on constructor.
440  
441 =back
442  
443 my $token = $consumer->get_request_token(
444 url => q{http://api.example.org/request_token},
445 realm => q{http://api.example.org/picture},
446 ) or die $consumer->errstr;
447  
448 say $token->token;
449 say $token->secret;
450  
451 =cut
452  
453 sub get_request_token {
454 0 ▶ my $self = shift;
Subroutine get_request_token: not called CC=5 SCAR=34.0
Pod documented
455 0 my $res = $self->_get_request_token(@_);
456 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
457 0 return $self->error($res->status_line);
458 }
459 0 ▶ my $token = OAuth::Lite::Token->from_encoded($res->decoded_content||$res->content);
Condition0%0/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table0%0/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
460 0 ▶ return $self->error(qq/oauth_callback_confirmed is not true/)
Branch: 1 branch
BranchTrueFalseTotal
unless $token and $token->callback_confirmed 0 0 0
Condition0%0/3
$token and $token->callback_confirmed
!ll&&!rl&&r
000
MC/DC0%0/2
$token and $token->callback_confirmed
$token $token->callback_confirmed
Truth table0%0/3
A and B
A= $tokenB= $token->callback_confirmed
ABresult
0X0
100
111
461 unless $token && $token->callback_confirmed;
462 0 $self->request_token($token);
463 0 $token;
464 }
465  
466 sub _get_request_token {
467 0 ▶ my ($self, %args) = @_;
Subroutine _get_request_token: not called CC=7 SCAR=40.3
468 0 ▶ $args{url} ||= $self->request_token_url;
Condition0%0/3
$args{"url"} ||= $self->request_token_url
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"url"} ||= $self->request_token_url
$args{"url"} $self->request_token_url
Truth table0%0/3
A ||= B
A= $args{"url"}B= $self->request_token_url
ABresult
1X1
011
000
469 my $request_token_url = delete $args{url}
470 0 ▶ or Carp::croak qq/get_request_token needs url in hash params
Branch: 1 branch
BranchTrueFalseTotal
my $request_token_url = delete $args{"url"} or Carp::croak("get_request_token needs url in hash params\n or set request_token_path on constructor.") 0 0 0
471 or set request_token_path on constructor./;
472 0 ▶ my $realm = delete $args{realm} || $self->{realm} || '';
Condition0%0/5
delete $args{'realm'} || $self->{'realm'} || ''
l!l
00
delete $args{"realm"} or $self->{"realm"}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'realm'} || $self->{'realm'} || ''
delete $args{"realm"} $self->{"realm"}
Truth table0%0/3
A or B
A= delete $args{"realm"}B= $self->{"realm"}
ABresult
1X1
011
000
473 0 ▶ my $callback = delete $args{callback_url} || $self->{callback_url} || 'oob';
Condition0%0/5
delete $args{'callback_url'} || $self->{'callback_url'} || 'oob'
l!l
00
delete $args{"callback_url"} or $self->{"callback_url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'callback_url'} || $self->{'callback_url'} || 'oob'
delete $args{"callback_url"} $self->{"callback_url"}
Truth table0%0/3
A or B
A= delete $args{"callback_url"}B= $self->{"callback_url"}
ABresult
1X1
011
000
474 0 my $res = $self->__request(
475 realm => $realm,
476 url => $request_token_url,
477 params => {%args, oauth_callback => $callback},
478 );
479 0 return $res;
480 }
481  
482 =head2 obtain_access_token
483  
484 my $res = $consumer->obtain_access_token(
485 url => $access_token_url,
486 params => {
487 x_auth_username => "myname",
488 x_auth_password => "mypass",
489 x_auth_mode => "client_auth",
490 },
491 );
492  
493 my $access_token = $res->token;
494 say $acces_token->token;
495 say $acces_token->secret;
496 my $expires = $res->param('x_auth_expires');
497  
498 What is the difference between obtain_access_token and get_access_token?
499 get_access_token requires token and verifier.
500 obtain_access_token doesn't. these parameters are optional.
501 You can pass extra parameters like above example.(see x_auth_XXX parameters)
502 And get_access_token returns OAuth::Lite::Token object directly,
503 obtain_access_token returns OAuth::Lite::Response object that includes
504 not only Token object but also other response parameters.
505 the extra parameters are used for some extensions.(Session extension, xAuth, etc.)
506  
507 Of cource, if you don't need to handle these extensions,
508 You can continue to use get_access_token for backward compatibility.
509  
510 my $token = $consumer->get_access_token(
511 url => $access_token_url,
512 token => $request_token,
513 verifier => $verification_code,
514 );
515  
516 # above code's behavior is same as (but response objects are different)
517  
518 my $res = $consumer->obtain_access_token(
519 url => $access_token_url,
520 token => $request_token,
521 params => {
522 oauth_verifier => $verification_code,
523 },
524 );
525  
526 =cut
527  
528 sub obtain_access_token {
529 0 ▶ my ($self, %args) = @_;
Subroutine obtain_access_token: not called CC=9 SCAR=45.0
Pod documented
530 0 ▶ $args{url} ||= $self->access_token_url;
Condition0%0/3
$args{"url"} ||= $self->access_token_url
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"url"} ||= $self->access_token_url
$args{"url"} $self->access_token_url
Truth table0%0/3
A ||= B
A= $args{"url"}B= $self->access_token_url
ABresult
1X1
011
000
531 my $access_token_url = $args{url}
532 0 ▶ or Carp::croak qq/get_access_token needs access_token_url./;
Branch: 1 branch
BranchTrueFalseTotal
my $access_token_url = $args{"url"} or Carp::croak("get_access_token needs access_token_url.") 0 0 0
533 0 ▶ my $realm = $args{realm} || $self->{realm} || '';
Condition0%0/5
$args{'realm'} || $self->{'realm'} || ''
l!l
00
$args{"realm"} or $self->{"realm"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'realm'} || $self->{'realm'} || ''
$args{"realm"} $self->{"realm"}
Truth table0%0/3
A or B
A= $args{"realm"}B= $self->{"realm"}
ABresult
1X1
011
000
534  
535 0 ▶ my $token = defined $args{token} ? $args{token} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'token'} ? : 0 0 0
536 0 ▶ my $params = $args{params} || {};
Condition0%0/2
$args{'params'} || {}
l!l
00
MC/DC0%0/1
$args{'params'} || {}
$args{'params'}
Truth table0%0/2
A
A= $args{'params'}
Aresult
11
00
537  
538 0 my $res = $self->__request(
539 realm => $realm,
540 url => $access_token_url,
541 token => $token,
542 params => $params,
543 );
544 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
545 0 return $self->error($res->status_line);
546 }
547 0 ▶ my $resp = OAuth::Lite::Response->from_encoded($res->decoded_content||$res->content);
Condition0%0/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table0%0/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
548 0 $self->access_token($resp->token);
549 0 $resp;
550 }
551  
552  
553 =head2 get_access_token(%params)
554  
555 Returns a access token as an L<OAuth::Lite::Token> object.
556  
557 =head3 parameters
558  
559 =over 4
560  
561 =item url
562  
563 Request token url. You can omit this if you set request_token_path on constructor
564  
565 =item realm
566  
567 Realm for the resource you want to access to.
568 You can omit this if you set realm on constructor.
569  
570 =item token
571  
572 Request token object.
573  
574 =item verifier
575  
576 Verfication code which provider returns.
577  
578 =back
579  
580 my $token = $consumer->get_access_token(
581 url => q{http://api.example.org/request_token},
582 realm => q{http://api.example.org/picture},
583 token => $request_token,
584 verifier => $verification_code,
585 ) or die $consumer->errstr;
586  
587 say $token->token;
588 say $token->secret;
589  
590  
591 =cut
592  
593 sub get_access_token {
594 0 ▶ my ($self, %args) = @_;
Subroutine get_access_token: not called CC=10 SCAR=47.0
Pod documented
595 0 ▶ $args{url} ||= $self->access_token_url;
Condition0%0/3
$args{"url"} ||= $self->access_token_url
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"url"} ||= $self->access_token_url
$args{"url"} $self->access_token_url
Truth table0%0/3
A ||= B
A= $args{"url"}B= $self->access_token_url
ABresult
1X1
011
000
596 my $access_token_url = $args{url}
597 0 ▶ or Carp::croak qq/get_access_token needs access_token_url./;
Branch: 1 branch
BranchTrueFalseTotal
my $access_token_url = $args{"url"} or Carp::croak("get_access_token needs access_token_url.") 0 0 0
598 0 ▶ my $token = defined $args{token} ? $args{token} : $self->request_token;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'token'} ? : 0 0 0
599 0 ▶ Carp::croak qq/get_access_token needs token./ unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 0 0
600 0 ▶ my $realm = $args{realm} || $self->{realm} || '';
Condition0%0/5
$args{'realm'} || $self->{'realm'} || ''
l!l
00
$args{"realm"} or $self->{"realm"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'realm'} || $self->{'realm'} || ''
$args{"realm"} $self->{"realm"}
Truth table0%0/3
A or B
A= $args{"realm"}B= $self->{"realm"}
ABresult
1X1
011
000
601 my $verifier = $args{verifier}
602 0 ▶ or Carp::croak qq/verfier not found./;
Branch: 1 branch
BranchTrueFalseTotal
my $verifier = $args{"verifier"} or Carp::croak("verfier not found.") 0 0 0
603 0 my $res = $self->__request(
604 realm => $realm,
605 url => $access_token_url,
606 token => $token,
607 params => { oauth_verifier => $verifier },
608 );
609 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
610 0 return $self->error($res->status_line);
611 }
612 0 ▶ my $access_token = OAuth::Lite::Token->from_encoded($res->decoded_content||$res->content);
Condition0%0/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table0%0/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
613 0 $self->access_token($access_token);
614 0 $access_token;
615 }
616  
617 =head2 gen_oauth_request(%args)
618  
619 Returns L<HTTP::Request> object.
620  
621 my $req = $consumer->gen_oauth_request(
622 method => 'GET',
623 url => 'http://example.com/',
624 headers => [ Accept => q{...}, 'Content-Type' => q{...}, ... ],
625 content => $content,
626 realm => $realm,
627 token => $token,
628 params => { file => 'mypic.jpg', size => 'small' },
629 );
630  
631 =cut
632  
633 sub gen_oauth_request {
634  
635 2 ▶ my ($self, %args) = @_;
Subroutine gen_oauth_request: called CC=29 SCAR=43.0
Pod documented
636  
637 2 ▶ my $method = $args{method} || $self->{http_method};
Condition66%2/3
$args{'method'} || $self->{'http_method'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$args{'method'} || $self->{'http_method'}
$args{'method'} $self->{'http_method'}
Truth table66%2/3
A || B
A= $args{'method'}B= $self->{'http_method'}
ABresult
1X1
011
000
638 2 my $url = $args{url};
639 2 my $content = $args{content};
640 2 my $token = $args{token};
641 2 ▶ my $extra = $args{params} || {};
Condition50%1/2
$args{'params'} || {}
l!l
20
MC/DC0%0/1
$args{'params'} || {}
$args{'params'}
Truth table50%1/2
A
A= $args{'params'}
Aresult
11
00
642 my $realm = $args{realm}
643 || $self->{realm}
644 2 ▶ || $self->find_realm_from_last_response
Condition37%3/8
$args{'realm'} || $self->{'realm'} || $self->find_realm_from_last_response || ''
l!l
02
$args{"realm"} or $self->{"realm"} or $self->find_realm_from_last_response
l!l&&r!l&&!r
002
$args{"realm"} or $self->{"realm"}
l!l&&r!l&&!r
002
MC/DC0%0/3
$args{'realm'} || $self->{'realm'} || $self->find_realm_from_last_response || ''
$args{"realm"} $self->{"realm"} $self->find_realm_from_last_response
Truth table25%1/4
A or B or C
A= $args{"realm"}B= $self->{"realm"}C= $self->find_realm_from_last_response
ABCresult
1XX1
01X1
0011
0000
645 || '';
646  
647 2 ▶ if (ref $extra eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $extra eq "ARRAY") 1 1 2
648 1 my %hash;
649 1 for (0...scalar(@$extra)/2-1) {
650 7 my $key = $extra->[$_ * 2];
651 7 my $value = $extra->[$_ * 2 + 1];
652 7 ▶ $hash{$key} ||= [];
Condition100%2/2
$hash{$key} ||= []
l!l
34
MC/DC100%1/1
$hash{$key} ||= []
$hash{$key}
Truth table100%2/2
A
A= $hash{$key}
Aresult
11
00
653 7 push @{ $hash{$key} }, $value;
654 }
655 1 $extra = \%hash;
656 }
657  
658 2 my $headers = $args{headers};
659 2 ▶ if (defined $headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) { } 0 2 2
660 0 ▶ if (ref($headers) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 0 0 0
661 0 $headers = HTTP::Headers->new(@$headers);
662 } else {
663 0 $headers = $headers->clone;
664 }
665 } else {
666 2 $headers = HTTP::Headers->new;
667 }
668  
669 2 my @send_data_methods = qw/POST PUT/;
670 2 my @non_send_data_methods = qw/GET HEAD DELETE/;
671  
672 2 ▶ my $is_send_data_method = any { $method eq $_ } @send_data_methods;
Subroutine __ANON__: called CC=1 SCAR=0.0
673  
674 2 my $auth_method = $self->{auth_method};
675 2 ▶ $auth_method = AUTH_HEADER
Branch: 1 branch
BranchTrueFalseTotal
if not $is_send_data_method and $auth_method eq "post_body" 0 2 2
Condition66%2/3
not $is_send_data_method and $auth_method eq "post_body"
!ll&&!rl&&r
110
MC/DC0%0/2
not $is_send_data_method and $auth_method eq "post_body"
not $is_send_data_method $auth_method eq "post_body"
Truth table66%2/3
not(A) and B
A= not $is_send_data_methodB= $auth_method eq "post_body"
ABresult
0X0
100
111
676 if ( !$is_send_data_method && $auth_method eq POST_BODY );
677  
678 2 ▶ if ($auth_method eq URL_QUERY) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth_method eq 'url_query') { } 0 2 2
elsif ($auth_method eq 'post_body') { } 0 2 2
679 0 ▶ if ( $is_send_data_method && !$content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_send_data_method and not $content) { } 0 0 0
Condition0%0/3
$is_send_data_method and not $content
!ll&&!rl&&r
000
MC/DC0%0/2
$is_send_data_method and not $content
$is_send_data_method not $content
Truth table0%0/3
A and not(B)
A= $is_send_data_methodB= not $content
ABresult
0X0
100
111
680 0 Carp::croak
681 qq(You must set content-body in case you use combination of URL_QUERY and POST/PUT http method);
682 } else {
683 0 ▶ if ( $is_send_data_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_send_data_method) 0 0 0
684 0 ▶ if ( my $hash = $self->build_body_hash($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = $self->build_body_hash($content)) 0 0 0
685 0 $extra->{oauth_body_hash} = $hash;
686 }
687 }
688 0 my $query = $self->gen_auth_query($method, $url, $token, $extra);
689 0 $url = sprintf q{%s?%s}, $url, $query;
690 }
691 } elsif ($auth_method eq POST_BODY) {
692 0 my $query = $self->gen_auth_query($method, $url, $token, $extra);
693 0 $content = $query;
694 0 $headers->header('Content-Type', q{application/x-www-form-urlencoded});
695 } else {
696 2 my $origin_url = $url;
697 2 my $copied_params = {};
698 2 for my $param_key ( keys %$extra ) {
699 5 ▶ next if $param_key =~ /^x?oauth_/;
Branch: 1 branch
BranchTrueFalseTotal
if $param_key =~ /^x?oauth_/ 1 4 5
700 4 $copied_params->{$param_key} = $extra->{$param_key};
701 }
702 2 ▶ if ( keys %$copied_params > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$copied_params > 0) 1 1 2
703 1 my $data = normalize_params($copied_params);
704 1 ▶ if ( $is_send_data_method && !$content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_send_data_method and not $content) { } 0 1 1
Condition33%1/3
$is_send_data_method and not $content
!ll&&!rl&&r
100
MC/DC0%0/2
$is_send_data_method and not $content
$is_send_data_method not $content
Truth table33%1/3
A and not(B)
A= $is_send_data_methodB= not $content
ABresult
0X0
100
111
705 0 $content = $data;
706 } else {
707 1 $url = sprintf q{%s?%s}, $url, $data;
708 }
709 }
710 2 ▶ if ( $is_send_data_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_send_data_method) 1 1 2
711 1 ▶ if ( my $hash = $self->build_body_hash($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = $self->build_body_hash($content)) 0 1 1
712 0 $extra->{oauth_body_hash} = $hash;
713 }
714 }
715 2 my $header = $self->gen_auth_header($method, $origin_url,
716 { realm => $realm, token => $token, extra => $extra });
717 2 $headers->header( Authorization => $header );
718 }
719 2 ▶ if ( $is_send_data_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_send_data_method) 1 1 2
720 1 ▶ $headers->header('Content-Type', q{application/x-www-form-urlencoded})
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->header("Content-Type") 1 0 1
721 unless $headers->header('Content-Type');
722 1 ▶ $headers->header('Content-Length', bytes::length($content) || 0 );
Condition50%1/2
&bytes::length($content) || 0
l!l
01
MC/DC0%0/1
&bytes::length($content) || 0
&bytes::length($content)
Truth table50%1/2
A
A= &bytes::length($content)
Aresult
11
00
723 }
724 2 my $req = HTTP::Request->new( $method, $url, $headers, $content );
725 2 $req;
726 }
727  
728 =head2 request(%params)
729  
730 Returns L<HTTP::Response> object.
731  
732 =head3 parameters
733  
734 =over 4
735  
736 =item realm
737  
738 Realm for a resource you want to access
739  
740 =item token
741  
742 Access token L<OAuth::Lite::Token> object
743  
744 =item method
745  
746 HTTP method.
747  
748 =item url
749  
750 Request URL
751  
752 =item parmas
753  
754 Extra params.
755  
756 =item content
757  
758 body data sent when method is POST or PUT.
759  
760 =back
761  
762 my $response = $consumer->request(
763 method => 'POST',
764 url => 'http://api.example.com/picture',
765 headers => [ Accept => q{...}, 'Content-Type' => q{...}, ... ],
766 content => $content,
767 realm => $realm,
768 token => $access_token,
769 params => { file => 'mypic.jpg', size => 'small' },
770 );
771  
772 unless ($response->is_success) {
773 ...
774 }
775  
776 =cut
777  
778 sub request {
779 0 ▶ my ($self, %args) = @_;
Subroutine request: not called CC=2 SCAR=17.9
Pod documented
780 0 ▶ $args{token} ||= $self->access_token;
Condition0%0/3
$args{"token"} ||= $self->access_token
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"token"} ||= $self->access_token
$args{"token"} $self->access_token
Truth table0%0/3
A ||= B
A= $args{"token"}B= $self->access_token
ABresult
1X1
011
000
781 0 $self->__request(%args);
782 }
783  
784 sub __request {
785 0 ▶ my ($self, %args) = @_;
Subroutine __request: not called CC=1 SCAR=6.9
786 0 my $req = $self->gen_oauth_request(%args);
787 0 $self->oauth_clear();
788 0 $self->oauth_request($req);
789 0 my $res = $self->{ua}->request($req);
790 0 $self->oauth_response($res);
791 0 $res;
792 }
793  
794 =head2 get
795  
796 There are simple methods to request protected resources.
797 You need to obtain access token and set it to consumer beforehand.
798  
799 my $access_token = $consumer->get_access_token(
800 token => $request_token,
801 verifier => $verifier,
802 );
803 # when successfully got an access-token,
804 # it internally execute saving method like following line.
805 # $consumer->access_token( $access_token )
806  
807 or
808 my $access_token = $your_app->pick_up_saved_access_token();
809 $consumer->access_token($access_token);
810  
811 Then you can access protected resource in a simple way.
812  
813 my $res = $consumer->get( 'http://api.example.com/pictures' );
814 if ($res->is_success) {
815 say $res->decoded_content||$res->content;
816 }
817  
818 This is same as
819  
820 my $res = $consumer->request(
821 method => q{GET},
822 url => q{http://api.example.com/picture},
823 );
824 if ($res->is_success) {
825 say $res->decoded_content||$res->content;
826 }
827  
828 =cut
829  
830 sub get {
831 0 ▶ my ( $self, $url, $args ) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
832 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
833 0 $args->{method} = 'GET';
834 0 $args->{url} = $url;
835 0 $self->request(%$args);
836 }
837  
838 =head2 post
839  
840 $res = $consumer->post( 'http://api.example.com/pictures', $content );
841 if ($res->is_success) {
842 ...
843 }
844  
845 This is same as
846  
847 $res = $consumer->request(
848 method => q{POST},
849 url => q{http://api.example.com/picture},
850 content => $content,
851 );
852 if ($res->is_success) {
853 ...
854 }
855  
856  
857 =cut
858  
859 sub post {
860 0 ▶ my ( $self, $url, $content, $args ) = @_;
Subroutine post: not called CC=2 SCAR=17.9
Pod documented
861 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
862 0 $args->{method} = 'POST';
863 0 $args->{url} = $url;
864 0 $args->{content} = $content;
865 0 $self->request(%$args);
866  
867 }
868  
869 =head2 put
870  
871 $res = $consumer->put( 'http://api.example.com/pictures', $content );
872 if ($res->is_success) {
873 ...
874 }
875  
876 This is same as
877  
878 my $res = $consumer->request(
879 method => q{PUT},
880 url => q{http://api.example.com/picture},
881 content => $content,
882 );
883 if ($res->is_success) {
884 ...
885 }
886  
887  
888 =cut
889  
890 sub put {
891 0 ▶ my ( $self, $url, $content, $args ) = @_;
Subroutine put: not called CC=2 SCAR=17.9
Pod documented
892 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
893 0 $args->{method} = 'PUT';
894 0 $args->{url} = $url;
895 0 $args->{content} = $content;
896 0 $self->request(%$args);
897  
898 }
899  
900 =head2 delete
901  
902 my $res = $consumer->delete('http://api.example.com/delete');
903 if ($res->is_success) {
904 ...
905 }
906  
907 This is same as
908  
909 my $res = $consumer->request(
910 method => q{DELETE},
911 url => q{http://api.example.com/picture},
912 );
913 if ($res->is_success) {
914 ...
915 }
916  
917 =cut
918  
919 sub delete {
920 0 ▶ my ( $self, $url, $args ) = @_;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
921 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
922 0 $args->{method} = 'DELETE';
923 0 $args->{url} = $url;
924 0 $self->request(%$args);
925 }
926  
927 =head2 find_realm_from_last_response
928  
929 =cut
930  
931 sub find_realm_from_last_response {
932 2 ▶ my $self = shift;
Subroutine find_realm_from_last_response: called CC=4 SCAR=24.2
Pod documented
933 2 ▶ return unless $self->oauth_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->oauth_response 2 0 2
934 0 my $authenticate = $self->oauth_response->header('WWW-Authenticate');
935 0 ▶ return unless ($authenticate && $authenticate =~ /^\s*OAuth/);
Branch: 1 branch
BranchTrueFalseTotal
unless $authenticate and $authenticate =~ /^\s*OAuth/ 0 0 0
Condition0%0/3
$authenticate and $authenticate =~ /^\s*OAuth/
!ll&&!rl&&r
000
MC/DC0%0/2
$authenticate and $authenticate =~ /^\s*OAuth/
$authenticate $authenticate =~ /^\s*OAuth/
Truth table0%0/3
A and B
A= $authenticateB= $authenticate =~ /^\s*OAuth/
ABresult
0X0
100
111
936 0 my $realm = parse_auth_header($authenticate);
937 0 $realm;
938 }
939  
940 =head2 gen_auth_header($http_method, $request_url, $params);
941  
942 =head3 parameters
943  
944 =over 4
945  
946 =item realm
947  
948 realm for a resource you want to access
949  
950 =item token
951  
952 OAuth::Lite::Token object(optional)
953  
954 =back
955  
956 my $header = $consumer->gen_auth_header($method, $url, {
957 realm => $realm,
958 token => $token,
959 });
960  
961 =cut
962  
963 sub gen_auth_header {
964 2 ▶ my ($self, $method, $url, $args) = @_;
Subroutine gen_auth_header: called CC=3 SCAR=11.2
Pod documented
965 2 ▶ my $extra = $args->{extra} || {};
Condition50%1/2
$args->{'extra'} || {}
l!l
20
MC/DC0%0/1
$args->{'extra'} || {}
$args->{'extra'}
Truth table50%1/2
A
A= $args->{'extra'}
Aresult
11
00
966 2 my $params = $self->gen_auth_params($method, $url, $args->{token}, $extra);
967 2 ▶ my $realm = $args->{realm} || '';
Condition50%1/2
$args->{'realm'} || ''
l!l
02
MC/DC0%0/1
$args->{'realm'} || ''
$args->{'realm'}
Truth table50%1/2
A
A= $args->{'realm'}
Aresult
11
00
968 2 my $authorization_header = build_auth_header($realm, $params);
969 2 $authorization_header;
970 }
971  
972 =head2 gen_auth_query($http_method, $ruqest_url, $token, $extra)
973  
974 =cut
975  
976 sub gen_auth_query {
977 4 ▶ my ($self, $method, $url, $token, $extra) = @_;
Subroutine gen_auth_query: called CC=2 SCAR=6.9
Pod documented
978 4 ▶ $extra ||= {};
Condition100%2/2
$extra ||= {}
l!l
22
MC/DC100%1/1
$extra ||= {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
979 4 my $params = $self->gen_auth_params($method, $url, $token, $extra);
980 4 my %all = (%$extra, %$params);
981 4 normalize_params({%all});
982 }
983  
984 =head2 gen_auth_params($http_method, $request_url, [$token])
985  
986 Generates and returns all oauth params.
987  
988 my $params = $consumer->gen_auth_params($http_method, $request_url);
989 say $params->{oauth_consumer_key};
990 say $params->{oauth_timestamp};
991 say $params->{oauth_nonce};
992 say $params->{oauth_signature_method};
993 say $params->{oauth_signature};
994 say $params->{oauth_version};
995  
996 If you pass token as third argument, the result includes oauth_token value.
997  
998 my $params = $consumer->gen_auth_params($http_method, $request_url, $token);
999 say $params->{oauth_consumer_key};
1000 say $params->{oauth_timestamp};
1001 say $params->{oauth_nonce};
1002 say $params->{oauth_signature_method};
1003 say $params->{oauth_signature};
1004 say $params->{oauth_token};
1005 say $params->{oauth_version};
1006  
1007 =cut
1008  
1009 sub gen_auth_params {
1010 10 ▶ my ($self, $method, $url, $token, $extra) = @_;
Subroutine gen_auth_params: called CC=10 SCAR=23.6
Pod documented
1011 10 my $params = {};
1012 10 ▶ $extra ||= {};
Condition100%2/2
$extra ||= {}
l!l
64
MC/DC100%1/1
$extra ||= {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
1013 10 ▶ $params->{oauth_consumer_key} = $self->consumer_key || '';
Condition100%2/2
$self->consumer_key || ''
l!l
91
MC/DC100%1/1
$self->consumer_key || ''
$self->consumer_key
Truth table100%2/2
A
A= $self->consumer_key
Aresult
11
00
1014 10 ▶ $params->{oauth_timestamp} = $self->{_timestamp} || time();
Condition33%1/3
$self->{'_timestamp'} || time
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->{'_timestamp'} || time
$self->{'_timestamp'} time
Truth table33%1/3
A || B
A= $self->{'_timestamp'}B= time
ABresult
1X1
011
000
1015 10 ▶ $params->{oauth_nonce} = $self->{_nonce} || gen_random_key();
Condition33%1/3
$self->{'_nonce'} || gen_random_key()
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->{'_nonce'} || gen_random_key()
$self->{'_nonce'} gen_random_key()
Truth table33%1/3
A || B
A= $self->{'_nonce'}B= gen_random_key()
ABresult
1X1
011
000
1016 10 $params->{oauth_version} = $OAuth::Lite::OAUTH_DEFAULT_VERSION;
1017 10 my $token_secret = '';
1018 10 ▶ if (defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 4 6 10
1019 4 ▶ if (eval { $token->isa('OAuth::Lite::Token') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $token->isa('OAuth::Lite::Token') } }) { } 3 1 4
1020 3 $params->{oauth_token} = $token->token;
1021 3 $token_secret = $token->secret;
1022 } else {
1023 1 $params->{oauth_token} = $token;
1024 }
1025 }
1026 10 ▶ my $consumer_secret = $self->consumer_secret || '';
Condition100%2/2
$self->consumer_secret || ''
l!l
91
MC/DC100%1/1
$self->consumer_secret || ''
$self->consumer_secret
Truth table100%2/2
A
A= $self->consumer_secret
Aresult
11
00
1027 10 $params->{oauth_signature_method} = $self->{signature_method}->method_name;
1028 10 ▶ if ($params->{oauth_signature_method} eq 'PLAINTEXT' && lc($url) !~ /^https/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"oauth_signature_method"} eq "PLAINTEXT" and not lc($url) =~ /^https/) 0 10 10
Condition33%1/3
$params->{"oauth_signature_method"} eq "PLAINTEXT" and not lc($url) =~ /^https/
!ll&&!rl&&r
1000
MC/DC0%0/2
$params->{"oauth_signature_method"} eq "PLAINTEXT" and not lc($url) =~ /^https/
$params->{"oauth_signature_method"} eq "PLAINTEXT" not lc($url) =~ /^https/
Truth table33%1/3
A and not(B)
A= $params->{"oauth_signature_method"} eq "PLAINTEXT"B= not lc($url) =~ /^https/
ABresult
0X0
100
111
1029 0 warn qq(PLAINTEXT signature method should be used on SSL/TSL.);
1030 }
1031 10 $params = {%$params, %$extra};
1032 10 my $base = create_signature_base_string($method, $url, $params);
1033 $params->{oauth_signature} = $self->{signature_method}->new(
1034 10 consumer_secret => $consumer_secret,
1035 token_secret => $token_secret,
1036 )->sign($base);
1037 10 $params;
1038 }
1039  
1040 =head2 oauth_request
1041  
1042 =head2 oauth_req
1043  
1044 Returns last oauth request.
1045  
1046 my $req_token = $consumer->get_request_token(...);
1047 say $consumer->oauth_request->uri;
1048  
1049 my $req_token = $consumer->get_access_token(...);
1050 say $consumer->oauth_request->uri;
1051  
1052 =head2 oauth_response
1053  
1054 =head2 oauth_res
1055  
1056 Returns last oauth response.
1057  
1058 my $req_token = $consumer->get_request_token(...);
1059 say $consumer->oauth_response->status;
1060  
1061 my $req_token = $consumer->get_access_token(...);
1062 say $consumer->oauth_response->status;
1063  
1064 =head2 oauth_clear
1065  
1066 remove last oauth-request and oauth-response.
1067  
1068 =cut
1069  
1070 sub oauth_clear {
1071 0 ▶ my $self = shift;
Subroutine oauth_clear: not called CC=1 SCAR=6.9
Pod documented
1072 0 $self->{oauth_request} = undef;
1073 0 $self->{oauth_response} = undef;
1074 }
1075  
1076 =head2 build_body_hash
1077  
1078 Build body hash according to the spec for 'OAuth Request Body Hash extension'
1079 http://oauth.googlecode.com/svn/spec/ext/body_hash/1.0/drafts/4/spec.html
1080  
1081 my $hash = $self->build_body_hash($content);
1082  
1083 =cut
1084  
1085 sub build_body_hash {
1086 1 ▶ my ( $self, $content ) = @_;
Subroutine build_body_hash: called CC=2 SCAR=8.4
Pod documented
1087 1 ▶ if ( $self->{use_request_body_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_request_body_hash"}) 0 1 1
1088 0 my $hash = $self->{signature_method}->build_body_hash($content);
1089 0 return $hash;
1090 }
1091 1 return;
1092 }
1093  
1094 =head1 AUTHOR
1095  
1096 Lyo Kato, C<lyo.kato _at_ gmail.com>
1097  
1098 =head1 COPYRIGHT AND LICENSE
1099  
1100 This library is free software; you can redistribute it and/or modify
1101 it under the same terms as Perl itself, either Perl version 5.8.6 or,
1102 at your option, any later version of Perl 5 you may have available.
1103  
1104 =cut
1105  
1106 4 1;
« Lite/Server/mod_perl2.pm ↑ Summary Lite/Server/Test/Echo.pm »