OAuth.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 52.9% 18 / 34 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 89.4% 17 / 19 Podpod 100% 2 / 2 totaltotal 76.4% 123 / 161 SCAR 32.6
CC
21
Cov
78%
authorization_header
24.7
_signer_plaintext
6.0
_signature_base_string
3.0
CRAP 25.9
« OAuth/Request/Mojo.pm ↑ Summary OAuth/Util.pm »
1 package WWW::OAuth;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 my %default_signer = (
7 'PLAINTEXT' => \&_signer_plaintext,
8 'HMAC-SHA1' => \&_signer_hmac_sha1,
9 );
10  
11 use Class::Tiny::Chained qw(client_id client_secret token token_secret), {
12 signature_method => 'HMAC-SHA1',
13 1 ▶ signer => sub { $default_signer{$_[0]->signature_method} },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Digest::SHA 'hmac_sha1_base64', 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use List::Util 'all', 'pairs', 'pairgrep';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use URI::Escape 'uri_escape_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use WWW::OAuth::Util 'oauth_request';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '0.006';
25  
26 sub authenticate {
27 1 ▶ my $self = shift;
Subroutine authenticate: called CC=2 SCAR=7.0
Pod documented
28 1 ▶ my @req_args = ref $_[0] ? shift() : (shift, shift);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
29 1 my $req = oauth_request(@req_args);
30
31 1 my $auth_header = $self->authorization_header($req, @_);
32
33 1 $req->header(Authorization => $auth_header);
34 1 return $req;
35 }
36  
37 sub authorization_header {
38 2 ▶ my $self = shift;
Subroutine authorization_header: called CC=15 SCAR=32.1
Pod documented
39 2 ▶ my @req_args = ref $_[0] ? shift() : (shift, shift);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
40 2 my $req = oauth_request(@req_args);
41 2 my $extra_params = shift;
42
43 2 my ($client_id, $client_secret, $token, $token_secret, $signature_method, $signer) =
44 ($self->client_id, $self->client_secret, $self->token, $self->token_secret, $self->signature_method, $self->signer);
45
46 2 ▶ croak 'Client ID and secret are required to generate authorization header'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client_id and defined $client_secret 0 2 2
Condition33%1/3
defined $client_id and defined $client_secret
!ll&&!rl&&r
002
MC/DC0%0/2
defined $client_id and defined $client_secret
defined $client_id defined $client_secret
Truth table33%1/3
A and B
A= defined $client_idB= defined $client_secret
ABresult
0X0
100
111
47 unless defined $client_id and defined $client_secret;
48
49 2 ▶ croak "Signer is required for signature method $signature_method" unless defined $signer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $signer 0 2 2
50
51 2 ▶ if ($signature_method eq 'RSA-SHA1' and blessed $signer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature_method eq "RSA-SHA1" and builtin::blessed($signer)) 0 2 2
Condition33%1/3
$signature_method eq "RSA-SHA1" and builtin::blessed($signer)
!ll&&!rl&&r
200
MC/DC0%0/2
$signature_method eq "RSA-SHA1" and builtin::blessed($signer)
$signature_method eq "RSA-SHA1" builtin::blessed($signer)
Truth table33%1/3
A and B
A= $signature_method eq "RSA-SHA1"B= builtin::blessed($signer)
ABresult
0X0
100
111
52 0 my $signer_obj = $signer;
53 0 ▶ croak 'Signer for RSA-SHA1 must have "sign" method' unless $signer_obj->can('sign');
Branch: 1 branch
BranchTrueFalseTotal
unless $signer_obj->can("sign") 0 0 0
54 0 ▶ $signer = sub { $signer_obj->sign($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 }
56 2 ▶ croak "Signer for $signature_method must be a coderef" unless !blessed $signer and ref $signer eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless not builtin::blessed($signer) and ref $signer eq "CODE" 0 2 2
Condition33%1/3
not builtin::blessed($signer) and ref $signer eq "CODE"
!ll&&!rl&&r
002
MC/DC0%0/2
not builtin::blessed($signer) and ref $signer eq "CODE"
not builtin::blessed($signer) ref $signer eq "CODE"
Truth table33%1/3
not(A) and B
A= not builtin::blessed($signer)B= ref $signer eq "CODE"
ABresult
0X0
100
111
57
58 2 my %oauth_params = (
59 oauth_consumer_key => $client_id,
60 oauth_nonce => _nonce(),
61 oauth_signature_method => $signature_method,
62 oauth_timestamp => time,
63 oauth_version => '1.0',
64 );
65 2 ▶ $oauth_params{oauth_token} = $token if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 1 1 2
66
67 # Extra parameters passed to authenticate()
68 2 ▶ if (defined $extra_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extra_params) 1 1 2
69 1 ▶ croak 'OAuth parameters must be specified as a hashref' unless ref $extra_params eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extra_params eq "HASH" 0 1 1
70 1 ▶ croak 'OAuth parameters must all begin with "oauth_"' unless all { m/^oauth_/ } keys %$extra_params;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { /^oauth_/; } , keys %$extra_params) 0 1 1
71 1 %oauth_params = (%oauth_params, %$extra_params);
72 }
73
74 # This parameter is not allowed when creating the signature
75 2 delete $oauth_params{oauth_signature};
76
77 # Don't bother to generate signature base string for PLAINTEXT method
78 2 ▶ my $base_str = $signature_method eq 'PLAINTEXT' ? '' : _signature_base_string($req, \%oauth_params);
Branch: 1 branch
BranchTrueFalseTotal
$signature_method eq 'PLAINTEXT' ? : 0 2 2
79 2 $oauth_params{oauth_signature} = $signer->($base_str, $client_secret, $token_secret);
80
81 2 my $auth_str = join ', ', map { $_ . '="' . uri_escape_utf8($oauth_params{$_}) . '"' } sort keys %oauth_params;
82 2 return "OAuth $auth_str";
83 }
84  
85 2 ▶sub _nonce { sha1_hex join '$', \my $dummy, time, $$, rand }
Subroutine _nonce: called CC=1 SCAR=0.0
86  
87 sub _signer_plaintext {
88 0 ▶ my ($base_str, $client_secret, $token_secret) = @_;
Subroutine _signer_plaintext: not called CC=2 SCAR=17.9
89 0 ▶ $token_secret = '' unless defined $token_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token_secret 0 0 0
90 0 return uri_escape_utf8($client_secret) . '&' . uri_escape_utf8($token_secret);
91 }
92  
93 sub _signer_hmac_sha1 {
94 2 ▶ my ($base_str, $client_secret, $token_secret) = @_;
Subroutine _signer_hmac_sha1: called CC=3 SCAR=11.0
95 2 ▶ $token_secret = '' unless defined $token_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token_secret 1 1 2
96 2 my $signing_key = uri_escape_utf8($client_secret) . '&' . uri_escape_utf8($token_secret);
97 2 my $digest = hmac_sha1_base64($base_str, $signing_key);
98 2 ▶ $digest .= '='x(4 - length($digest) % 4) if length($digest) % 4; # Digest::SHA does not pad Base64 digests
Branch: 1 branch
BranchTrueFalseTotal
if length($digest) % 4 2 0 2
99 2 return $digest;
100 }
101  
102 sub _signature_base_string {
103 2 ▶ my ($req, $oauth_params) = @_;
Subroutine _signature_base_string: called CC=3 SCAR=11.1
104
105 2 my @all_params = (@{$req->query_pairs}, %$oauth_params);
106 2 ▶ push @all_params, @{$req->body_pairs} if $req->content_is_form;
Branch: 1 branch
BranchTrueFalseTotal
if $req->content_is_form 0 2 2
107 2 my @pairs = pairs map { uri_escape_utf8 $_ } @all_params;
108 2 ▶ @pairs = sort { ($a->[0] cmp $b->[0]) or ($a->[1] cmp $b->[1]) } @pairs;
Branch: 1 branch
BranchTrueFalseTotal
$WWW::OAuth::a->[0] cmp $WWW::OAuth::b->[0] or $WWW::OAuth::a->[1] cmp $WWW::OAuth::b->[1] 0 19 19
109 2 my $params_str = join '&', map { $_->[0] . '=' . $_->[1] } @pairs;
110
111 2 my $base_url = URI->new($req->url);
112 2 $base_url->query(undef);
113 2 $base_url->fragment(undef);
114 2 return uc($req->method) . '&' . uri_escape_utf8($base_url) . '&' . uri_escape_utf8($params_str);
115 }
116  
117 1 1;
118  
119 =head1 NAME
120  
121 WWW::OAuth - Portable OAuth 1.0 authentication
122  
123 =head1 SYNOPSIS
124  
125 use WWW::OAuth;
126
127 my $oauth = WWW::OAuth->new(
128 client_id => $client_id,
129 client_secret => $client_secret,
130 token => $token,
131 token_secret => $token_secret,
132 );
133
134 # Just retrieve authorization header
135 my $auth_header = $oauth->authorization_header($http_request, { oauth_callback => $url });
136 $http_request->header(Authorization => $auth_header);
137
138 # HTTP::Tiny
139 use HTTP::Tiny;
140 my $res = $oauth->authenticate(Basic => { method => 'GET', url => $url })
141 ->request_with(HTTP::Tiny->new);
142
143 # HTTP::Request
144 use HTTP::Request::Common;
145 use LWP::UserAgent;
146 my $res = $oauth->authenticate(GET $url)->request_with(LWP::UserAgent->new);
147
148 # Mojo::Message::Request
149 use Mojo::UserAgent;
150 my $tx = $ua->build_tx(get => $url);
151 $tx = $oauth->authenticate($tx->req)->request_with(Mojo::UserAgent->new);
152
153 =head1 DESCRIPTION
154  
155 L<WWW::OAuth> implements OAuth 1.0 request authentication according to
156 L<RFC 5849|http://tools.ietf.org/html/rfc5849> (sometimes referred to as OAuth
157 1.0A). It does not implement the user agent requests needed for the complete
158 OAuth 1.0 authorization flow; it only prepares and signs requests, leaving the
159 rest up to your application. It can authenticate requests for
160 L<LWP::UserAgent>, L<Mojo::UserAgent>, L<HTTP::Tiny>, and can be extended to
161 operate on other types of requests.
162  
163 Some user agents can be configured to automatically authenticate each request
164 with a L<WWW::OAuth> object.
165  
166 # LWP::UserAgent
167 my $ua = LWP::UserAgent->new;
168 $ua->add_handler(request_prepare => sub { $oauth->authenticate($_[0]) });
169
170 # Mojo::UserAgent
171 my $ua = Mojo::UserAgent->new;
172 $ua->on(start => sub { $oauth->authenticate($_[1]->req) });
173  
174 =head1 RETRIEVING ACCESS TOKENS
175  
176 The process of retrieving access tokens and token secrets for authorization on
177 behalf of a user may differ among various APIs, but it follows this general
178 format (error checking is left as an exercise to the reader):
179  
180 use WWW::OAuth;
181 use WWW::OAuth::Util 'form_urldecode';
182 use HTTP::Tiny;
183 my $ua = HTTP::Tiny->new;
184 my $oauth = WWW::OAuth->new(
185 client_id => $client_id,
186 client_secret => $client_secret,
187 );
188
189 # Request token request
190 my $res = $oauth->authenticate({ method => 'POST', url => $request_token_url },
191 { oauth_callback => $callback_url })->request_with($ua);
192 my %res_data = @{form_urldecode $res->{content}};
193 my ($request_token, $request_secret) = @res_data{'oauth_token','oauth_token_secret'};
194
195 Now, the returned request token must be used to construct a URL for the user to
196 go to and authorize your application. The exact method differs by API. The user
197 will usually be redirected to the C<$callback_url> passed earlier after
198 authorizing, with a verifier token that can be used to retrieve the access
199 token and secret.
200
201 # Access token request
202 $oauth->token($request_token);
203 $oauth->token_secret($request_secret);
204 my $res = $oauth->authenticate({ method => 'POST', url => $access_token_url },
205 { oauth_verifier => $verifier_token })->request_with($ua);
206 my %res_data = @{form_urldecode $res->{content}};
207 my ($access_token, $access_secret) = @res_data{'oauth_token','oauth_token_secret'};
208
209 Finally, the access token and secret can now be stored and used to authorize
210 your application on behalf of this user.
211  
212 $oauth->token($access_token);
213 $oauth->token_secret($access_secret);
214  
215 =head1 ATTRIBUTES
216  
217 L<WWW::OAuth> implements the following attributes.
218  
219 =head2 client_id
220  
221 my $client_id = $oauth->client_id;
222 $oauth = $oauth->client_id($client_id);
223  
224 Client ID used to identify application (sometimes called an API key or consumer
225 key). Required for all requests.
226  
227 =head2 client_secret
228  
229 my $client_secret = $oauth->client_secret;
230 $oauth = $oauth->client_secret($client_secret);
231  
232 Client secret used to authenticate application (sometimes called an API secret
233 or consumer secret). Required for all requests.
234  
235 =head2 token
236  
237 my $token = $oauth->token;
238 $oauth = $oauth->token($token);
239  
240 Request or access token used to identify resource owner. Leave undefined for
241 temporary credentials requests (request token requests).
242  
243 =head2 token_secret
244  
245 my $token_secret = $oauth->token_secret;
246 $oauth = $oauth->token_secret($token_secret);
247  
248 Request or access token secret used to authenticate on behalf of resource
249 owner. Leave undefined for temporary credentials requests (request token
250 requests).
251  
252 =head2 signature_method
253  
254 my $method = $oauth->signature_method;
255 $oauth = $oauth->signature_method($method);
256  
257 Signature method, can be C<PLAINTEXT>, C<HMAC-SHA1>, C<RSA-SHA1>, or a custom
258 signature method. For C<RSA-SHA1> or custom signature methods, a L</"signer">
259 must be provided. Defaults to C<HMAC-SHA1>.
260  
261 =head2 signer
262  
263 my $signer = $oauth->signer;
264 $oauth = $oauth->signer(sub {
265 my ($base_str, $client_secret, $token_secret) = @_;
266 ...
267 return $signature;
268 });
269  
270 Coderef which implements the L</"signature_method">. A default signer is
271 provided for signature methods C<PLAINTEXT> and C<HMAC-SHA1>; this attribute is
272 required for other signature methods. For signature method C<RSA-SHA1>, this
273 attribute may also be an object which has a C<sign> method like
274 L<Crypt::OpenSSL::RSA>.
275  
276 The signer is passed the computed signature base string, the client secret, and
277 (if present) the token secret, and must return the signature string.
278  
279 =head1 METHODS
280  
281 L<WWW::OAuth> implements the following methods.
282  
283 =head2 authenticate
284  
285 $container = $oauth->authenticate($container, \%oauth_params);
286 my $container = $oauth->authenticate($http_request, \%oauth_params);
287 my $container = $oauth->authenticate(Basic => { method => 'GET', url => $url }, \%oauth_params);
288  
289 Wraps the HTTP request in a container with L<WWW::OAuth::Util/"oauth_request">,
290 then sets the Authorization header using L</"authorization_header"> to sign the
291 request for OAuth 1.0. An optional hashref of OAuth parameters will be passed
292 through to L</"authorization_header">. Returns the container object.
293  
294 =head2 authorization_header
295  
296 my $auth_header = $oauth->authorization_header($container, \%oauth_params);
297 my $auth_header = $oauth->authorization_header($http_request, \%oauth_params);
298 my $auth_header = $oauth->authorization_header(Basic => { method => 'GET', url => $url }, \%oauth_params);
299  
300 Forms an OAuth 1.0 signed Authorization header for the passed request. As in
301 L</"authenticate">, the request may be specified in any form accepted by
302 L<WWW::OAuth::Util/"oauth_request">. OAuth parameters (starting with C<oauth_>)
303 may be optionally specified in a hashref and will override any generated OAuth
304 parameters of the same name (they should not be present in the request URL or
305 body parameters). Returns the signed header value.
306  
307 =head1 HTTP REQUEST CONTAINERS
308  
309 Request containers provide a unified interface for L</"authenticate"> to parse
310 and update HTTP requests. They must perform the L<Role::Tiny> role
311 L<WWW::OAuth::Request>. Custom container classes can be instantiated
312 directly or via L<WWW::OAuth::Util/"oauth_request">.
313  
314 =head2 Basic
315  
316 L<WWW::OAuth::Request::Basic> contains the request attributes directly, for
317 user agents such as L<HTTP::Tiny> that do not use request objects.
318  
319 =head2 HTTP_Request
320  
321 L<WWW::OAuth::Request::HTTP_Request> wraps a L<HTTP::Request> object, which
322 is compatible with several user agents including L<LWP::UserAgent>,
323 L<HTTP::Thin>, and L<Net::Async::HTTP>.
324  
325 =head2 Mojo
326  
327 L<WWW::OAuth::Request::Mojo> wraps a L<Mojo::Message::Request> object,
328 which is used by L<Mojo::UserAgent> via L<Mojo::Transaction>.
329  
330 =head1 BUGS
331  
332 Report any issues on the public bugtracker.
333  
334 =head1 AUTHOR
335  
336 Dan Book <dbook@cpan.org>
337  
338 =head1 COPYRIGHT AND LICENSE
339  
340 This software is Copyright (c) 2015 by Dan Book.
341  
342 This is free software, licensed under:
343  
344 The Artistic License 2.0 (GPL Compatible)
345  
346 =head1 SEE ALSO
347  
348 L<Net::OAuth>, L<Mojolicious::Plugin::OAuth2>
« OAuth/Request/Mojo.pm ↑ Summary OAuth/Util.pm »