OAuth.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 8 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 20.0% 1 / 5 totaltotal 88.7% 63 / 71 CC 8 SCAR 10.6
CC
8
Cov
96%
smart_require
5.0
camel
3.0
message
2.0
CRAP 2.9
« OAuth/SignatureMethod/RSA_SHA1.pm ↑ Summary OAuth/V1_0A/UserAuthResponse.pm »
1 package Net::OAuth;
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub PROTOCOL_VERSION_1_0() {1}
7 sub PROTOCOL_VERSION_1_0A() {1.001}
8  
9 sub OAUTH_VERSION() {'1.0'}
10  
11 13 our $VERSION = '0.32';
12 13 our $SKIP_UTF8_DOUBLE_ENCODE_CHECK = 0;
13 13 our $PROTOCOL_VERSION = PROTOCOL_VERSION_1_0;
14  
15 sub request {
16 18 ▶ my $self = shift;
Subroutine request: called CC=1 SCAR=0.0
Pod undocumented
17 18 my $what = shift;
18 18 return $self->message($what . ' Request');
19 }
20  
21 sub response {
22 5 ▶ my $self = shift;
Subroutine response: called CC=1 SCAR=0.0
Pod undocumented
23 5 my $what = shift;
24 5 return $self->message($what . ' Response');
25 }
26  
27 sub message {
28 24 ▶ my $self = shift;
Subroutine message: called CC=2 SCAR=7.1
Pod documented
29 24 ▶ my $base_class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0240
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
30 24 my $type = camel(shift);
31 24 my $class = $base_class . '::' . $type;
32 24 smart_require($class, 1);
33 23 return $class;
34 }
35  
36 sub camel {
37 24 ▶ my @words;
Subroutine camel: called CC=3 SCAR=11.0
Pod undocumented
38 24 foreach (@_) {
39 24 ▶ while (/([A-Za-z0-9]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/([A-Za-z0-9]+)/g) 74 24 98
40 74 (my $word = $1) =~ s/authentication/auth/i;
41 74 push @words, $word;
42 }
43 }
44 24 my $name = join('', map("\u$_", @words));
45 }
46  
47 13 our %ALREADY_REQUIRED = (); # class_name => rv of ->require
48  
49 sub smart_require {
50 82 ▶ my $required_class = shift;
Subroutine smart_require: called CC=5 SCAR=16.1
Pod undocumented
51 82 ▶ my $croak_on_error = shift || 0;
Condition100%2/2
shift() || 0
l!l
2458
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
52 82 ▶ unless (exists $ALREADY_REQUIRED{$required_class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Net::OAuth::ALREADY_REQUIRED{$required_class}) 35 47 82
53 35 my $rv = eval "require $required_class";
54 35 my $error = $@;
55 35 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 3 32 35
56 3 ▶ croak $error if $croak_on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $croak_on_error 1 2 3
57 2 return 0;
58 }
59 32 $ALREADY_REQUIRED{$required_class} = $rv;
60 }
61 79 return $ALREADY_REQUIRED{$required_class};
62 }
63  
64 =encoding utf8
65  
66 =head1 NAME
67  
68 Net::OAuth - OAuth 1.0 for Perl
69  
70 =head1 SYNOPSIS
71  
72 use Net::OAuth::Client;
73  
74 # client_id is the Consumer Key, client_secret the Consumer Secret.
75 # Framework-agnostic: supply your own redirect and session handling.
76  
77 my $client = Net::OAuth::Client->new(
78 $client_id,
79 $client_secret,
80 site => 'https://provider.example/',
81 request_token_path => '/oauth/request_token',
82 authorize_path => '/oauth/authorize',
83 access_token_path => '/oauth/access_token',
84 callback => 'https://you.example/auth/callback',
85 session => \&session,
86 );
87  
88 # 1. Send the user to the provider to authorize.
89 $client->authorize_url;
90  
91 # 2. They return to your callback with oauth_token and oauth_verifier.
92 my $access_token = $client->get_access_token($token, $verifier);
93  
94 # 3. Use the access token to fetch a protected resource.
95 my $response = $access_token->get('/profile');
96 die $response->status_line unless $response->is_success;
97 print $response->decoded_content;
98  
99 =head1 IMPORTANT
100  
101 Net::OAuth provides a low-level API for reading and writing OAuth messages.
102  
103 You probably should start with L<Net::OAuth::Client>.
104  
105 =head1 ABSTRACT
106  
107 OAuth is
108  
109 "An open protocol to allow secure API authentication in a simple and standard method from desktop and web applications."
110  
111 In practical terms, OAuth is a mechanism for a Consumer to request protected resources from a Service Provider on behalf of a user.
112  
113 Please refer to the OAuth spec: L<http://oauth.net/documentation/spec>
114  
115 Net::OAuth provides:
116  
117 =over
118  
119 =item * classes that encapsulate OAuth messages (requests and responses).
120  
121 =item * message signing
122  
123 =item * message serialization and parsing.
124  
125 =item * 2-legged requests (aka. tokenless requests, aka. consumer requests), see L</"CONSUMER REQUESTS">
126  
127 =back
128  
129 Net::OAuth does not provide:
130  
131 =over
132  
133 =item * Consumer or Service Provider encapsulation
134  
135 =item * token/nonce/key storage/management
136  
137 =back
138  
139 =head1 DESCRIPTION
140  
141 =head2 OAUTH MESSAGES
142  
143 An OAuth message is a set of key-value pairs. The following message types are supported:
144  
145 Requests
146  
147 =over
148  
149 =item * Request Token (Net::OAuth::RequestTokenRequest)
150  
151 =item * Access Token (Net::OAuth::AccessTokenRequest)
152  
153 =item * User Authentication (Net::OAuth::UserAuthRequest)
154  
155 =item * Protected Resource (Net::OAuth::ProtectedResourceRequest)
156  
157 =item * Consumer Request (Net::OAuth::ConsumerRequest) (2-legged / token-less request)
158  
159 =back
160  
161 Responses
162  
163 =over
164  
165 =item * Request Token (Net::OAuth::RequestTokenResponse)
166  
167 =item * Access Token (Net::OAuth:AccessTokenResponse)
168  
169 =item * User Authentication (Net::OAuth::UserAuthResponse)
170  
171 =back
172  
173 Each OAuth message type has one or more required parameters, zero or more optional parameters, and most allow arbitrary parameters.
174  
175 All OAuth requests must be signed by the Consumer. Responses from the Service Provider, however, are not signed.
176  
177 To create a message, the easiest way is to use the factory methods (Net::OAuth->request, Net::OAuth->response, Net::OAuth->message). The following method invocations are all equivalent:
178  
179 $request = Net::OAuth->request('user authentication')->new(%params);
180 $request = Net::OAuth->request('user_auth')->new(%params);
181 $request = Net::OAuth->request('UserAuth')->new(%params);
182 $request = Net::OAuth->message('UserAuthRequest')->new(%params);
183  
184 The more verbose way is to use the class directly:
185  
186 use Net::OAuth::UserAuthRequest;
187 $request = Net::OAuth::UserAuthRequest->new(%params);
188  
189 You can also create a message by deserializing it from a Authorization header, URL, query hash, or POST body
190  
191 $request = Net::OAuth->request('protected resource')->from_authorization_header($ENV{HTTP_AUTHORIZATION}, %api_params);
192 $request = Net::OAuth->request('protected resource')->from_url($url, %api_params);
193 $request = Net::OAuth->request('protected resource')->from_hash({$q->Vars}, %api_params); # CGI
194 $request = Net::OAuth->request('protected resource')->from_hash($c->request->params, %api_params); # Catalyst
195 $response = Net::OAuth->response('request token')->from_post_body($response_content, %api_params);
196  
197 Note that the deserialization methods (as opposed to new()) expect OAuth protocol parameters to be prefixed with 'oauth_', as you would expect in a valid OAuth message.
198  
199 Before sending a request, the Consumer must first sign it:
200  
201 $request->sign;
202  
203 When receiving a request, the Service Provider should first verify the signature:
204  
205 die "Signature verification failed" unless $request->verify;
206  
207 When sending a message the last step is to serialize it and send it to wherever it needs to go. The following serialization methods are available:
208  
209 $response->to_post_body # a application/x-www-form-urlencoded POST body
210  
211 $request->to_url # the query string of a URL
212  
213 $request->to_authorization_header # the value of an HTTP Authorization header
214  
215 $request->to_hash # a hash that could be used for some other serialization
216  
217 =head2 API PARAMETERS vs MESSAGE PARAMETERS
218  
219 Net::OAuth defines 'message parameters' as parameters that are part of the transmitted OAuth message. These include any protocol parameter (prefixed with 'oauth_' in the message), and any additional message parameters (the extra_params hash).
220  
221 'API parameters' are parameters required to build a message object that are not transmitted with the message, e.g. consumer_secret, token_secret, request_url, request_method.
222  
223 There are various methods to inspect a message class to see what parameters are defined:
224  
225 $request->required_message_params;
226 $request->optional_message_params;
227 $request->all_message_params;
228 $request->required_api_params;
229 $request->optional_api_params;
230 $request->all_api_params;
231 $request->all_params;
232  
233 E.g.
234  
235 use Net::OAuth;
236 use Data::Dumper;
237 print Dumper(Net::OAuth->request("protected resource")->required_message_params);
238  
239 $VAR1 = [
240 'consumer_key',
241 'signature_method',
242 'timestamp',
243 'nonce',
244 'token'
245 ];
246  
247 =head2 ACCESSING PARAMETERS
248  
249 All parameters can be get/set using accessor methods. E.g.
250  
251 my $consumer_key = $request->consumer_key;
252 $request->request_method('POST');
253  
254 =head2 THE REQUEST_URL PARAMETER
255  
256 Any query parameters in the request_url are removed and added to the extra_params hash when generating the signature.
257  
258 E.g. the following requests are pretty much equivalent:
259  
260 my $request = Net::OAuth->request('Request Token')->new(
261 %params,
262 request_url => 'https://photos.example.net/request_token',
263 extra_params => {
264 foo => 'bar'
265 },
266 );
267  
268 my $request = Net::OAuth->request('Request Token')->new(
269 %params,
270 request_url => 'https://photos.example.net/request_token?foo=bar',
271 );
272  
273 Calling $request->request_url will still return whatever you set it to originally. If you want to get the request_url with the query parameters removed, you can do:
274  
275 my $url = $request->normalized_request_url;
276  
277 =head2 SIGNATURE METHODS
278  
279 The following signature methods are supported:
280  
281 =over
282  
283 =item * PLAINTEXT
284  
285 =item * HMAC-SHA1
286  
287 =item * HMAC-SHA256
288  
289 =item * RSA-SHA1
290  
291 =back
292  
293 The signature method is determined by the value of the signature_method parameter that is passed to the message constructor.
294  
295 If an unknown signature method is specified, the signing/verification will throw an exception.
296  
297 =head3 PLAINTEXT SIGNATURES
298  
299 This method is a trivial signature which adds no security. Not recommended.
300  
301 =head3 HMAC-SHA1 SIGNATURES
302  
303 This method is available if you have Digest::SHA installed. This is by far the most commonly used method.
304  
305 =head3 HMAC-SHA256 SIGNATURES
306  
307 This method is available if you have Digest::SHA installed.
308  
309 =head3 RSA-SHA1 SIGNATURES
310  
311 To use RSA-SHA1 signatures, pass in a Crypt::OpenSSL::RSA object (or any object that can do $o->sign($str) and/or $o->verify($str, $sig))
312  
313 E.g.
314  
315 Consumer:
316  
317 use Crypt::OpenSSL::RSA;
318 use File::Slurp;
319 $keystring = read_file('private_key.pem');
320 $private_key = Crypt::OpenSSL::RSA->new_private_key($keystring);
321 $request = Net::OAuth->request('request token')->new(%params);
322 $request->sign($private_key);
323  
324 Service Provider:
325  
326 use Crypt::OpenSSL::RSA;
327 use File::Slurp;
328 $keystring = read_file('public_key.pem');
329 $public_key = Crypt::OpenSSL::RSA->new_public_key($keystring);
330 $request = Net::OAuth->request('request token')->new(%params);
331 if (!$request->verify($public_key)) {
332 die "Signature verification failed";
333 }
334  
335 Note that you can pass the key in as a parameter called 'signature_key' to the message constructor, rather than passing it to the sign/verify method, if you like.
336  
337 =head2 CONSUMER REQUESTS
338  
339 To send a request without including a token, use a Consumer Request:
340  
341 my $request = Net::OAuth->request('consumer')->new(
342 consumer_key => 'dpf43f3p2l4k3l03',
343 consumer_secret => 'kd94hf93k423kf44',
344 request_url => 'http://provider.example.net/profile',
345 request_method => 'GET',
346 signature_method => 'HMAC-SHA1',
347 timestamp => '1191242096',
348 nonce => 'kllo9940pd9333jh',
349 );
350  
351 $request->sign;
352  
353 See L<Net::OAuth::ConsumerRequest>
354  
355 =head2 I18N
356  
357 Per the OAuth spec, when making the signature Net::OAuth first encodes parameters to UTF-8. This means that any parameters you pass to Net::OAuth, if they might be outside of ASCII character set, should be run through Encode::decode() (or an equivalent PerlIO layer) first to decode them to Perl's internal character structure.
358  
359 =head2 OAUTH 1.0A
360  
361 Background:
362  
363 L<http://mojodna.net/2009/05/20/an-idiots-guide-to-oauth-10a.html>
364  
365 L<http://oauth.googlecode.com/svn/spec/core/1.0a/drafts/3/oauth-core-1_0a.html>
366  
367 Net::OAuth defaults to OAuth 1.0 spec compliance, and supports OAuth 1.0 Rev A with an optional switch:
368  
369 use Net::OAuth
370 $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
371  
372 It is recommended that any new projects use this switch if possible, and existing projects move to supporting this switch as soon as possible. Probably the easiest way for existing projects to do this is to turn on the switch and run your test suite. The Net::OAuth constructor will throw an exception where the new protocol parameters (callback, callback_confirmed, verifier) are missing.
373  
374 Internally, the Net::OAuth::Message constructor checks $Net::OAuth::PROTOCOL_VERSION and attempts to load the equivalent subclass in the Net::OAuth::V1_0A:: namespace. So if you instantiate a Net::OAuth::RequestTokenRequest object, you will end up with a Net::OAuth::V1_0A::RequestTokenRequest (a subclass of Net::OAuth::RequestTokenRequest) if the protocol version is set to PROTOCOL_VERSION_1_0A. You can also select a 1.0a subclass on a per-message basis by passing
375  
376 protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0A
377  
378 in the API parameters hash.
379  
380 If you are not sure whether the entity you are communicating with is 1.0A compliant, you can try instantiating a 1.0A message first and then fall back to 1.0 if that fails:
381  
382 use Net::OAuth
383 $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
384 my $is_oauth_1_0 = 0;
385 my $response = eval{Net::OAuth->response('request token')->from_post_body($res->content)};
386 if ($@) {
387 if ($@ =~ /Missing required parameter 'callback_confirmed'/) {
388 # fall back to OAuth 1.0
389 $response = Net::OAuth->response('request token')->from_post_body(
390 $res->content,
391 protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0
392 );
393 $is_oauth_1_0 = 1; # from now on treat the server as OAuth 1.0 compliant
394 }
395 else {
396 die $@;
397 }
398 }
399  
400 At some point in the future, Net::OAuth will default to Net::OAuth::PROTOCOL_VERSION_1_0A.
401  
402 =head1 SEE ALSO
403  
404 L<http://oauth.net>
405  
406 Check out L<Net::OAuth::Simple> - it has a simpler API that may be more to your liking
407  
408 Check out L<Net::Twitter::OAuth> for a Twitter-specific OAuth API
409  
410 Check out L<WWW::Netflix::API> for a Netflix-specific OAuth API
411  
412 =head1 TODO
413  
414 =over
415  
416 =item * Support for repeating/multivalued parameters
417  
418 =item * Add convenience methods for SPs
419  
420 Something like:
421  
422 # direct from CGI.pm object
423 $request = Net::OAuth->request('Request Token')->from_cgi_query($cgi, %api_params);
424  
425 # direct from Catalyst::Request object
426 $request = Net::OAuth->request('Request Token')->from_catalyst_request($c->req, %api_params);
427  
428 # from Auth header and GET and POST params in one
429 local $/;
430 my $post_body = <STDIN>;
431 $request = Net::OAuth->request('Request Token')->from_auth_get_and_post(
432 $ENV{HTTP_AUTHORIZATION},
433 $ENV{QUERY_STRING},
434 $post_body,
435 %api_params
436 );
437  
438 =back
439  
440 =head1 AUTHOR
441  
442 Originally by Keith Grennan <foss@nearlyfree.org>
443  
444 Currently maintained by Robert Rothenberg <perl@rhizomnic.com>
445  
446 =head1 CONTRIBUTORS
447  
448 Adam Taylor
449  
450 Brad Whitaker
451  
452 Cosimo Streppone <cosimo@cpan.org>
453  
454 Daisuke Maki
455  
456 Gregor Herrmann
457  
458 James Raspass <jraspass@gmail.com>
459  
460 Jeff Dairiki
461  
462 Jens Rehsack
463  
464 Katou Akira
465  
466 Masayoshi Sekimura
467  
468 Marc Mims
469  
470 Mike Schleif
471  
472 Nobuo Danjou
473  
474 Pattawan Gerlings-Kaewduangdee (oiami)
475  
476 Sergey Romanov <sromanov-dev@yandex.ru>
477  
478 Shawn Moore <cpan@sartak.org>
479  
480 Simon Wistow
481  
482 Tomaž Šolc <avian@cpan.org>
483  
484 =head1 COPYRIGHT & LICENSE
485  
486 Copyright 2007-2012, 2024-2025 Keith Grennan
487  
488 This program is free software; you can redistribute it and/or modify it
489 under the same terms as Perl itself.
490  
491 =cut
492  
493 13 1;
« OAuth/SignatureMethod/RSA_SHA1.pm ↑ Summary OAuth/V1_0A/UserAuthResponse.pm »