Client.pm

filter: Statementstmt 59.8% 106 / 177 Branchbran 21.7% 10 / 46 Conditioncond 17.9% 7 / 39 MC/DCmcdc 9.0% 2 / 22 Subroutinesub 73.0% 19 / 26 Podpod 100% 6 / 6 totaltotal 47.4% 150 / 316 CC 42 SCAR 36.2
CC
42
Cov
47%
_build_access_token_object
96.2
_access_token_params
56.0
request
50.0
CRAP 37.4
↑ Summary AccessToken.pm »
1 package Net::OAuth2::Moosey::Client;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Net::OAuth2::Moosey::Client - OAuth 2.0 client for perl
7  
8 =head1 VERSION
9  
10 0.02
11  
12 =cut
13  
14 2 our $VERSION = '0.02';
15  
16 =head1 DESCRIPTION
17  
18 This is a perl implementation of the OAuth 2.0 protocol.
19  
20 It is based on (forked from), and very similar in functionality to Keith Grennan's L<Net::OAuth2> module.
21  
22 The major differences to the original L<Net::OAuth2> module are:
23  
24 =over 2
25  
26 =item * Converted to use Moose
27  
28 =item * Named parameters for all methods
29  
30 =item * More documentation
31  
32 =item * No demo code for a web application
33  
34 =back
35  
36 =head1 SYNOPSIS
37  
38 use Net::OAuth2::Moosey::Client;
39 my %client_params = (
40 site_url_base => 'https://accounts.google.com/o/oauth2/auth',
41 access_token_url_base => 'https://accounts.google.com/o/oauth2/token',
42 authorize_url_base => 'https://accounts.google.com/o/oauth2/auth',
43 scope => 'https://www.google.com/fusiontables/api/query',
44 client_id => '123456789.apps.googleusercontent.com',
45 client_secret => 'atecSNTE23sthbjcasrCuw4i',
46 );
47  
48 my $client = Net::OAuth2::Moosey::Client->new( %client_params );
49  
50 # Get a fresh access token
51 $client->get_fresh_access_token();
52  
53 # Send a request
54 my @post_args = ( 'https://www.google.com/fusiontables/api/query',
55 HTTP::Headers->new( Content_Type => 'application/x-www-form-urlencoded' ),
56 sprintf( 'sql=%s', url_encode( 'SHOW TABLES' ) ) );
57 my $response = $self->auth_client->post( @post_args );
58  
59  
60 =cut
61  
62 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Net::OAuth2::Moosey::AccessToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use MooseX::Types::URI qw(Uri FileUri DataUri);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use MooseX::Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 =head1 METHODS
74  
75 =head2 new
76  
77 =head3 ATTRIBUTES
78  
79 =over 2
80  
81 =item * client_id <Str>
82  
83 ID for your application as given to you by your service provider.
84  
85 =item * client_secret <Str>
86  
87 Secret for your application as given to you by your service provider.
88  
89 =item * scope <Uri>
90  
91 Scope for which your are applying for access to.
92  
93 e.g. https://www.google.com/fusiontables/api/query
94  
95 =item * site_url_base <Uri>
96  
97 Base url for OAuth.
98  
99 e.g. https://accounts.google.com/o/oauth2/auth
100  
101 =item * access_token_url_base <Uri>
102  
103 Access token url.
104  
105 e.g. https://accounts.google.com/o/oauth2/token
106  
107 =item * authorize_url_base <Uri>
108  
109 Authorize url.
110  
111 e.g. https://accounts.google.com/o/oauth2/auth
112  
113 =item * access_token_path <Str>
114  
115 =item * authorize_path <Str>
116  
117 The ..._path parameters are an alternative to their ..._url_base counterparts.
118 If used, the authorize_url will be built from the site_url_base and the _path.
119  
120 =item * refresh_token <Str>
121  
122 If known, the refresh token can be defined here
123 If not, it will be determined during a request.
124  
125 =item * access_token <Str>
126  
127 If known the access token can be defined here.
128 If not, it will be determined during a request.
129  
130 =item * access_code <Str>
131  
132 If known, the access code can be defined here.
133 It is only necessary if you have not yet got an access/refresh token.
134 If you are running in interactive mode (and access/refresh tokens are not defined),
135 you will be given a URL to open in a browser and copy the resulting code to the command line.
136  
137 =item * token_store <Str>
138  
139 Path to a file to store your tokens.
140 This can be the same file for multiple services - it is a simple YAML file with one entry per
141 client_id which stores your refresh and access tokens.
142  
143 =item * redirect_uri <Str>
144  
145 Only needs to be defined if using the 'webserver' profile. The page to which the service provider
146 should redirect to after authorization.
147 For instances using the 'application' profile, the default 'urn:ietf:wg:oauth:2.0:oob' is used.
148  
149 =item * access_token_method <Str>
150  
151 GET or POST?
152  
153 Default: POST
154  
155 =item * bearer_token_scheme <Str>
156  
157 Should be one of: auth-header, uri-query, form-body
158
159 Default: auth-header
160  
161 =item * profile <Str>
162  
163 Are you using this module as a webserver (users browser is forwarded to the authorization urls, and they
164 in turn redirect back to your redirect_uri), or as an application (interactively, no browser interaction
165 for authorization possible)?
166  
167 Should be one of: application, webserver
168  
169 Default: application
170  
171 =item * interactive <Bool>
172  
173 Are you running your program interactively (i.e. if necessary, do you want to have a prompt for, and paste
174 the authorization code from your browser on the command line?).
175  
176 Options: 0, 1
177  
178 Default: 1
179  
180 =item * keep_alive <Int>
181  
182 Should the LWP::UserAgent instance used have a connection cache, and how many connections should it cache?
183 Turning off keep_alive can make interaction with your service provider very slow, especially if it is
184 over an encrypted connection (which it should be).
185  
186 Default: 1 (try 2 if your service provider requires frequent authorization token refreshing)
187  
188 =item * user_agent <LWP::UserAgent>
189  
190 It is not necessary to pass a UserAgent, but maybe you have a custom crafted instance which you want to reuse...
191  
192 =item * access_token_object <Net::OAuth2::Moosey::AccessToken>
193  
194 The access token object which manages always having a fresh token ready for you.
195  
196 =back
197  
198 =cut
199  
200 2 has 'client_id' => ( is => 'ro', isa => 'Str', );
201 2 has 'client_secret' => ( is => 'ro', isa => 'Str', );
202 2 has 'scope' => ( is => 'ro', isa => Uri, coerce => 1, );
203 2 has 'site_url_base' => ( is => 'ro', isa => Uri, coerce => 1, );
204 2 has 'access_token_url_base' => ( is => 'ro', isa => Uri, coerce => 1, );
205 2 has 'authorize_url_base' => ( is => 'ro', isa => Uri, coerce => 1, );
206  
207 2 has 'access_token_path' => ( is => 'ro', isa => 'Str', );
208 2 has 'authorize_path' => ( is => 'ro', isa => 'Str', );
209 2 has 'refresh_token' => ( is => 'ro', isa => 'Str', );
210 2 has 'access_token' => ( is => 'ro', isa => 'Str', );
211 2 has 'access_code' => ( is => 'rw', isa => 'Str', );
212 2 has 'token_store' => ( is => 'ro', isa => 'Str', );
213  
214 # TODO: RCL 2011-11-03 Test if is URI if profile eq 'webserver'
215 2 has 'redirect_uri' => ( is => 'ro', isa => 'Str', required => 1,
216 default => 'urn:ietf:wg:oauth:2.0:oob' );
217  
218 2 has 'access_token_method' => ( is => 'ro', isa => 'Str', required => 1, default => 'POST' );
219 2 has 'bearer_token_scheme' => ( is => 'ro', isa => 'Str', required => 1, default => 'auth-header' );
220 2 has 'profile' => ( is => 'ro', isa => 'Str', required => 1, default => 'application' );
221 2 has 'interactive' => ( is => 'ro', isa => 'Bool', required => 1, default => 1 );
222 2 has 'keep_alive' => ( is => 'ro', isa => 'Int', required => 1, default => 1 );
223  
224 2 has 'user_agent' => (
225 is => 'ro',
226 isa => 'LWP::UserAgent',
227 writer => '_set_user_agent',
228 predicate => '_has_user_agent',
229 );
230  
231 2 has 'access_token_object' => ( is => 'rw',
232 isa => 'Net::OAuth2::Moosey::AccessToken',
233 builder => '_build_access_token_object',
234 lazy => 1,
235 );
236  
237  
238 # Create a LWP::UserAgent if necessary
239 12 ▶around 'user_agent' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
240 12 my $orig = shift;
241 12 my $self = shift;
242 12 ▶ unless( $self->_has_user_agent ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_user_agent) 3 9 12
243 3 $self->_set_user_agent( LWP::UserAgent->new( 'keep_alive' => $self->keep_alive ) );
244 }
245 12 return $self->$orig;
246 2 };
247  
248  
249 # Because a valid combination of parameters is not possible to define with 'has',
250 # doing a more complex param check before new
251 3 ▶before 'new' => sub{
Subroutine __ANON__: called CC=4 SCAR=14.1
252 3 my $class = shift;
253 3 my %params = @_;
254
255 3 my $found_valid = 0;
256 3 my @valid = (
257 [ qw/client_id client_secret site_url_base/ ],
258 [ qw/access_token no_refresh_token_ok/ ],
259 [ qw/refresh_token site_url_base/ ],
260 );
261 FOUND_VALID:
262 3 foreach( @valid ){
263 3 my @test = @{ $_ };
264 3 ▶ if( scalar( grep{ $params{$_} } @test ) == scalar( @test ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$params{$_};} @test) == scalar @test) 3 0 3
265 3 $found_valid = 1;
266 3 last FOUND_VALID;
267 }
268 }
269 3 ▶ if( not $found_valid ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_valid) 0 3 3
270 0 die( "Not initialised with a valid combination of parameters...\n" . Dump( \%params ) );
271 }
272 2 };
273  
274 3 ▶sub _build_access_token_object {
Subroutine _build_access_token_object: called CC=13 SCAR=45.7
275 3 my $self = shift;
276
277 # Try to load an access token from the store first
278 3 my $access_token = undef;
279 3 my %token_params = ( client => $self );
280 3 foreach( qw/client_id client_secret access_token access_code
281 access_token_url refresh_token token_store user_agent/ ){
282 24 ▶ $token_params{$_} = $self->$_ if $self->$_;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_ 21 3 24
283 }
284 3 $access_token = Net::OAuth2::Moosey::AccessToken->new( %token_params );
285 3 $access_token->sync_with_store;
286 3 ▶ if( not $access_token->refresh_token ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token->refresh_token) 0 3 3
287 0 my $profile = $self->profile;
288  
289 # Interactive applications need to supply a code
290 0 ▶ if( not $self->access_code ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->access_code) 0 0 0
291 0 ▶ if( $self->profile ne 'application' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->profile ne "application") 0 0 0
292 0 croak( "access_code required but not available" );
293 }
294 0 printf "Please authorize your application with this URL\n%s\n",
295 $self->authorize_url();
296 0 ▶ if( not $self->interactive ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->interactive) 0 0 0
297 #TODO: RCL 2011-11-02 Better handling for non-interactive. Maybe return the URL?
298 0 exit;
299 }
300 0 print "Code: ";
301 0 my $code = <STDIN>;
302 0 chomp( $code );
303 0 $self->access_code( $code );
304 }
305  
306 0 my $request;
307 0 ▶ if( $self->access_token_method eq 'POST' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token_method eq 'POST') { } 0 0 0
308 0 $request = POST( $self->access_token_url(), { $self->_access_token_params() } );
309 } else {
310 0 $request = HTTP::Request->new(
311 $self->access_token_method => $self->access_token_url( $self->_access_token_params() ),
312 );
313 };
314
315 0 my $response = $self->user_agent->request($request);
316 0 ▶ if( not $response->is_success ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
317 0 croak( "Fetch of access token failed: " . $response->status_line . ": " . $response->decoded_content );
318 }
319
320 0 my $res_params = _parse_json($response->decoded_content);
321 0 ▶ $res_params = _parse_query_string($response->decoded_content) unless defined $res_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res_params 0 0 0
322 0 ▶ if( not defined $res_params ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res_params) 0 0 0
323 0 croak( "Unable to parse access token response '".substr($response->decoded_content, 0, 64)."'" );
324 }
325
326 #TODO: RCL 2011-11-02 Check that required values returned.
327 # Write the returned values to the access token object
328 0 foreach my $key( keys( %{ $res_params } ) ){
329 # TODO: RCL 2011-11-02 Isn't there a has_accessor way of doing this?
330 0 ▶ if( $access_token->meta->has_method( $key ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($access_token->meta->has_method($key)) { } 0 0 0
331 0 $access_token->$key( $res_params->{$key} );
332 }else{
333 0 warn( "Unknown key found in response parameters: $key\n" );
334 }
335 }
336 0 $access_token->sync_with_store;
337 }
338 3 return $access_token;
339 }
340  
341  
342 =head2 refresh_access_token
343  
344 Make the current access token expire, and request a fresh access token
345  
346 =cut
347 0 ▶sub refresh_access_token {
Subroutine refresh_access_token: not called CC=1 SCAR=6.9
Pod documented
348 0 my $self = shift;
349  
350 # Make it expire now
351 0 $self->access_token_object->expires_at( time() );
352  
353 # Request a fresh access token
354 0 $self->access_token_object->valid_access_token();
355 }
356  
357 =head2 request
358  
359 Submit a request. This is a wrapper arround a basic LWP::UserAgent->request, but adds the necessary
360 headers with the access tokens necessary for an OAuth2 request.
361  
362 =cut
363 6 ▶sub request {
Subroutine request: called CC=11 SCAR=39.1
Pod documented
364 6 my $self = shift;
365 6 my ($method, $uri, $header, $content) = @_;
366 6 my $request = HTTP::Request->new(
367 $method => $self->_site_url($uri), $header, $content
368 );
369 # We assume a bearer token type, but could extend to other types in the future
370 6 my @bearer_token_scheme = split ':', $self->bearer_token_scheme;
371 6 ▶ if (lc($bearer_token_scheme[0]) eq 'auth-header') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $bearer_token_scheme[0] eq 'auth-header') { } 6 0 6
elsif (lc $bearer_token_scheme[0] eq 'uri-query') { } 0 0 0
elsif (lc $bearer_token_scheme[0] eq 'form-body') { } 0 0 0
372 # Specs suggest using Bearer or OAuth2 for this value, but OAuth appears to be the de facto accepted value.
373 # Going to use OAuth until there is wide acceptance of something else.
374 6 ▶ my $auth_scheme = $self->access_token_object->token_type || $bearer_token_scheme[1] || 'OAuth';
Condition40%2/5
$self->access_token_object->token_type || $bearer_token_scheme[1] || 'OAuth'
l!l
60
$self->access_token_object->token_type or $bearer_token_scheme[1]
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->access_token_object->token_type || $bearer_token_scheme[1] || 'OAuth'
$self->access_token_object->token_type $bearer_token_scheme[1]
Truth table33%1/3
A or B
A= $self->access_token_object->token_typeB= $bearer_token_scheme[1]
ABresult
1X1
011
000
375 6 $request->headers->push_header(Authorization => $auth_scheme . " " . $self->access_token_object->valid_access_token);
376 }
377 elsif (lc($bearer_token_scheme[0]) eq 'uri-query') {
378 0 ▶ my $query_param = $bearer_token_scheme[1] || 'oauth_token';
Condition0%0/2
$bearer_token_scheme[1] || 'oauth_token'
l!l
00
MC/DC0%0/1
$bearer_token_scheme[1] || 'oauth_token'
$bearer_token_scheme[1]
Truth table0%0/2
A
A= $bearer_token_scheme[1]
Aresult
11
00
379 0 $request->uri->query_form($request->uri->query_form, $query_param => $self->access_token_object->valid_access_token);
380 }
381 elsif (lc($bearer_token_scheme[0]) eq 'form-body') {
382 0 ▶ croak "Embedding access token in request body is only valid for 'application/x-www-form-urlencoded' content type"
Branch: 1 branch
BranchTrueFalseTotal
unless $request->headers->content_type eq "application/x-www-form-urlencoded" 0 0 0
383 unless $request->headers->content_type eq 'application/x-www-form-urlencoded';
384 0 ▶ my $query_param = $bearer_token_scheme[1] || 'oauth_token';
Condition0%0/2
$bearer_token_scheme[1] || 'oauth_token'
l!l
00
MC/DC0%0/1
$bearer_token_scheme[1] || 'oauth_token'
$bearer_token_scheme[1]
Truth table0%0/2
A
A= $bearer_token_scheme[1]
Aresult
11
00
385 0 ▶ $request->add_content(
Branch: 1 branch
BranchTrueFalseTotal
defined $request->content && length $request->content ? : 0 0 0
Condition0%0/3
defined $request->content and length $request->content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $request->content and length $request->content
defined $request->content length $request->content
Truth table0%0/3
A and B
A= defined $request->contentB= length $request->content
ABresult
0X0
100
111
386 ((defined $request->content and length $request->content) ? "&" : "") .
387 uri_escape($query_param) . '=' . uri_escape($self->valid_access_token)
388 );
389 }
390 6 return $self->user_agent->request( $request );
391 }
392  
393 =head2 post
394  
395 A wrapper for the request method already defining the request method as POST
396  
397 =cut
398 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
399 0 my $self = shift;
400 0 $self->request( 'POST', @_ );
401 }
402  
403 =head2 get
404  
405 A wrapper for the request method already defining the request method as GET
406  
407 =cut
408 6 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
409 6 my $self = shift;
410 6 $self->request( 'GET', @_ );
411 }
412  
413 =head2 authorize_url
414  
415 Returns the authorization url
416  
417 =cut
418 0 ▶sub authorize_url {
Subroutine authorize_url: not called CC=1 SCAR=6.9
Pod documented
419 0 my $self = shift;
420 0 return $self->_make_url("authorize", $self->_authorize_params( @_ ) );
421 }
422  
423 =head2 access_token_url
424  
425 Returns the access token url
426  
427 =cut
428 6 ▶sub access_token_url {
Subroutine access_token_url: called CC=1 SCAR=0.0
Pod documented
429 6 return shift->_make_url("access_token", @_ );
430 }
431  
432  
433 # Internal method to prepare the necessary authorize parameters
434 0 ▶sub _authorize_params {
Subroutine _authorize_params: not called CC=6 SCAR=37.4
435 0 my $self = shift;
436 0 my %options = @_;
437 0 ▶ $options{scope} ||= $self->scope;
Condition0%0/3
$options{"scope"} ||= $self->scope
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"scope"} ||= $self->scope
$options{"scope"} $self->scope
Truth table0%0/3
A ||= B
A= $options{"scope"}B= $self->scope
ABresult
1X1
011
000
438 0 ▶ $options{client_id} ||= $self->client_id;
Condition0%0/3
$options{"client_id"} ||= $self->client_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"client_id"} ||= $self->client_id
$options{"client_id"} $self->client_id
Truth table0%0/3
A ||= B
A= $options{"client_id"}B= $self->client_id
ABresult
1X1
011
000
439 0 ▶ $options{response_type} ||= 'code';
Condition0%0/2
$options{"response_type"} ||= "code"
l!l
00
MC/DC0%0/1
$options{"response_type"} ||= "code"
$options{"response_type"}
Truth table0%0/2
A
A= $options{"response_type"}
Aresult
11
00
440 0 ▶ $options{redirect_uri} ||= $self->redirect_uri;
Condition0%0/3
$options{"redirect_uri"} ||= $self->redirect_uri
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"redirect_uri"} ||= $self->redirect_uri
$options{"redirect_uri"} $self->redirect_uri
Truth table0%0/3
A ||= B
A= $options{"redirect_uri"}B= $self->redirect_uri
ABresult
1X1
011
000
441
442 0 ▶ if( $self->profile eq 'webserver' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->profile eq "webserver") 0 0 0
443 # legacy for pre v2.09 (37Signals)
444 0 $options{type} = 'web_server';
445 }
446 0 return %options;
447 }
448  
449 # Internal method to prepare the necessary access token parameters
450 0 ▶sub _access_token_params {
Subroutine _access_token_params: not called CC=7 SCAR=40.3
451 0 my $self = shift;
452 0 my %options = @_;
453 0 ▶ $options{client_id} ||= $self->client_id;
Condition0%0/3
$options{"client_id"} ||= $self->client_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"client_id"} ||= $self->client_id
$options{"client_id"} $self->client_id
Truth table0%0/3
A ||= B
A= $options{"client_id"}B= $self->client_id
ABresult
1X1
011
000
454 0 ▶ $options{client_secret} ||= $self->client_secret;
Condition0%0/3
$options{"client_secret"} ||= $self->client_secret
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"client_secret"} ||= $self->client_secret
$options{"client_secret"} $self->client_secret
Truth table0%0/3
A ||= B
A= $options{"client_secret"}B= $self->client_secret
ABresult
1X1
011
000
455 0 ▶ $options{grant_type} ||= 'authorization_code';
Condition0%0/2
$options{"grant_type"} ||= "authorization_code"
l!l
00
MC/DC0%0/1
$options{"grant_type"} ||= "authorization_code"
$options{"grant_type"}
Truth table0%0/2
A
A= $options{"grant_type"}
Aresult
11
00
456 0 ▶ $options{code} = $self->access_code if $self->access_code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->access_code 0 0 0
457 0 ▶ $options{redirect_uri} ||= $self->redirect_uri;
Condition0%0/3
$options{"redirect_uri"} ||= $self->redirect_uri
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"redirect_uri"} ||= $self->redirect_uri
$options{"redirect_uri"} $self->redirect_uri
Truth table0%0/3
A ||= B
A= $options{"redirect_uri"}B= $self->redirect_uri
ABresult
1X1
011
000
458  
459 0 ▶ if( $self->profile eq 'webserver' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->profile eq "webserver") 0 0 0
460 # legacy for pre v2.09 (37Signals)
461 0 $options{type} = 'web_server';
462 }
463
464 0 return %options;
465 }
466  
467 # The URL can be put together with various information... do what you can with what you've got!
468 6 ▶sub _make_url {
Subroutine _make_url: called CC=3 SCAR=11.0
469 6 my $self = shift;
470 6 my $thing = shift;
471 6 ▶ my $path = $self->{"${thing}_url_base"} || $self->{"${thing}_path"} || "/oauth/${thing}";
Condition100%5/5
$$self{"${thing}_url_base"} || $$self{"${thing}_path"} || "/oauth/$thing"
l!l
42
$$self{"${thing}_url_base"} or $$self{"${thing}_path"}
l!l&&r!l&&!r
222
MC/DC100%2/2
$$self{"${thing}_url_base"} || $$self{"${thing}_path"} || "/oauth/$thing"
$$self{"${thing}_url_base"} $$self{"${thing}_path"}
Truth table100%3/3
A or B
A= $$self{"${thing}_url_base"}B= $$self{"${thing}_path"}
ABresult
1X1
011
000
472 6 return $self->_site_url($path, @_);
473 }
474  
475 # Internal method to return the site url built from the site_url_base
476 12 ▶sub _site_url {
Subroutine _site_url: called CC=3 SCAR=11.7
477 12 my $self = shift;
478 12 my $path = shift;
479 12 my %params = @_;
480 12 my $url;
481 12 ▶ if( $self->site_url_base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->site_url_base) { } 12 0 12
482 12 $url = URI->new_abs($path, $self->site_url_base );
483 }
484 else {
485 0 $url = URI->new($path);
486 }
487 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 12 12
488 0 $url->query_form($url->query_form , %params);
489 }
490 12 return $url;
491 }
492  
493  
494 # Parse the query string
495 0 ▶sub _parse_query_string {
Subroutine _parse_query_string: not called CC=1 SCAR=6.9
496 0 my $str = shift;
497 0 my $uri = URI->new;
498 0 $uri->query($str);
499 0 return {$uri->query_form};
500 }
501  
502 # Parse json non-fataly
503 0 ▶sub _parse_json {
Subroutine _parse_json: not called CC=1 SCAR=6.9
504 0 my $str = shift;
505 0 my $obj = eval{local $SIG{__DIE__}; decode_json($str)};
506 0 return $obj;
507 }
508  
509 2 1;
510 =head1 LICENSE AND COPYRIGHT
511  
512 Copyright 2011 Robin Clarke
513  
514 This program is free software; you can redistribute it and/or modify it
515 under the terms of either: the GNU General Public License as published
516 by the Free Software Foundation; or the Artistic License.
517  
518 See http://dev.perl.org/licenses/ for more information.
519  
520 =head1 CONTRIBUTORS
521  
522 Thanks to Keith Grennan for Net::OAuth2 on which this is based
523  
524 =cut
525  
↑ Summary AccessToken.pm »