API/OAuth.pm

filter: Statementstmt 21.5% 20 / 93 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 54.5% 6 / 11 Podpod 100% 4 / 4 totaltotal 19.7% 30 / 152 CC 22 SCAR 34.0
CC
22
Cov
15%
request_request_token
56.0
request_access_token
42.0
restricted_request
30.0
CRAP 30.0
« API/Role/CRUD.pm ↑ Summary API.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::FreshBooks::API::OAuth;
5 8 $Net::FreshBooks::API::OAuth::VERSION = '0.24';
6 8 ▶use base qw(Net::OAuth::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
13  
14 0 my $class = shift;
15 0 my %tokens = @_;
16  
17 0 foreach my $key ( 'consumer_secret', 'consumer_key', 'account_name' ) {
18 0 ▶ if ( !exists $tokens{$key} || !$tokens{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $tokens{$key} and $tokens{$key}) 0 0 0
Condition0%0/3
exists $tokens{$key} and $tokens{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $tokens{$key} and $tokens{$key}
exists $tokens{$key} $tokens{$key}
Truth table0%0/3
A and B
A= exists $tokens{$key}B= $tokens{$key}
ABresult
0X0
100
111
19 0 croak( "$key required as an argument to new()" );
20 }
21 }
22  
23 0 my $account_name = delete $tokens{account_name};
24  
25 0 my $url = 'https://' . $account_name . '.freshbooks.com/oauth';
26  
27 0 my %create = (
28 tokens => \%tokens,
29 protocol_version => '1.0a',
30 urls => {
31 authorization_url => $url . '/oauth_authorize.php',
32 request_token_url => $url . '/oauth_request.php',
33 access_token_url => $url . '/oauth_access.php',
34 },
35 signature_method => 'PLAINTEXT',
36 );
37  
38 0 return $class->SUPER::new( %create );
39  
40 }
41  
42 0 ▶sub restricted_request {
Subroutine restricted_request: not called CC=5 SCAR=34.0
Pod documented
43  
44 0 my $self = shift;
45 0 my $url = shift;
46 0 my $content = shift;
47  
48 0 ▶ if ( !$self->authorized ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->authorized) 0 0 0
49 0 return $self->_error( "This restricted request is not authorized" );
50 }
51  
52 0 my %request = (
53 consumer_key => $self->consumer_key,
54 consumer_secret => $self->consumer_secret,
55 request_url => $url,
56 request_method => 'POST',
57 signature_method => $self->signature_method,
58 protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0A,
59 timestamp => time,
60 nonce => $self->_nonce,
61 token => $self->access_token,
62 token_secret => $self->access_token_secret,
63 );
64  
65 0 my $request = Net::OAuth::ProtectedResourceRequest->new( %request );
66  
67 0 $request->sign;
68  
69 0 ▶ if ( !$request->verify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->verify) 0 0 0
70 0 return $self->_error(
71 "Couldn't verify request! Check OAuth parameters." );
72 }
73  
74 0 my $params = $request->to_hash;
75 0 my @auth_header = ();
76  
77 # building the header here because the Net::OAuth::Simple stuff wasn't
78 # authenticating
79 0 foreach my $key ( keys %{ $request->to_hash } ) {
80 0 my $name = $key;
81 0 $name =~ s{-}{_}g;
82 0 push @auth_header, sprintf( '%s="%s"', $name, $params->{$key} );
83 }
84  
85 0 my $auth = 'OAuth realm="",' . join ",", @auth_header;
86 0 my $headers = HTTP::Headers->new( Authorization => $auth );
87  
88 0 my $req = HTTP::Request->new( 'POST' => $url, $headers, $content );
89  
90 0 my $response = $self->{browser}->request( $req );
91  
92 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
93 0 return $self->_error( "POST on "
94 . $request->normalized_request_url
95 . " failed: "
96 . $response->status_line . " - "
97 . $response->content );
98 }
99  
100 0 return $response;
101 }
102  
103 ##############################################################################
104 # the following methods can be deleted once a patched version of
105 # Net::OAuth::Simple has been released
106  
107 0 ▶sub request_access_token {
Subroutine request_access_token: not called CC=6 SCAR=37.4
Pod documented
108 0 my $self = shift;
109 0 my %params = @_;
110 0 my $url = $self->access_token_url;
111  
112 0 ▶ $params{token} = $self->request_token unless defined $params{token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"token"} 0 0 0
113 $params{token_secret} = $self->request_token_secret
114 0 ▶ unless defined $params{token_secret};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"token_secret"} 0 0 0
115  
116 0 ▶ if ( $self->oauth_1_0a ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->oauth_1_0a) 0 0 0
117 0 ▶ $params{verifier} = $self->verifier unless defined $params{verifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"verifier"} 0 0 0
118 return $self->_error(
119 "You must pass a verified parameter when using OAuth v1.0a" )
120 0 ▶ unless defined $params{verifier};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"verifier"} 0 0 0
121  
122 }
123  
124 0 my $access_token_response
125 = $self->_make_request( 'Net::OAuth::AccessTokenRequest',
126 $url, 'POST', %params, );
127  
128 0 return $self->_decode_tokens( $url, $access_token_response );
129 }
130  
131 0 ▶sub request_request_token {
Subroutine request_request_token: not called CC=7 SCAR=40.3
Pod documented
132 0 my $self = shift;
133 0 my %params = @_;
134 0 my $url = $self->request_token_url;
135  
136 0 ▶ if ( $self->oauth_1_0a ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->oauth_1_0a) 0 0 0
137 0 ▶ $params{callback} = $self->callback unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
138 return $self->_error(
139 "You must pass a callback parameter when using OAuth v1.0a" )
140 0 ▶ unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
141 }
142  
143 0 my $request_token_response
144 = $self->_make_request( 'Net::OAuth::RequestTokenRequest',
145 $url, 'POST', %params );
146  
147 0 ▶ return $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
unless $request_token_response->is_success 0 0 0
148 "GET for $url failed: " . $request_token_response->status_line )
149 unless ( $request_token_response->is_success );
150  
151 # Cast response into CGI query for EZ parameter decoding
152 0 my $request_token_response_query
153 = new CGI( $request_token_response->content );
154  
155 # Split out token and secret parameters from the request token response
156 0 $self->request_token(
157 $request_token_response_query->param( 'oauth_token' ) );
158 0 $self->request_token_secret(
159 $request_token_response_query->param( 'oauth_token_secret' ) );
160 0 $self->callback_confirmed(
161 $request_token_response_query->param( 'oauth_callback_confirmed' ) );
162  
163 0 ▶ return $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
if $self->oauth_1_0a and not $self->callback_confirmed 0 0 0
Condition0%0/3
$self->oauth_1_0a and not $self->callback_confirmed
!ll&&!rl&&r
000
MC/DC0%0/2
$self->oauth_1_0a and not $self->callback_confirmed
$self->oauth_1_0a not $self->callback_confirmed
Truth table0%0/3
A and not(B)
A= $self->oauth_1_0aB= not $self->callback_confirmed
ABresult
0X0
100
111
164 "Response does not confirm to OAuth1.0a. oauth_callback_confirmed not received"
165 ) if $self->oauth_1_0a && !$self->callback_confirmed;
166  
167 }
168  
169 0 ▶sub _make_request {
Subroutine _make_request: not called CC=4 SCAR=30.0
170 0 my $self = shift;
171 0 my $class = shift;
172 0 my $url = shift;
173 0 my $method = uc( shift );
174 0 my @extra = @_;
175  
176 0 my $uri = URI->new( $url );
177 0 my %query = $uri->query_form;
178 0 $uri->query_form( {} );
179  
180 0 ▶ my $request = $class->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->oauth_1_0a ? : 0 0 0
181 consumer_key => $self->consumer_key,
182 consumer_secret => $self->consumer_secret,
183 request_url => $uri,
184 request_method => $method,
185 signature_method => $self->signature_method,
186 protocol_version => $self->oauth_1_0a
187 ? Net::OAuth::PROTOCOL_VERSION_1_0A
188 : Net::OAuth::PROTOCOL_VERSION_1_0,
189 timestamp => time,
190 nonce => $self->_nonce,
191 extra_params => \%query,
192 @extra,
193 );
194 0 $request->sign;
195 0 ▶ return $self->_error( "Couldn't verify request! Check OAuth parameters." )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->verify 0 0 0
196 unless $request->verify;
197  
198 0 my $params = $request->to_hash;
199 0 $uri->query_form( %$params );
200 0 my $req = HTTP::Request->new( $method => "$uri" );
201 0 my $response = $self->{browser}->request( $req );
202 0 ▶ return $self->_error( "$method on "
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
203 . $request->normalized_request_url
204 . " failed: "
205 . $response->status_line . " - "
206 . $response->content )
207 unless ( $response->is_success );
208  
209 0 return $response;
210 }
211  
212 # ABSTRACT: FreshBooks OAuth implementation
213  
214  
215 8 1;
216  
217 __END__
« API/Role/CRUD.pm ↑ Summary API.pm »