OAuth/Client.pm

filter: Statementstmt 85.3% 111 / 130 Branchbran 50.0% 15 / 30 Conditioncond 40.7% 11 / 27 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 87.5% 21 / 24 Podpod 10.0% 1 / 10 totaltotal 68.3% 162 / 237 CC 28 SCAR 17.5
CC
28
Cov
73%
authorize_url
20.0
_parse_oauth_response
15.6
_parse_url_encoding
4.1
CRAP 5.8
« OAuth/Message.pm ↑ Summary OAuth/SignatureMethod/RSA_SHA1.pm »
1 package Net::OAuth::Client;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 __PACKAGE__->mk_accessors(qw/id secret callback is_v1a user_agent site debug session allow_v1a_downgrade/);
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::OAuth;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::OAuth::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::OAuth::AccessToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Crypt::SysRandom qw( random_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.33';
15  
16 =head1 NAME
17  
18 Net::OAuth::Client - OAuth 1.0A Client
19  
20 =head1 SYNOPSIS
21  
22 use Net::OAuth::Client;
23  
24 # client_id is the Consumer Key, client_secret the Consumer Secret.
25 # Framework-agnostic: supply your own redirect and session handling.
26  
27 my $client = Net::OAuth::Client->new(
28 $client_id,
29 $client_secret,
30 site => 'https://provider.example/',
31 request_token_path => '/oauth/request_token',
32 authorize_path => '/oauth/authorize',
33 access_token_path => '/oauth/access_token',
34 callback => 'https://you.example/auth/callback',
35 session => \&session,
36 );
37  
38 # 1. Send the user to the provider to authorize.
39 $client->authorize_url;
40  
41 # 2. They return to your callback with oauth_token and oauth_verifier.
42 my $access_token = $client->get_access_token($token, $verifier);
43  
44 # 3. Use the access token to fetch a protected resource.
45 my $response = $access_token->get('/profile');
46 die $response->status_line unless $response->is_success;
47 print $response->decoded_content;
48  
49 =head1 DESCRIPTION
50  
51 Net::OAuth::Client represents an OAuth client or consumer.
52  
53 WARNING: Net::OAuth::Client is alpha code. The rest of Net::OAuth is quite
54 stable but this particular module is new, and is under-documented and under-tested.
55  
56  
57 =head1 METHODS
58  
59 =over
60  
61 =item new($client_id, $client_secret, %params)
62  
63 Create a new Client
64  
65 =over
66  
67 =item * $client_id
68  
69 AKA Consumer Key - you get this from the service provider when you register your application.
70  
71 =item * $client_secret
72  
73 AKA Consumer Secret - you get this from the service provider when you register your application.
74  
75 =item * $params{site}
76  
77 =item * $params{request_token_path}
78  
79 =item * $params{authorize_path}
80  
81 =item * $params{access_token_path}
82  
83 =item * $params{callback}
84  
85 =item * $params{session}
86  
87 =item * $params{allow_v1a_downgrade}
88  
89 Permit the fallback to OAuth 1.0 described in L</OAUTH 1.0A AND THE
90 CALLBACK CONFIRMATION>. Off by default.
91  
92 =back
93  
94 =back
95  
96 =head2 OAUTH 1.0A AND THE CALLBACK CONFIRMATION
97  
98 Passing a C<callback> to C<new> selects OAuth 1.0a. In 1.0a the service
99 provider echoes C<oauth_callback_confirmed> in its request token response,
100 and later hands the user back with an C<oauth_verifier> that the client
101 must present when it exchanges the request token for an access token.
102  
103 If the provider's request token response does not contain
104 C<oauth_callback_confirmed>, this client cannot use 1.0a. It used to drop
105 back to plain OAuth 1.0 by itself, and that fallback was invisible: the
106 access token request is then built from the 1.0 message class, which has
107 no C<verifier> parameter, so C<oauth_verifier> is quietly left off the
108 wire even when the caller passed one to L</get_access_token>. The verifier
109 is exactly what 1.0a added to stop an attacker starting a flow, getting a
110 victim to authorize the attacker's request token, and then completing the
111 exchange - so an application that asked for 1.0a and silently got 1.0 is
112 open to that, with nothing to tell it so.
113  
114 C<get_request_token> now croaks in that situation instead. An application
115 that has to talk to a provider which is not 1.0a can opt in with
116  
117 allow_v1a_downgrade => 1
118  
119 which restores the fallback and warns rather than dying.
120  
121 =cut
122  
123 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
124 4 my $class = shift;
125 4 my $client_id = shift;
126 4 my $client_secret = shift;
127 4 my %opts = @_;
128 4 ▶ $opts{user_agent} ||= LWP::UserAgent->new;
Condition33%1/3
$opts{"user_agent"} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
400
MC/DC0%0/2
$opts{"user_agent"} ||= "LWP::UserAgent"->new
$opts{"user_agent"} "LWP::UserAgent"->new
Truth table33%1/3
A ||= B
A= $opts{"user_agent"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
129 4 $opts{id} = $client_id;
130 4 $opts{secret} = $client_secret;
131 4 $opts{is_v1a} = defined $opts{callback};
132 4 my $self = bless \%opts, $class;
133 4 return $self;
134 }
135  
136 7 ▶sub request {
Subroutine request: called CC=1 SCAR=0.0
Pod undocumented
137 7 my $self = shift;
138 7 my $response = $self->user_agent->request(@_);
139 }
140  
141 7 ▶sub _parse_oauth_response {
Subroutine _parse_oauth_response: called CC=7 SCAR=27.5
142 7 my $self = shift;
143 7 my $do_what = shift;
144 7 my $http_res = shift;
145 7 my $msg = "Unable to $do_what: Request for " . $http_res->request->uri . " failed";
146 7 ▶ unless ($http_res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_res->is_success) 0 7 7
147 0 ▶ if ($self->debug) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
elsif ($http_res->content_type eq 'application/x-www-form-urlencoded' and $http_res->decoded_content =~ /\boauth_problem=(\w+)/) { } 0 0 0
148 0 $msg .= "," . $http_res->as_string . " ";
149 }
150 ▶ elsif (
Condition0%0/3
$http_res->content_type eq "application/x-www-form-urlencoded" and $http_res->decoded_content =~ /\boauth_problem=(\w+)/
!ll&&!rl&&r
000
MC/DC0%0/2
$http_res->content_type eq "application/x-www-form-urlencoded" and $http_res->decoded_content =~ /\boauth_problem=(\w+)/
$http_res->content_type eq "application/x-www-form-urlencoded" $http_res->decoded_content =~ /\boauth_problem=(\w+)/
Truth table0%0/3
A and B
A= $http_res->content_type eq "application/x-www-form-urlencoded"B= $http_res->decoded_content =~ /\boauth_problem=(\w+)/
ABresult
0X0
100
111
151 $http_res->content_type eq 'application/x-www-form-urlencoded'
152 and $http_res->decoded_content =~ /\boauth_problem=(\w+)/
153 ) {
154 0 $msg .= ", reason: " . $1;
155 }
156 else {
157 0 $msg .= ": " . $http_res->status_line . " (pass debug=>1 to Net::OAuth::Client->new to dump the entire response)";
158 }
159 0 croak $msg;
160 }
161 7 my $oauth_res = _parse_url_encoding($http_res->decoded_content);
162 7 foreach my $k (qw/token token_secret/) {
163 14 ▶ croak "Unable to $do_what: server response is missing '$k'" unless defined $oauth_res->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oauth_res->{$k} 0 14 14
164 }
165 7 return $oauth_res;
166  
167 }
168  
169 7 ▶sub _parse_url_encoding {
Subroutine _parse_url_encoding: called CC=4 SCAR=14.1
170 7 my $str = shift;
171 7 my @pairs = split '&', $str;
172 7 my %params;
173 7 foreach my $pair (@pairs) {
174 15 my ($k,$v) = split /=/, $pair;
175 15 ▶ if (defined $k and defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k and defined $v) 15 0 15
Condition33%1/3
defined $k and defined $v
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $k and defined $v
defined $k defined $v
Truth table33%1/3
A and B
A= defined $kB= defined $v
ABresult
0X0
100
111
176 15 $v =~ s/(^"|"$)//g;
177 15 ($k,$v) = map Net::OAuth::Message::decode($_), $k, $v;
178 15 $k =~ s/^oauth_//;
179 15 $params{$k} = $v;
180 }
181 }
182 7 return \%params;
183 }
184  
185 4 ▶sub get_request_token {
Subroutine get_request_token: called CC=4 SCAR=13.9
Pod undocumented
186 4 my $self = shift;
187 4 my %params = @_;
188 4 my $oauth_req = $self->_make_request(
189 "request token",
190 request_method => $self->request_token_method,
191 request_url => $self->_make_url("request_token"),
192 %params
193 );
194 4 $oauth_req->sign;
195 4 my $http_res = $self->request(HTTP::Request->new(
196 $self->request_token_method => $oauth_req->to_url
197 ));
198 4 my $oauth_res = $self->_parse_oauth_response('get a request token', $http_res);
199 4 ▶ if ($self->is_v1a and !defined $oauth_res->{callback_confirmed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_v1a and not defined $oauth_res->{"callback_confirmed"}) 2 2 4
Condition100%3/3
$self->is_v1a and not defined $oauth_res->{"callback_confirmed"}
!ll&&!rl&&r
112
MC/DC100%2/2
$self->is_v1a and not defined $oauth_res->{"callback_confirmed"}
$self->is_v1a not defined $oauth_res->{"callback_confirmed"}
Truth table100%3/3
A and not(B)
A= $self->is_v1aB= not defined $oauth_res->{"callback_confirmed"}
ABresult
0X0
100
111
200 # The application asked for OAuth 1.0a by configuring a callback and the
201 # provider did not confirm it. Dropping to 1.0 here also drops
202 # oauth_verifier from the access token request further down, which is
203 # the parameter 1.0a added to prevent session fixation, so this is not a
204 # decision to take on the application's behalf without telling it.
205 2 ▶ croak "Unable to get a request token: the service provider did not "
Branch: 1 branch
BranchTrueFalseTotal
unless $self->allow_v1a_downgrade 1 1 2
206 . "confirm the OAuth 1.0a callback (no oauth_callback_confirmed in "
207 . "its response). Pass allow_v1a_downgrade => 1 to "
208 . "Net::OAuth::Client->new to fall back to OAuth 1.0 instead"
209 unless $self->allow_v1a_downgrade;
210 1 carp "Net::OAuth::Client warning: service provider did not confirm the "
211 . "OAuth 1.0a callback; falling back to OAuth 1.0, so oauth_verifier "
212 . "will not be sent";
213 1 $self->is_v1a(0);
214 }
215 3 return $oauth_res;
216 }
217  
218 0 ▶sub authorize_url {
Subroutine authorize_url: not called CC=4 SCAR=30.0
Pod undocumented
219 0 my $self = shift;
220 0 my %params = @_;
221 # allow user to get request token their own way
222 0 ▶ unless (defined $params{token} and defined $params{token_secret}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"token"} and defined $params{"token_secret"}) 0 0 0
Condition0%0/3
defined $params{"token"} and defined $params{"token_secret"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"token"} and defined $params{"token_secret"}
defined $params{"token"} defined $params{"token_secret"}
Truth table0%0/3
A and B
A= defined $params{"token"}B= defined $params{"token_secret"}
ABresult
0X0
100
111
223 0 my $request_token = $self->get_request_token;
224 0 $params{token} = $request_token->{token};
225 0 $params{token_secret} = $request_token->{token_secret};
226 }
227 0 ▶ if (defined $self->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->session) 0 0 0
228 0 $self->session->($params{token} => $params{token_secret});
229 }
230 0 my $oauth_req = $self->_make_request(
231 'user auth',
232 %params
233 );
234 0 return $oauth_req->to_url($self->_make_url('authorize'));
235 }
236  
237 3 ▶sub get_access_token {
Subroutine get_access_token: called CC=4 SCAR=13.9
Pod undocumented
238 3 my $self = shift;
239 3 my $token = shift;
240 3 my $verifier = shift;
241 3 my %params = @_;
242  
243 3 ▶ if (defined $self->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->session) 3 0 3
244 3 $params{token_secret} = $self->session->($token);
245 }
246  
247 # In OAuth 1.0 mode the access token request class has no verifier message
248 # parameter, so gather_message_parameters() would drop this on the floor
249 # without a word. Say so rather than sending a request that is missing
250 # the credential the caller believed it was presenting.
251 3 ▶ if (defined $verifier and !$self->is_v1a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verifier and not $self->is_v1a) 2 1 3
Condition66%2/3
defined $verifier and not $self->is_v1a
!ll&&!rl&&r
012
MC/DC50%1/2
defined $verifier and not $self->is_v1a
defined $verifier not $self->is_v1a
Truth table66%2/3
A and not(B)
A= defined $verifierB= not $self->is_v1a
ABresult
0X0
100
111
252 2 carp "Net::OAuth::Client warning: an oauth_verifier was supplied but "
253 . "this client is in OAuth 1.0 mode, so it will not be sent";
254 }
255  
256 3 my $oauth_req = $self->_make_request(
257 'access token',
258 request_method => $self->access_token_method,
259 request_url => $self->_make_url('access_token'),
260 token => $token,
261 verifier => $verifier,
262 %params
263 );
264 3 $oauth_req->sign;
265  
266 3 my $http_res = $self->request(HTTP::Request->new(
267 $self->access_token_method => $oauth_req->to_url
268 ));
269  
270 3 my $oauth_res = $self->_parse_oauth_response('get an access token', $http_res);
271  
272 3 return Net::OAuth::AccessToken->new(%$oauth_res, client => $self);
273 }
274  
275 0 ▶sub access_token_url {
Subroutine access_token_url: not called CC=1 SCAR=6.9
Pod undocumented
276 0 return shift->_make_url('access_token', @_);
277 }
278  
279 0 ▶sub request_token_url {
Subroutine request_token_url: not called CC=1 SCAR=6.9
Pod undocumented
280 0 return shift->_make_url('request_token', @_);
281 }
282  
283 6 ▶sub access_token_method {
Subroutine access_token_method: called CC=2 SCAR=7.6
Pod undocumented
284 6 ▶ return shift->{access_token_method} || 'GET';
Condition50%1/2
+(shift())->{'access_token_method'} || 'GET'
l!l
06
MC/DC0%0/1
+(shift())->{'access_token_method'} || 'GET'
+(shift())->{'access_token_method'}
Truth table50%1/2
A
A= +(shift())->{'access_token_method'}
Aresult
11
00
285 }
286  
287 8 ▶sub request_token_method {
Subroutine request_token_method: called CC=2 SCAR=7.6
Pod undocumented
288 8 ▶ return shift->{request_token_method} || 'GET';
Condition50%1/2
+(shift())->{'request_token_method'} || 'GET'
l!l
08
MC/DC0%0/1
+(shift())->{'request_token_method'} || 'GET'
+(shift())->{'request_token_method'}
Truth table50%1/2
A
A= +(shift())->{'request_token_method'}
Aresult
11
00
289 }
290  
291 7 ▶sub _make_request {
Subroutine _make_request: called CC=3 SCAR=11.0
292 7 my $self = shift;
293 7 my $type = shift;
294 7 my %params = @_;
295 7 my %defaults = (
296 timestamp => time,
297 consumer_key => $self->id,
298 consumer_secret => $self->secret,
299 callback => $self->callback,
300 signature_method => 'HMAC-SHA1',
301 request_method => 'GET',
302 );
303 7 ▶ $defaults{nonce} = unpack("H*", random_bytes(16)) unless exists $params{nonce};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"nonce"} 7 0 7
304 7 ▶ $defaults{protocol_version} = Net::OAuth::PROTOCOL_VERSION_1_0A if $self->is_v1a;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_v1a 4 3 7
305 7 my $req = Net::OAuth->request($type)->new(
306 %defaults,
307 %params
308 );
309 7 return $req;
310 }
311  
312 7 ▶sub _make_url {
Subroutine _make_url: called CC=3 SCAR=12.0
313 7 my $self = shift;
314 7 my $thing = shift;
315 7 ▶ my $path = $self->{"${thing}_url"} || $self->{"${thing}_path"} || "/oauth/${thing}";
Condition40%2/5
$$self{"${thing}_url"} || $$self{"${thing}_path"} || "/oauth/$thing"
l!l
70
$$self{"${thing}_url"} or $$self{"${thing}_path"}
l!l&&r!l&&!r
070
MC/DC0%0/2
$$self{"${thing}_url"} || $$self{"${thing}_path"} || "/oauth/$thing"
$$self{"${thing}_url"} $$self{"${thing}_path"}
Truth table33%1/3
A or B
A= $$self{"${thing}_url"}B= $$self{"${thing}_path"}
ABresult
1X1
011
000
316 7 return $self->site_url($path, @_);
317 }
318  
319 7 ▶sub site_url {
Subroutine site_url: called CC=3 SCAR=11.7
Pod undocumented
320 7 my $self = shift;
321 7 my $path = shift;
322 7 my %params = @_;
323 7 my $url;
324 7 ▶ if (defined $self->{site}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'site'}) { } 7 0 7
325 7 $url = URI->new_abs($path, $self->{site});
326 }
327 else {
328 0 $url = URI->new($path);
329 }
330 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 7 7
331 0 $url->query_form($url->query_form , %params);
332 }
333 7 return $url;
334 }
335  
336 =head1 AUTHOR
337  
338 Originally by Keith Grennan <foss@nearlyfree.org>
339  
340 Currently maintained by Robert Rothenberg <perl@rhizomnic.com>
341  
342 =head1 LICENSE AND COPYRIGHT
343  
344 Copyright 2007-2012, 2024-2026 Keith Grennan
345  
346 This program is free software; you can redistribute it and/or modify it
347 under the terms of either: the GNU General Public License as published
348 by the Free Software Foundation; or the Artistic License.
349  
350 See http://dev.perl.org/licenses/ for more information.
351  
352 =cut
353  
354 1 1;
« OAuth/Message.pm ↑ Summary OAuth/SignatureMethod/RSA_SHA1.pm »