OAuth.pm

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