lib/OAuthomatic/Caller.pm

filter: Statementstmt 30.2% 53 / 175 Branchbran 1.1% 1 / 88 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 48.1% 13 / 27 Podpod 100% 4 / 4 totaltotal 20.4% 71 / 347 SCAR 75.8
CC
59
Cov
18%
_build_oauth_request_ext
1406.0
_create_temporary_cred
30.0
_execute_oauth_request_ext
20.0
CRAP 1957.0
↑ Summary blib/lib/OAuthomatic.pm »
1 package OAuthomatic::Caller;
2 # ABSTRACT: actually make OAuth-signed calls
3  
4  
5 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Net::OAuth;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use OAuthomatic::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use OAuthomatic::Internal::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 const my $OAUTH_REALM => "OAuthomatic";
20  
21 5 const my $FORM_CONTENT_TYPE => "application/x-www-form-urlencoded";
22 5 const my $DEFAULT_FORM_CONTENT_TYPE => "application/x-www-form-urlencoded; charset=utf-8";
23 5 const my $DEFAULT_CONTENT_TYPE => "text/plain; charset=utf-8";
24  
25 5 const my $_HAS_BUGGY_PROXY_IN_LWP => (
26 version->parse($LWP::UserAgent::VERSION) < version->parse("6.06") );
27  
28  
29 5 has 'config' => (
30 is => 'ro', isa => 'OAuthomatic::Config', required => 1,
31 handles => ['debug']);
32  
33  
34 5 has 'server' => (
35 is => 'ro', isa => 'OAuthomatic::Server', required => 1,
36 handles => [
37 'oauth_authorize_page', 'oauth_temporary_url', 'oauth_token_url',
38 'protocol_version', 'signature_method'
39 ]);
40  
41 # Object responsible for executing calls.
42 has 'user_agent' => (
43 is => 'ro', isa => 'LWP::UserAgent', default => sub {
44 # Workaround for SSL-over-proxy problems. Disables buggy proxy behaviour in
45 # LWP (proxy should be handled by Crypt::SSLeay below thanks to env variable).
46 # https://rt.cpan.org/Public/Bug/Display.html?id=1894
47 # http://www.perlmonks.org/?node_id=994683
48 # FIXME: when LWP 6.06 turns popular, drop it and just depend on 6.06
49 0 ▶ my $ua;
Subroutine __ANON__: not called CC=3 SCAR=24.8
50 0 ▶ if($_HAS_BUGGY_PROXY_IN_LWP && $ENV{https_proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_HAS_BUGGY_PROXY_IN_LWP and $ENV{'https_proxy'}) { } 0 0 0
Condition0%0/3
$_HAS_BUGGY_PROXY_IN_LWP and $ENV{"https_proxy"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_HAS_BUGGY_PROXY_IN_LWP and $ENV{"https_proxy"}
$_HAS_BUGGY_PROXY_IN_LWP $ENV{"https_proxy"}
Truth table0%0/3
A and B
A= $_HAS_BUGGY_PROXY_IN_LWPB= $ENV{"https_proxy"}
ABresult
0X0
100
111
51 0 require Net::SSL;
52 0 local $Net::HTTPS::SSL_SOCKET_CLASS = "Net::SSL"; # Force use of Net::SSL
53 0 $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
54 0 $ua->proxy(https => undef);
55 } else {
56 0 $ua = LWP::UserAgent->new;
57 }
58 0 return $ua;
59 5 });
60  
61 # Application identity
62 has 'client_cred' => (
63 is => 'rw', isa => 'Maybe[OAuthomatic::Types::ClientCred]', required => 1,
64 trigger => sub {
65 0 ▶ my ($self, $cred, $old_cred) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
66 0 ▶ return if @_ == 2; # Don't act during object build
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 0 0 0
67 0 ▶ return if OAuthomatic::Types::ClientCred->equal($cred, $old_cred);
Branch: 1 branch
BranchTrueFalseTotal
if "OAuthomatic::Types::ClientCred"->equal($cred, $old_cred) 0 0 0
68 # Changed client_cred means access creds are no longer valid.
69 0 $self->token_cred(undef);
70 5 });
71  
72 # Current access credentials, for use in normal calls
73 5 has 'token_cred' => (
74 is => 'rw', isa => 'Maybe[OAuthomatic::Types::TokenCred]', required => 1);
75  
76  
77 sub create_authorization_url {
78 0 ▶ my ($self, $callback_url) = @_;
Subroutine create_authorization_url: not called CC=1 SCAR=6.9
Pod documented
79  
80 # Make temporary credentials
81 0 my $temporary_cred = $self->_create_temporary_cred($callback_url);
82  
83 # Calculate authorization url
84 0 my $url = URI->new($self->oauth_authorize_page);
85 0 $url->query_param(oauth_token => $temporary_cred->token);
86  
87 0 $temporary_cred->authorize_page($url);
88  
89 0 return $temporary_cred;
90 }
91  
92 # Create request token
93 sub _create_temporary_cred {
94 0 ▶ my ($self, $callback_url) = @_;
Subroutine _create_temporary_cred: not called CC=5 SCAR=34.0
95  
96 0 ▶ unless ($self->_using_legacy_oauth10) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_using_legacy_oauth10) 0 0 0
97 0 ▶ unless($callback_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callback_url) 0 0 0
98 0 OAuthomatic::Error::Generic->throw(
99 ident => "missing required argument",
100 extra => "callback_url is required");
101 }
102 }
103  
104 0 my $response = $self->_execute_oauth_request_ext(
105 class => 'RequestToken', # No token
106 callback => $callback_url,
107 method => 'POST', url => $self->oauth_temporary_url,
108 auth_in_post => 1);
109  
110 0 my $response_params = _parse_form_reply(
111 $response, ['oauth_token', 'oauth_token_secret']);
112  
113 0 ▶ unless($response_params->{oauth_callback_confirmed}
Branch: 1 branch
BranchTrueFalseTotal
unless ($response_params->{"oauth_callback_confirmed"} or $self->_using_legacy_oauth10) 0 0 0
Condition0%0/3
$response_params->{"oauth_callback_confirmed"} or $self->_using_legacy_oauth10
l!l&&r!l&&!r
000
MC/DC0%0/2
$response_params->{"oauth_callback_confirmed"} or $self->_using_legacy_oauth10
$response_params->{"oauth_callback_confirmed"} $self->_using_legacy_oauth10
Truth table0%0/3
A or B
A= $response_params->{"oauth_callback_confirmed"}B= $self->_using_legacy_oauth10
ABresult
1X1
011
000
114 || $self->_using_legacy_oauth10) {
115 0 OAuthomatic::Error::Protocol->throw(
116 ident => "Bad OAuth reply",
117 extra => "Missing oauth_callback_confirmed (is site using OAuth 1.0?)");
118 }
119  
120 0 return OAuthomatic::Types::TemporaryCred->new(
121 data => $response_params,
122 remap => {'oauth_token' => 'token', 'oauth_token_secret' => 'secret'});
123 }
124  
125  
126 sub create_token_cred {
127 0 ▶ my ($self, $temporary_cred, $verifier_cred) = @_;
Subroutine create_token_cred: not called CC=3 SCAR=24.8
Pod documented
128  
129 0 ▶ unless ($verifier_cred || $self->_using_legacy_oauth10) { # FIXME maybe drop support for it?
Branch: 1 branch
BranchTrueFalseTotal
unless ($verifier_cred or $self->_using_legacy_oauth10) 0 0 0
Condition0%0/3
$verifier_cred or $self->_using_legacy_oauth10
l!l&&r!l&&!r
000
MC/DC0%0/2
$verifier_cred or $self->_using_legacy_oauth10
$verifier_cred $self->_using_legacy_oauth10
Truth table0%0/3
A or B
A= $verifier_credB= $self->_using_legacy_oauth10
ABresult
1X1
011
000
130 0 OAuthomatic::Error::Generic->throw(
131 ident => "Missing parameter",
132 extra => "For OAuth 1.0a verifier is required");
133 }
134  
135 0 my $response = $self->_execute_oauth_request_ext(
136 class => 'AccessToken',
137 token => $temporary_cred, verifier => $verifier_cred,
138 method => 'POST', url => $self->oauth_token_url,
139 auth_in_post => 1);
140  
141 0 my $response_params = _parse_form_reply(
142 $response, ['oauth_token', 'oauth_token_secret']);
143  
144 0 my $cred = OAuthomatic::Types::TokenCred->new(
145 data => $response_params,
146 remap => {'oauth_token' => 'token', 'oauth_token_secret' => 'secret'});
147  
148 0 $self->token_cred($cred);
149  
150 0 return $cred;
151 }
152  
153 ###########################################################################
154 # Call-making
155 ###########################################################################
156  
157  
158 sub build_oauth_request {
159 0 ▶ my $self = shift;
Subroutine build_oauth_request: not called CC=2 SCAR=17.9
Pod documented
160  
161 0 ▶ unless($self->token_cred) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token_cred) 0 0 0
162 0 OAuthomatic::Error::Generic->throw(
163 ident => "Unauthorized",
164 extra => "Missing token.");
165 }
166  
167 0 return $self->_build_oauth_request_ext(
168 class => 'ProtectedResource',
169 token => $self->token_cred,
170 @_);
171 }
172  
173  
174 sub execute_oauth_request {
175 0 ▶ my $self = shift;
Subroutine execute_oauth_request: not called CC=2 SCAR=17.9
Pod documented
176  
177 0 ▶ unless($self->token_cred) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->token_cred) 0 0 0
178 0 OAuthomatic::Error::Generic->throw(
179 ident => "Unauthorized",
180 extra => "Missing token.");
181 }
182  
183 0 return $self->_execute_oauth_request_ext(
184 class => 'ProtectedResource',
185 token => $self->token_cred,
186 @_);
187 }
188  
189  
190 sub _build_oauth_request_ext {
191 0 ▶ my ($self, %args) = @_;
Subroutine _build_oauth_request_ext: not called CC=37 SCAR=72.5
192  
193 0 foreach my $req_arg (qw(class method url)) {
194 0 ▶ unless($args{$req_arg}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{$req_arg}) 0 0 0
195 0 OAuthomatic::Error::Generic->throw(
196 ident => "Bad parameter",
197 extra => "Missing required parameter '$req_arg'");
198 }
199 }
200  
201 0 my $class = $args{class};
202 0 my $token = $args{token};
203 0 my $verifier = $args{verifier};
204 0 my $callback = $args{callback};
205 0 my $method = $args{method};
206 0 my $url = $args{url};
207 0 my $url_args = $args{url_args};
208 0 my $body_form = $args{body_form};
209 0 my $body = $args{body};
210 0 my $content_type = $args{content_type};
211 0 my $auth_in_post = $args{auth_in_post};
212  
213 # Sanity checks
214  
215 0 ▶ if($method =~ /^(?:POST|PUT)$/x) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method =~ /^(?:POST|PUT)$/x) { } 0 0 0
elsif ($method =~ /^(?:GET|DELETE)$/x) { } 0 0 0
216 0 ▶ $body_form = {} unless ($body || $body_form);
Branch: 1 branch
BranchTrueFalseTotal
unless $body or $body_form 0 0 0
Condition0%0/3
$body or $body_form
l!l&&r!l&&!r
000
MC/DC0%0/2
$body or $body_form
$body $body_form
Truth table0%0/3
A or B
A= $bodyB= $body_form
ABresult
1X1
011
000
217 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $body and $body_form 0 0 0
Condition0%0/3
$body and $body_form
!ll&&!rl&&r
000
MC/DC0%0/2
$body and $body_form
$body $body_form
Truth table0%0/3
A and B
A= $bodyB= $body_form
ABresult
0X0
100
111
218 ident => "Bad parameter",
219 extra => "Can not specify both body and body_form")
220 if $body && $body_form;
221 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $auth_in_post and $body 0 0 0
Condition0%0/3
$auth_in_post and $body
!ll&&!rl&&r
000
MC/DC0%0/2
$auth_in_post and $body
$auth_in_post $body
Truth table0%0/3
A and B
A= $auth_in_postB= $body
ABresult
0X0
100
111
222 ident => "Bad parameter",
223 extra => "Can't use plain body with auth_in_post")
224 if $auth_in_post && $body;
225 }
226 elsif($method =~ /^(?:GET|DELETE)$/x) {
227 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $body or $body_form 0 0 0
Condition0%0/3
$body or $body_form
l!l&&r!l&&!r
000
MC/DC0%0/2
$body or $body_form
$body $body_form
Truth table0%0/3
A or B
A= $bodyB= $body_form
ABresult
1X1
011
000
228 ident => "Bad parameter",
229 extra => "Can not specify body* for $method request")
230 if ($body || $body_form);
231 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $auth_in_post 0 0 0
232 ident => "Bad parameter",
233 extra => "Can't use auth_in_post for $method request")
234 if $auth_in_post;
235 }
236 else {
237 0 OAuthomatic::Error::Generic->throw(
238 ident => "Bad parameter",
239 extra => "Unknown method: $method (expected GET, POST, PUT or DELETE)");
240 }
241  
242 0 ▶ if($url_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url_args) 0 0 0
243 0 ▶ unless(ref($url_args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $url_args eq "HASH") 0 0 0
244 0 ▶ OAuthomatic::Error::Generic->throw(
Condition0%0/2
ref $url_args || "scalar value $url_args"
l!l
00
MC/DC0%0/1
ref $url_args || "scalar value $url_args"
ref $url_args
Truth table0%0/2
A
A= ref $url_args
Aresult
11
00
245 ident => "Bad parameter",
246 extra => "url_args should be specified as hash reference, but are given as " . (ref($url_args) || "scalar value $url_args"));
247 }
248 }
249  
250 0 ▶ if($body_form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_form) { } 0 0 0
251 0 ▶ $content_type ||= $DEFAULT_FORM_CONTENT_TYPE;
Condition0%0/3
$content_type ||= $DEFAULT_FORM_CONTENT_TYPE
l!l&&r!l&&!r
000
MC/DC0%0/2
$content_type ||= $DEFAULT_FORM_CONTENT_TYPE
$content_type $DEFAULT_FORM_CONTENT_TYPE
Truth table0%0/3
A ||= B
A= $content_typeB= $DEFAULT_FORM_CONTENT_TYPE
ABresult
1X1
011
000
252 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $content_type =~ /^$FORM_CONTENT_TYPE(;.*)?$/ 0 0 0
253 ident => "Bad parameter",
254 extra => "With body_form, the only allowed content_type is $FORM_CONTENT_TYPE (plus coding), but $content_type was given")
255 unless $content_type =~ /^$FORM_CONTENT_TYPE(;.*)?$/;
256 } else {
257 0 ▶ $content_type ||= $DEFAULT_CONTENT_TYPE;
Condition0%0/3
$content_type ||= $DEFAULT_CONTENT_TYPE
l!l&&r!l&&!r
000
MC/DC0%0/2
$content_type ||= $DEFAULT_CONTENT_TYPE
$content_type $DEFAULT_CONTENT_TYPE
Truth table0%0/3
A ||= B
A= $content_typeB= $DEFAULT_CONTENT_TYPE
ABresult
1X1
011
000
258 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $content_type =~ /^$FORM_CONTENT_TYPE(;.*)?$/ 0 0 0
259 ident => "Bad parameter",
260 extra => "Can not specify content_type=$content_type and plain body, use body_form!")
261 if $content_type =~ /^$FORM_CONTENT_TYPE(;.*)?$/;
262 }
263  
264 0 ▶ if($verifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verifier) 0 0 0
265 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
266 ident => "Bad parameter",
267 extra => "Attempt to specify verifier without token")
268 unless $token;
269 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $verifier->token eq $token->token 0 0 0
270 ident => "Token mismatch",
271 extra => "Obtained verifier is for token " . $verifier->token
272 . " while (temporary) token is " . $token->token)
273 unless $verifier->token eq $token->token;
274 }
275  
276 # Calculate final URL
277  
278 # FIXME: URI encodes as utf-8 true strings or as Latin-1 binary strings. Consider
279 # using default encoding if non-utf8
280 0 my $uri = URI->new($url);
281 # Append additional params, if given
282 0 ▶ if($url_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url_args) 0 0 0
283 0 foreach my $key (keys %$url_args) {
284 0 $uri->query_param($key, $url_args->{$key});
285 }
286 }
287  
288 # Net::OAuth is used to
289 # (a) calculate signature
290 # and
291 # (b) render authorization header
292 0 ▶ my $oauth_request = Net::OAuth->request($class)->new(
Branch: 4 branches
BranchTrueFalseTotal
$token ? : 0 0 0
$verifier ? : 0 0 0
$callback ? : 0 0 0
$body_form ? : 0 0 0
293 request_method => $method,
294 request_url => $uri,
295 $self->client_cred->as_hash("consumer"), # consumer_key, consumer_secret
296 ($token
297 ? ($token->as_hash()) # token, token_secret
298 : ()),
299 signature_method => $self->signature_method,
300 protocol_version => $self->_net_oauth_version_constant(), # FIXME: isn't it version?
301 # version => $self->protocol_version, # changes oauth_version but better not, bb fails
302 timestamp => time,
303 nonce => $self->_nonce,
304 ($verifier
305 ? (verifier => $verifier->verifier)
306 : ()),
307 ($callback
308 ? (callback => $callback)
309 : ()),
310 ($body_form
311 ? (extra_params => $body_form) # body parts used in signature
312 : ()),
313 );
314 0 $oauth_request->sign;
315 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $oauth_request->verify 0 0 0
316 ident => "OAuth signature verification failed.")
317 unless $oauth_request->verify;
318  
319 0 my $headers = HTTP::Headers->new();
320 # FIXME: handle custom headers (Accept?)
321  
322 0 ▶ if($auth_in_post) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_in_post) { } 0 0 0
323 0 my $oauth_par = $oauth_request->to_hash();
324 0 ▶ if($body_form && %$body_form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_form and %$body_form) { } 0 0 0
Condition0%0/3
$body_form and %$body_form
!ll&&!rl&&r
000
MC/DC0%0/2
$body_form and %$body_form
$body_form %$body_form
Truth table0%0/3
A and B
A= $body_formB= %$body_form
ABresult
0X0
100
111
325 # Merge OAuth with other post oauth_par
326 0 @{$body_form}{keys %$oauth_par} = values %$oauth_par;
327 } else {
328 0 $body_form = $oauth_par;
329 }
330 } else {
331 # This builds "OAuth realm=... oauth_consumer_key=.. ..." text
332 0 my $oauth_header_text = $oauth_request->to_authorization_header($OAUTH_REALM);
333 0 $headers->header('Authorization' => $oauth_header_text);
334 }
335  
336 0 my $http_request = HTTP::Request->new($method, $uri, $headers);
337 0 ▶ if($body) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body) { } 0 0 0
elsif ($body_form) { } 0 0 0
338 0 fill_httpmsg_text($http_request, $body, $content_type);
339 } elsif($body_form) {
340 0 fill_httpmsg_form($http_request, $body_form);
341 }
342  
343 0 return $http_request;
344 }
345  
346  
347 sub _execute_oauth_request_ext {
348 0 ▶ my $self = shift;
Subroutine _execute_oauth_request_ext: not called CC=4 SCAR=30.0
349 0 my $request = $self->_build_oauth_request_ext(@_);
350  
351 0 ▶ print "[OAuthomatic] Executing request: ", $request->as_string, "\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
352  
353 0 my $response = $self->user_agent->request($request);
354  
355 0 ▶ print "[OAuthomatic] Obtained response: ", $response->as_string, "\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
356  
357 0 ▶ OAuthomatic::Error::HTTPFailure->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
358 ident => 'OAuth-signed HTTP call failed',
359 request => $request, response => $response)
360 unless $response->is_success;
361  
362 0 return $response;
363 }
364  
365 ###########################################################################
366 # Helpers
367 ###########################################################################
368  
369 sub _net_oauth_version_constant {
370 0 ▶ my $self = shift;
Subroutine _net_oauth_version_constant: not called CC=3 SCAR=24.8
371 0 my $ver = $self->protocol_version;
372 0 ▶ return Net::OAuth::PROTOCOL_VERSION_1_0A() if $ver eq '1.0a';
Branch: 1 branch
BranchTrueFalseTotal
if $ver eq "1.0a" 0 0 0
373 0 ▶ return Net::OAuth::PROTOCOL_VERSION_1_0() if $ver eq '1.0';
Branch: 1 branch
BranchTrueFalseTotal
if $ver eq "1.0" 0 0 0
374 0 OAuthomatic::Error::Generic->throw(
375 ident => "Invalid parameter",
376 extra => "Invalid protocol version: $ver");
377 }
378  
379 sub _using_legacy_oauth10 {
380 0 ▶ my $self = shift;
Subroutine _using_legacy_oauth10: not called CC=2 SCAR=17.9
381 0 ▶ return 1 if $self->protocol_version eq '1.0';
Branch: 1 branch
BranchTrueFalseTotal
if $self->protocol_version eq "1.0" 0 0 0
382 0 return 0;
383 }
384  
385 # Pair (timestamp-rounded-to-second, nonce) should be unique. Usual
386 # random should do in practical cases, but there is tiny risk of
387 # collisions. Marsenne-Twister, with its uniform distribution,
388 # reduces this risk even further.
389 BEGIN {
390 eval {
391 5 require Math::Random::MT;
392 5 my $rnd_gen = Math::Random::MT->new();
393 *_nonce = sub {
394 0 ▶ return $rnd_gen->rand(2**32);
Subroutine __ANON__: not called CC=1 SCAR=6.9
395 5 };
396 5 1;
397 5 ▶ } or do {
Subroutine BEGIN: called CC=2 SCAR=10.1
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Math::Random::MT; my $rnd_gen = "Math::Random::MT"->new; *_nonce = sub { return $rnd_gen->rand("4294967296"); } ; 1 } } or do { srand(); *_nonce = sub { return int rand "4294967296"; } } 0 5 5
398 0 srand();
399 *_nonce = sub {
400 0 ▶ return int( rand( 2**32 ) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 0 };
402 };
403 # Random setup
404 5 srand();
405 };
406  
407 # Checks response: throws exception on error, parses and returns param hash
408 # Second param, if present, is a list of required parameters
409 sub _parse_form_reply {
410 0 ▶ my ($http_response, $required_params) = @_;
Subroutine _parse_form_reply: not called CC=4 SCAR=30.0
411  
412 0 my $params = parse_httpmsg_form($http_response, 1);
413  
414 0 ▶ if($required_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_params) 0 0 0
415 0 foreach my $par_name (@$required_params) {
416 # FIXME: bind http_response
417 OAuthomatic::Error::Generic->throw(
418 ident => "Invalid reply obtained",
419 extra => "Missing required item in parsed reply: $par_name\n"
420 . "Reply items: " . join(", ", keys %$params). "\n")
421 0 ▶ unless $params->{$par_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$par_name} 0 0 0
422 }
423 }
424  
425 0 return $params;
426 }
427  
428 5 1;
429  
430 __END__
431  
432 =pod
433  
434 =encoding UTF-8
435  
436 =head1 NAME
437  
438 OAuthomatic::Caller - actually make OAuth-signed calls
439  
440 =head1 VERSION
441  
442 version 0.01
443  
444 =head1 DESCRIPTION
445  
446 Sign OAuth calls and execute them.
447  
448 This object is mostly used internally by L<OAuthomatic>, but may be useful
449 separately if you want to implement initialization scheme by yourself but
450 prefer it's API and structural exceptions to raw L<Net::OAuth>.
451  
452 =head1 METHODS
453  
454 =head2 create_authorization_url($callback_url) => TemporaryCred
455  
456 Calculates URL which user should visit to authorize app (and
457 associated temporary token).
458  
459 =head2 create_token_cred
460  
461 Acquires access token, preserves them in the object (so future calls
462 will be authenticated), and return (so it can be saved etc).
463  
464 =head2 build_oauth_request(method => ..., ...)
465  
466 Prepare properly signed L<HTTP::Request> but do not execute it, just
467 return ready-to-be-sent object.
468  
469 Parameters: identical as in L</execute_oauth_request>
470  
471 =head2 execute_oauth_request(method => $method, url => $url, url_args => $args,
472 body_form => $body_form, body => $body,
473 content_type => $content_type)
474  
475 Make a request to C<url> using the given HTTP method and signing
476 request with OAuth credentials.
477  
478 =over 4
479  
480 =item method
481  
482 One of C<GET>, C<POST>, C<PUT>, C<DELETE>.
483  
484 =item url
485  
486 Actual URL to call (C<http://some.site.com/api/...>)
487  
488 =item url_args (optional)
489  
490 Additional arguments to escape and add to the URL. This is simply shortcut,
491 three calls below are equivalent:
492  
493 $c->execute_oauth_request(method => "GET",
494 url => "http://some.where/api?x=1&y=2&z=a+b");
495  
496 $c->execute_oauth_request(method => "GET",
497 url => "http://some.where/api",
498 url_args => {x => 1, y => 2, z => 'a b'});
499  
500 $c->execute_oauth_request(method => "GET",
501 url => "http://some.where/api?x=1",
502 url_args => {y => 2, z => 'a b'});
503  
504 =item body_form OR body
505  
506 Exactly one of those must be specified for POST and PUT (none for GET or DELETE).
507  
508 Specifying C<body_form> means, that we are creating www-urlencoded
509 form. Parameters will be included in OAuth signature. Example:
510  
511 $c->execute_oauth_request(method => "POST",
512 url => "http://some.where/api",
513 body_form => {par1 => 'abc', par2 => 'd f'});
514  
515 Note that this is not just a shortcut for setting body to already
516 serialized form. Case of urlencoded form is treated in a special way
517 by OAuth (those values impact OAuth signature). To avoid signature
518 verification errors, OAuthomatic will reject such attempts:
519  
520 # WRONG AND WILL FAIL. Use body_form if you post form.
521 $c->execute_oauth_request(method => "POST",
522 url => "http://some.where/api",
523 body => 'par1=abc&par2=d+f',
524 content_type => 'application/x-www-form-urlencoded');
525  
526 Specifying C<body> means, that we post non-form body (for example
527 JSON, XML or even binary data). Example:
528  
529 $c->execute_oauth_request(method => "POST",
530 url => "http://some.where/api",
531 body => "<product><item-no>3434</item-no><price>334.22</price></product>",
532 content_type => "application/xml; charset=utf-8");
533  
534 Value of body can be either binary string (which will be posted as-is), or
535 perl unicode string (which will be encoded according to the content type, what by
536 default means utf-8).
537  
538 Such content is not covered by OAuth signature, so less secure (at
539 least if it is posted over non-SSL connection).
540  
541 For longer bodies, references are supported:
542  
543 $c->execute_oauth_request(method => "POST",
544 url => "http://some.where/api",
545 body => \$body_string,
546 content_type => "application/xml; charset=utf-8");
547  
548 =item content_type
549  
550 Used to set content type of the request. If missing, it is set to
551 C<text/plain; charset=utf-8> if C<body> param is specified and to
552 C<application/x-www-form-urlencoded; charset=utf-8> if C<body_form>
553 param is specified.
554  
555 Note that module author does not test behaviour on encodings different
556 than utf-8 (although they may work).
557  
558 =back
559  
560 =head2 _execute_oauth_request_ext
561  
562 Common code for API and OAuth-protocol calls. Uses all parameters
563 described in L</execute_oauth_request> and two additional:
564  
565 =over 4
566  
567 =item class
568  
569 ProtectedResource, UserAuth, RequestToken etc (XXX from
570 Net::Oauth::XXXXRequest)
571  
572 =item token
573  
574 Actual token to use while signing (skip to use only client token) -
575 either $self->token_cred, or some temporary_cred, depending on task at
576 hand.
577  
578 =back
579  
580 =head1 ATTRIBUTES
581  
582 =head2 config
583  
584 L<OAuthomatic::Config> object used to bundle various configuration params.
585  
586 =head2 server
587  
588 L<OAuthomatic::Server> object used to bundle server-related configuration params.
589  
590 =head1 INTERNAL METHODS
591  
592 =head2 _build_oauth_request_ext
593  
594 Common code for API and OAuth-protocol calls. Uses all parameters
595 described in L</execute_oauth_request> and some additional:
596  
597 =over 4
598  
599 =item class
600  
601 ProtectedResource, UserAuth, RequestToken etc (XXX from Net::Oauth::XXXXRequest)
602  
603 =item token
604  
605 Actual token to use while signing (skip to use only client token) - either $self->token_cred,
606 or some temporary_cred, depending on task at hand.
607  
608 =item verifier
609  
610 Verifier to be added to access token creation.
611  
612 =item callback
613  
614 Callback url for temporary token creation.
615  
616 =item auth_in_post
617  
618 True if authorization tokens are to be merged into POST body, false if
619 they are to be preserved in Authorize header.
620  
621 =back
622  
623 =head1 AUTHOR
624  
625 Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
626  
627 =head1 COPYRIGHT AND LICENSE
628  
629 This software is copyright (c) 2015 by Marcin Kasperski.
630  
631 This is free software; you can redistribute it and/or modify it under
632 the same terms as the Perl 5 programming language system itself.
633  
634 =cut
↑ Summary blib/lib/OAuthomatic.pm »