blib/lib/LWP/Authen/OAuth.pm

filter: Statementstmt 25.8% 32 / 124 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 44.4% 8 / 18 Podpod 63.6% 7 / 11 totaltotal 23.0% 47 / 204 CC 27 SCAR 43.7
CC
27
Cov
19%
sign_hmac_sha1
215.6
oauth_authorization_param
30.0
oauth_nonce
6.0
CRAP 79.0
↑ Summary
1 package LWP::Authen::OAuth;
2  
3 =head1 NAME
4  
5 LWP::Authen::OAuth - generate signed OAuth requests
6  
7 =head1 SYNOPSIS
8  
9 require LWP::Authen::OAuth;
10  
11 =head2 Google
12  
13 # Google uses 'anonymous' for unregistered Web/offline applications or the
14 # domain name for registered Web applications
15 my $ua = LWP::Authen::OAuth->new(
16 oauth_consumer_secret => "anonymous",
17 );
18
19 # request a 'request' token
20 my $r = $ua->post( "https://www.google.com/accounts/OAuthGetRequestToken",
21 [
22 oauth_consumer_key => 'anonymous',
23 oauth_callback => 'http://example.net/oauth',
24 xoauth_displayname => 'Example Application',
25 scope => 'https://docs.google.com/feeds/',
26 ]
27 );
28 die $r->as_string if $r->is_error;
29
30 # update the token secret from the HTTP response
31 $ua->oauth_update_from_response( $r );
32
33 # open a browser for the user
34
35 # data are returned as form-encoded
36 my $uri = URI->new( 'http:' );
37 $uri->query( $r->content );
38 my %oauth_data = $uri->query_form;
39
40 # Direct the user to here to grant you access:
41 # https://www.google.com/accounts/OAuthAuthorizeToken?
42 # oauth_token=$oauth_data{oauth_token}\n";
43
44 # turn the 'request' token into an 'access' token with the verifier
45 # returned by google
46 $r = $ua->post( "https://www.google.com/accounts/OAuthGetAccessToken", [
47 oauth_consumer_key => 'anonymous',
48 oauth_token => $oauth_data{oauth_token},
49 oauth_verifier => $oauth_verifier,
50 ]);
51
52 # update the token secret from the HTTP response
53 $ua->oauth_update_from_response( $r );
54
55 # now use the $ua to perform whatever actions you want
56  
57 =head2 Twitter
58  
59 Sending status updates to a single account is quite easy if you create an application. The C<oauth_consumer_key> and C<oauth_consumer_secret> come from the 'Application Details' page and the C<oauth_token> and C<oauth_token_secret> from the 'My Access Token' page.
60  
61 my $ua = LWP::Authen::OAuth->new(
62 oauth_consumer_key => 'xxx1',
63 oauth_consumer_secret => 'xxx2',
64 oauth_token => 'yyy1',
65 oauth_token_secret => 'yyy2',
66 );
67
68 $ua->post( 'http://api.twitter.com/1/statuses/update.json', [
69 status => 'Posted this using LWP::Authen::OAuth!'
70 ]);
71  
72 =head1 DESCRIPTION
73  
74 This module provides a sub-class of L<LWP::UserAgent> that generates OAuth 1.0 signed requests. You should familiarise yourself with OAuth at L<http://oauth.net/>.
75  
76 This module only supports HMAC_SHA1 signing.
77  
78 OAuth nonces are generated using the Perl random number generator. To set a nonce manually define 'oauth_nonce' in your requests via a CGI parameter or the Authorization header - see the OAuth documentation.
79  
80 =head1 METHODS
81  
82 =over 4
83  
84 =item $ua = LWP::Authen::OAuth->new( ... )
85  
86 Takes the same options as L<LWP::UserAgent/new> plus optionally:
87  
88 oauth_consumer_key
89 oauth_consumer_secret
90 oauth_token
91 oauth_token_secret
92  
93 Most services will require some or all of these to be set even if it's just 'anonymous'.
94  
95 =item $ua->oauth_update_from_response( $r )
96  
97 Update the C<oauth_token> and C<oauth_token_secret> from an L<HTTP::Response> object returned by a previous request e.g. when converting a request token into an access token.
98  
99 =item $key = $ua->oauth_consumer_key( [ KEY ] )
100  
101 Get and optionally set the consumer key.
102  
103 =item $secret = $ua->oauth_consumer_secret( [ SECRET ] )
104  
105 Get and optionally set the consumer secret.
106  
107 =item $token = $ua->oauth_token( [ TOKEN ] )
108  
109 Get and optionally set the oauth token.
110  
111 =item $secret = $ua->oauth_token_secret( [ SECRET ] )
112  
113 Get and optionally set the oauth token secret.
114  
115 =back
116  
117 =head1 SEE ALSO
118  
119 L<LWP::UserAgent>, L<MIME::Base64>, L<Digest::SHA>, L<URI>, L<URI::Escape>
120  
121 =head2 Rationale
122  
123 I think the complexity in OAuth is in the parameter normalisation and message signing. What this module does is to hide that complexity without replicating the higher-level protocol chatter.
124  
125 In Net::OAuth:
126  
127 $r = Net::OAuth->request('request token')->new(
128 consumer_key => 'xxx',
129 request_url => 'https://photos.example.net/request_token',
130 callback => 'http://printer.example.com/request_token_ready',
131 ...
132 extra_params {
133 scope => 'global',
134 }
135 );
136 $r->sign;
137 $res = $ua->request(POST $r->to_url);
138 $res = Net::OAuth->response('request token')
139 ->from_post_body($res->content);
140 ... etc
141  
142 In LWP::Authen::OAuth:
143  
144 $ua = LWP::Authen::OAuth->new(
145 oauth_consumer_key => 'xxx'
146 );
147 $res = $ua->post( 'https://photos.example.net/request_token', [
148 oauth_callback => 'http://printer.example.com/request_token_ready',
149 ...
150 scope => 'global',
151 ]);
152 $ua->oauth_update_from_response( $res );
153 ... etc
154  
155 L<Net::OAuth>, L<OAuth::Lite>.
156  
157 =head1 AUTHOR
158  
159 Timothy D Brody <tdb2@ecs.soton.ac.uk>
160  
161 Copyright 2011 University of Southampton, UK
162  
163 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself
164  
165 =cut
166  
167 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 1 $VERSION = '1.02';
174 1 @ISA = qw( LWP::UserAgent );
175  
176 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 sub new
179 1 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
180 1 my( $class, %self ) = @_;
181  
182 1 my %opts;
183 1 for(qw( oauth_consumer_key oauth_consumer_secret oauth_token oauth_token_secret ))
184 {
185 4 $opts{$_} = delete $self{$_};
186 }
187  
188 1 my $self = $class->SUPER::new( %self );
189  
190 1 for(keys %opts)
191 {
192 4 $self->{$_} = $opts{$_};
193 }
194  
195 1 return $self;
196 }
197  
198 sub request
199 0 ▶{
Subroutine request: not called CC=1 SCAR=6.9
Pod documented
200 0 my( $self, $request, @args ) = @_;
201  
202 0 $self->sign_hmac_sha1( $request );
203  
204 0 return $self->SUPER::request( $request, @args );
205 }
206  
207 sub oauth_encode_parameter
208 0 ▶{
Subroutine oauth_encode_parameter: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my( $str ) = @_;
210 0 return URI::Escape::uri_escape_utf8( $str, '^\w.~-' ); # 5.1
211 }
212  
213 sub oauth_nonce
214 0 ▶{
Subroutine oauth_nonce: not called CC=2 SCAR=17.9
Pod undocumented
215 0 my $nonce = '';
216 0 $nonce .= sprintf("%02x", int(rand(255))) for 1..16;
217 0 return $nonce;
218 }
219  
220 sub oauth_authorization_param
221 0 ▶{
Subroutine oauth_authorization_param: not called CC=5 SCAR=34.0
Pod undocumented
222 0 my( $request, @args ) = @_;
223  
224 0 ▶ if( @args )
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
225 {
226 0 my @parts;
227 0 ▶ for(my $i = 0; $i < @args; $i+=2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 0 0 0
228 {
229 # header values are in quotes
230 push @parts, sprintf('%s="%s"',
231 0 map { oauth_encode_parameter( $_ ) }
232 @args[$i,$i+1]
233 );
234 }
235 0 $request->header( 'Authorization', sprintf('OAuth %s',
236 join ',', @parts ) );
237 }
238  
239 0 my $authorization = $request->header( 'Authorization' );
240 0 ▶ return if !$authorization;
Branch: 1 branch
BranchTrueFalseTotal
unless $authorization 0 0 0
241 0 ▶ return if $authorization !~ s/^\s*OAuth\s+//i;
Branch: 1 branch
BranchTrueFalseTotal
unless $authorization =~ s/^\s*OAuth\s+//i 0 0 0
242  
243 return
244 0 map { URI::Escape::uri_unescape( $_ ) }
245 0 map { $_ =~ /([^=]+)="(.*)"/; ($1, $2) }
246 split /\s*,\s*/,
247 $authorization;
248 }
249  
250 sub sign_hmac_sha1
251 0 ▶{
Subroutine sign_hmac_sha1: not called CC=15 SCAR=53.7
Pod undocumented
252 0 my( $self, $request ) = @_;
253  
254 0 my $method = $request->method;
255 0 my $uri = URI->new( $request->uri )->canonical;
256 0 my $content_type = $request->header( 'Content-Type' );
257 0 ▶ $content_type = '' if !defined $content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_type 0 0 0
258 0 my $oauth_header = $request->header( "Authorization" );
259  
260 # build the parts of the string to sign
261 0 my @parts;
262  
263 0 push @parts, $method;
264  
265 0 my $request_uri = $uri->clone;
266 0 $request_uri->query( undef );
267 0 push @parts, "$request_uri";
268  
269 # build up a list of parameters
270 0 my @params;
271  
272 # CGI parameters (OAuth only supports urlencoded)
273 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" and $content_type eq "application/x-www-form-urlencoded") 0 0 0
Condition0%0/3
$method eq "POST" and $content_type eq "application/x-www-form-urlencoded"
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "POST" and $content_type eq "application/x-www-form-urlencoded"
$method eq "POST" $content_type eq "application/x-www-form-urlencoded"
Truth table0%0/3
A and B
A= $method eq "POST"B= $content_type eq "application/x-www-form-urlencoded"
ABresult
0X0
100
111
274 $method eq "POST" &&
275 $content_type eq 'application/x-www-form-urlencoded'
276 )
277 {
278 0 $uri->query( $request->content );
279 }
280
281 0 push @params, $uri->query_form;
282  
283 # HTTP OAuth Authorization parameters
284 0 my @auth_params = oauth_authorization_param( $request );
285 0 my %auth_params = @auth_params;
286 0 ▶ if( !exists($auth_params{oauth_nonce}) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $auth_params{"oauth_nonce"}) 0 0 0
287 {
288 0 push @auth_params, oauth_nonce => oauth_nonce();
289 }
290 0 ▶ if( !exists($auth_params{oauth_timestamp}) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $auth_params{"oauth_timestamp"}) 0 0 0
291 {
292 0 push @auth_params, oauth_timestamp => time();
293 }
294 0 ▶ if( !exists($auth_params{oauth_version}) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $auth_params{"oauth_version"}) 0 0 0
295 {
296 0 push @auth_params, oauth_version => '1.0';
297 }
298 0 for(qw( oauth_consumer_key oauth_token ))
299 {
300 0 ▶ if( !exists($auth_params{$_}) && defined($self->{$_}) )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $auth_params{$_} and defined $self->{$_}) 0 0 0
Condition0%0/3
not exists $auth_params{$_} and defined $self->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $auth_params{$_} and defined $self->{$_}
not exists $auth_params{$_} defined $self->{$_}
Truth table0%0/3
not(A) and B
A= not exists $auth_params{$_}B= defined $self->{$_}
ABresult
0X0
100
111
301 {
302 0 push @auth_params, $_ => $self->{$_};
303 }
304 }
305 0 push @auth_params, oauth_signature_method => "HMAC-SHA1";
306  
307 0 push @params, @auth_params;
308  
309 # lexically order the parameters as bytes (sorry for obscure code)
310 {
311 0 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.2
312 0 my @pairs;
313 0 ▶ push @pairs, [splice(@params,0,2)] while @params;
Branch: 1 branch
BranchTrueFalseTotal
@params and do { push @pairs, [splice(@params, 0, 2)] } 0 0 0
314 # order by key name then value
315 @pairs = sort {
316 0 ▶ $a->[0] cmp $b->[0] || $a->[1] cmp $b->[0]
Condition0%0/3
$LWP::Authen::OAuth::a->[0] cmp $LWP::Authen::OAuth::b->[0] || $LWP::Authen::OAuth::a->[1] cmp $LWP::Authen::OAuth::b->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$LWP::Authen::OAuth::a->[0] cmp $LWP::Authen::OAuth::b->[0] || $LWP::Authen::OAuth::a->[1] cmp $LWP::Authen::OAuth::b->[0]
$LWP::Authen::OAuth::a->[0] cmp $LWP::Authen::OAuth::b->[0] $LWP::Authen::OAuth::a->[1] cmp $LWP::Authen::OAuth::b->[0]
Truth table0%0/3
A || B
A= $LWP::Authen::OAuth::a->[0] cmp $LWP::Authen::OAuth::b->[0]B= $LWP::Authen::OAuth::a->[1] cmp $LWP::Authen::OAuth::b->[0]
ABresult
1X1
011
000
317 } @pairs;
318 0 @params = map { @$_ } @pairs;
319 }
320  
321 # re-encode the parameters according to OAuth spec.
322 0 my @query;
323 0 ▶ for(my $i = 0; $i < @params; $i+=2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @params) 0 0 0
324 {
325 0 ▶ next if $params[$i] eq "oauth_signature"; # 9.1.1
Branch: 1 branch
BranchTrueFalseTotal
if $params[$i] eq "oauth_signature" 0 0 0
326 push @query, sprintf('%s=%s',
327 0 map { oauth_encode_parameter( $_ ) }
328 @params[$i,$i+1]
329 );
330 }
331 0 push @parts, join '&', @query;
332  
333 # calculate the data to sign and the secret to use (encoded again)
334 my $data = join '&',
335 0 map { oauth_encode_parameter( $_ ) }
336 @parts;
337 my $secret = join '&',
338 0 ▶ map { defined($_) ? oauth_encode_parameter( $_ ) : '' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
339 $self->{oauth_consumer_secret},
340 0 $self->{oauth_token_secret};
341  
342 # 9.2
343 0 my $digest = Digest::SHA::hmac_sha1( $data, $secret );
344  
345 0 push @auth_params,
346 oauth_signature => MIME::Base64::encode_base64( $digest, '' );
347  
348 0 oauth_authorization_param( $request, @auth_params );
349 }
350  
351 sub oauth_update_from_response
352 0 ▶{
Subroutine oauth_update_from_response: not called CC=2 SCAR=17.9
Pod documented
353 0 my( $self, $r ) = @_;
354  
355 0 my $uri = URI->new( 'http:' );
356 0 $uri->query( $r->content );
357 0 my %oauth_data = $uri->query_form;
358  
359 0 for(qw( oauth_token oauth_token_secret ))
360 {
361 0 $self->{$_} = $oauth_data{$_};
362 }
363 }
364  
365 sub oauth_consumer_key
366 0 ▶{
Subroutine oauth_consumer_key: not called CC=2 SCAR=17.9
Pod documented
367 0 my $self = shift;
368 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
369 {
370 0 $self->{oauth_consumer_key} = shift;
371 }
372 0 return $self->{oauth_consumer_key};
373 }
374  
375 sub oauth_consumer_secret
376 0 ▶{
Subroutine oauth_consumer_secret: not called CC=2 SCAR=17.9
Pod documented
377 0 my $self = shift;
378 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
379 {
380 0 $self->{oauth_consumer_secret} = shift;
381 }
382 0 return $self->{oauth_consumer_secret};
383 }
384  
385 sub oauth_token
386 0 ▶{
Subroutine oauth_token: not called CC=2 SCAR=17.9
Pod documented
387 0 my $self = shift;
388 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
389 {
390 0 $self->{oauth_token} = shift;
391 }
392 0 return $self->{oauth_token};
393 }
394  
395 sub oauth_token_secret
396 0 ▶{
Subroutine oauth_token_secret: not called CC=2 SCAR=17.9
Pod documented
397 0 my $self = shift;
398 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
399 {
400 0 $self->{oauth_token_secret} = shift;
401 }
402 0 return $self->{oauth_token_secret};
403 }
404  
405 1 1;
↑ Summary