blib/lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.pm

filter: Statementstmt 40.5% 47 / 116 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 65.0% 13 / 20 Podpod 50.0% 1 / 2 totaltotal 30.5% 61 / 200 CC 20 SCAR 34.7
CC
20
Cov
29%
__read_certificate_file
72.0
_refresh_access_token
56.0
initialize
42.0
CRAP 32.1
« lib/Google/Ads/AdWords/Utilities/ReportQueryBuilder.pm ↑ Summary lib/Google/Ads/AdWords/Utilities/ServiceQueryBuilder.pm »
1 # Copyright 2013, Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Ads::Common::OAuth2ServiceAccountsHandler;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use base qw(Google::Ads::Common::OAuth2BaseHandler);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # The following needs to be on one line because CPAN uses a particularly hacky
23 # eval() to determine module versions.
24 1 ▶use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use JSON::Parse qw(parse_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use constant OAUTH2_BASE_URL => "https://accounts.google.com/o/oauth2";
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # Class::Std-style attributes. Need to be kept in the same line.
36 # These need to go in the same line for older Perl interpreters to understand.
37 1 my %email_address_of : ATTR(:name<email_address> :default<>);
38 1 my %delegated_email_address_of :
39 ATTR(:name<delegated_email_address> :default<>);
40 1 my %additional_scopes_of : ATTR(:name<additional_scopes> :default<>);
41 1 my %pem_file_of : ATTR(:name<pem_file> :default<>);
42 1 my %json_file_of : ATTR(:name<json_file> :default<>);
43 1 my %__crypt_module_of : ATTR(:name<__crypt_module> :default<>);
44  
45 # Constructor
46 0 ▶sub START {
Subroutine START: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my ($self, $ident) = @_;
48  
49 0 ▶ $__crypt_module_of{$ident} ||= "Crypt::OpenSSL::RSA";
Condition0%0/2
$__crypt_module_of{$ident} ||= "Crypt::OpenSSL::RSA"
l!l
00
MC/DC0%0/1
$__crypt_module_of{$ident} ||= "Crypt::OpenSSL::RSA"
$__crypt_module_of{$ident}
Truth table0%0/2
A
A= $__crypt_module_of{$ident}
Aresult
11
00
50 }
51  
52 0 ▶sub initialize : CUMULATIVE(BASE FIRST) {
Subroutine initialize: not called CC=6 SCAR=37.4
Pod documented
53 0 my ($self, $api_client, $properties) = @_;
54 0 my $ident = ident $self;
55  
56 $email_address_of{$ident} = $properties->{oAuth2ServiceAccountEmailAddress}
57 0 ▶ || $email_address_of{$ident};
Condition0%0/3
$properties->{'oAuth2ServiceAccountEmailAddress'} || $email_address_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ServiceAccountEmailAddress'} || $email_address_of{$ident}
$properties->{'oAuth2ServiceAccountEmailAddress'} $email_address_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ServiceAccountEmailAddress'}B= $email_address_of{$ident}
ABresult
1X1
011
000
58 $delegated_email_address_of{$ident} =
59 $properties->{oAuth2ServiceAccountDelegateEmailAddress}
60 0 ▶ || $delegated_email_address_of{$ident};
Condition0%0/3
$properties->{'oAuth2ServiceAccountDelegateEmailAddress'} || $delegated_email_address_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ServiceAccountDelegateEmailAddress'} || $delegated_email_address_of{$ident}
$properties->{'oAuth2ServiceAccountDelegateEmailAddress'} $delegated_email_address_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ServiceAccountDelegateEmailAddress'}B= $delegated_email_address_of{$ident}
ABresult
1X1
011
000
61 $pem_file_of{$ident} = $properties->{oAuth2ServiceAccountPEMFile}
62 0 ▶ || $pem_file_of{$ident};
Condition0%0/3
$properties->{'oAuth2ServiceAccountPEMFile'} || $pem_file_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ServiceAccountPEMFile'} || $pem_file_of{$ident}
$properties->{'oAuth2ServiceAccountPEMFile'} $pem_file_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ServiceAccountPEMFile'}B= $pem_file_of{$ident}
ABresult
1X1
011
000
63 $json_file_of{$ident} = $properties->{oAuth2ServiceAccountJSONFile}
64 0 ▶ || $json_file_of{$ident};
Condition0%0/3
$properties->{'oAuth2ServiceAccountJSONFile'} || $json_file_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ServiceAccountJSONFile'} || $json_file_of{$ident}
$properties->{'oAuth2ServiceAccountJSONFile'} $json_file_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ServiceAccountJSONFile'}B= $json_file_of{$ident}
ABresult
1X1
011
000
65 $additional_scopes_of{$ident} = $properties->{oAuth2AdditionalScopes}
66 0 ▶ || $additional_scopes_of{$ident};
Condition0%0/3
$properties->{'oAuth2AdditionalScopes'} || $additional_scopes_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2AdditionalScopes'} || $additional_scopes_of{$ident}
$properties->{'oAuth2AdditionalScopes'} $additional_scopes_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2AdditionalScopes'}B= $additional_scopes_of{$ident}
ABresult
1X1
011
000
67 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 0 ▶sub _refresh_access_token {
Subroutine _refresh_access_token: not called CC=7 SCAR=40.3
70 0 my $self = shift;
71  
72 0 ▶ if ($self->get_json_file() and $self->get_pem_file()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_json_file and $self->get_pem_file) 0 0 0
Condition0%0/3
$self->get_json_file and $self->get_pem_file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->get_json_file and $self->get_pem_file
$self->get_json_file $self->get_pem_file
Truth table0%0/3
A and B
A= $self->get_json_fileB= $self->get_pem_file
ABresult
0X0
100
111
73 0 warn("Only one of oAuth2ServiceAccountPEMFile or " .
74 "oAuth2ServiceAccountJSONFile can be specified in adwords.properties.");
75 0 return 0;
76 }
77  
78 0 ▶ my $file = $self->__read_certificate_file() || return 0;
Condition0%0/2
$self->__read_certificate_file || (return 0)
l!l
00
MC/DC0%0/1
$self->__read_certificate_file || (return 0)
$self->__read_certificate_file
Truth table0%0/2
A
A= $self->__read_certificate_file
Aresult
11
00
79  
80 0 my $iat = time;
81 0 my $exp = $iat + 3600;
82 0 my $iss = $self->get_email_address();
83 0 my $delegated_email_address = $self->get_delegated_email_address();
84 0 my $scope = $self->_formatted_scopes();
85  
86 0 my $header = '{"alg":"RS256","typ":"JWT"}';
87 0 my $claims = "{
88 \"iss\":\"${iss}\",
89 \"sub\":\"${delegated_email_address}\",
90 \"scope\":\"${scope}\",
91 \"aud\":\"" . OAUTH2_BASE_URL . "/token\",
92 \"exp\":${exp},
93 \"iat\":${iat}
94 }";
95  
96 0 my $encoded_header = __encode_base64_url($header);
97 0 my $encoded_claims = __encode_base64_url($claims);
98  
99 0 ▶ my $key = $self->get___crypt_module()->new_private_key($file) || return 0;
Condition0%0/2
$self->get___crypt_module->new_private_key($file) || (return 0)
l!l
00
MC/DC0%0/1
$self->get___crypt_module->new_private_key($file) || (return 0)
$self->get___crypt_module->new_private_key($file)
Truth table0%0/2
A
A= $self->get___crypt_module->new_private_key($file)
Aresult
11
00
100 0 $key->use_pkcs1_padding();
101 0 $key->use_sha256_hash();
102  
103 0 my $signature = $key->sign("${encoded_header}.${encoded_claims}");
104 0 my $encoded_signature = __encode_base64_url($signature);
105 0 my $assertion = "${encoded_header}.${encoded_claims}.${encoded_signature}";
106 0 my $body =
107 "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer" .
108 "&assertion=" . $assertion;
109 0 push my @headers, "Content-Type" => "application/x-www-form-urlencoded";
110 0 my $request =
111 HTTP::Request->new("POST", OAUTH2_BASE_URL . "/token", \@headers, $body);
112 0 my $user_agent = $self->get___user_agent();
113 0 my $res = $user_agent->request($request);
114  
115 0 ▶ if (!$res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
116 0 my $err_msg = $res->decoded_content();
117 0 ▶ $self->get_api_client()->get_die_on_faults()
Branch: 1 branch
BranchTrueFalseTotal
$self->get_api_client->get_die_on_faults ? : 0 0 0
118 ? die($err_msg)
119 : warn($err_msg);
120 0 return 0;
121 }
122  
123 0 my $content_hash = $self->__parse_auth_response($res->decoded_content());
124  
125 0 $self->set_access_token($content_hash->{access_token});
126 0 $self->set_access_token_expires($iat + $content_hash->{expires_in});
127 }
128  
129 # Return the private key string from either the PEM file or JSON file specified.
130 0 ▶sub __read_certificate_file {
Subroutine __read_certificate_file: not called CC=8 SCAR=42.8
131 0 my $self = shift;
132 0 my $private_key;
133  
134 0 ▶ if (!$self->get_pem_file() and !$self->get_json_file()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_pem_file or $self->get_json_file) 0 0 0
Condition0%0/3
$self->get_pem_file or $self->get_json_file
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_pem_file or $self->get_json_file
$self->get_pem_file $self->get_json_file
Truth table0%0/3
A or B
A= $self->get_pem_fileB= $self->get_json_file
ABresult
1X1
011
000
135 0 return 0;
136 }
137  
138 # JSON File
139 0 ▶ if ($self->get_json_file()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_json_file) { } 0 0 0
140 0 my $file_str;
141 0 ▶ open(MYFILE, $self->get_json_file()) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open MYFILE, $self->get_json_file or return 0 0 0 0
142 0 ▶ while (<MYFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MYFILE)) 0 0 0
143 0 $file_str .= $_;
144 }
145 0 my $json_values = parse_json ($file_str);
146 0 $private_key = $json_values->{'private_key'};
147 0 $self->set_email_address($json_values->{'client_email'});
148 0 close(MYFILE);
149 }
150 # PEM File
151 else {
152 0 ▶ open(MYFILE, $self->get_pem_file()) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open MYFILE, $self->get_pem_file or return 0 0 0 0
153 0 ▶ while (<MYFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MYFILE)) 0 0 0
154 0 $private_key .= $_;
155 }
156 0 close(MYFILE);
157 }
158  
159 0 return $private_key;
160 }
161  
162 0 ▶sub __encode_base64_url($) {
Subroutine __encode_base64_url: not called CC=1 SCAR=6.9
163 0 my ($s) = shift;
164 0 $s = encode_base64($s);
165 0 $s =~ tr{+/}{-_};
166 0 $s =~ s/=*$//;
167 0 $s =~ s/\n//g;
168 0 return $s;
169 }
170  
171 0 ▶sub _scope {
Subroutine _scope: not called CC=1 SCAR=6.9
172 0 my $self = shift;
173 0 die "Need to be implemented by subclass";
174 }
175  
176 0 ▶sub _formatted_scopes {
Subroutine _formatted_scopes: not called CC=1 SCAR=6.9
177 0 my $self = shift;
178 0 die "Need to be implemented by subclass";
179 }
180  
181 1 1;
182  
183 =pod
184  
185 =head1 NAME
186  
187 Google::Ads::Common::OAuth2ServiceAccountsHandler
188  
189 =head1 DESCRIPTION
190  
191 A generic abstract implementation of L<Google::Ads::Common::OAuth2BaseHandler>
192 that supports OAuth2 for Service Accounts semantics.
193  
194 It is meant to be specialized and its L<_scope> and L<_formatted_scopes> methods
195 be properly implemented.
196  
197 =head1 ATTRIBUTES
198  
199 Each of these attributes can be set via
200 Google::Ads::Common::OAuth2ServiceAccountsHandler->new().
201  
202 Alternatively, there is a get_ and set_ method associated with each attribute
203 for retrieving or setting them dynamically.
204  
205 =head2 api_client
206  
207 A reference to the API client used to send requests.
208  
209 =head2 client_id
210  
211 OAuth2 client id obtained from the Google APIs Console.
212  
213 =head2 email_address
214  
215 Service account email address as found in the Google API Console.
216  
217 =head2 delegated_email_address
218  
219 Delegated email address of the accounts that has access to the API.
220  
221 =head2 pem_file
222  
223 Private key PEM file path. Keep in mind that the Google API Console generates
224 files in PKCS12 format and it should be converted to PEM format with no password
225 for this module to function.
226  
227 =head2 json_file
228  
229 JOSN file path. This contains the private key and client id needed for
230 authentication.
231  
232 =head2 access_token
233  
234 Stores an OAuth2 access token after the authorization flow is followed or for
235 you to manually set it in case you had it previously stored.
236 If this is manually set this handler will verify its validity before preparing
237 a request.
238  
239 =head2 additional_scopes
240  
241 Stores additional OAuth2 scopes as a comma-separated string.
242 The scope defines which services the tokens
243 are allowed to access e.g. https://www.googleapis.com/auth/analytics
244  
245 =head1 METHODS
246  
247 =head2 initialize
248  
249 Initializes the handler with properties for generating authorization requests.
250  
251 =head3 Parameters
252  
253 =over
254  
255 =item *
256  
257 A required I<api_client> with a reference to the API client object handling the
258 requests against the API.
259  
260 =item *
261  
262 A hash reference with the following keys (with this example being for a simple
263 JSON keyfile):
264 {
265 # Refer to the documentation of the L<delegated_email_address> property.
266 oAuth2ServiceAccountDelegateEmailAddress => "delegated-email-address",
267 # Refer to the documentation of the L<json_file> property.
268 oAuth2ServiceAccountJSONFile => "json-file-path",
269 }
270  
271 =back
272  
273 =head2 is_auth_enabled
274  
275 Refer to L<Google::Ads::Common::AuthHandlerInterface> documentation of this
276 method.
277  
278 =head2 prepare_request
279  
280 Refer to L<Google::Ads::Common::AuthHandlerInterface> documentation of this
281 method.
282  
283 =head1 LICENSE AND COPYRIGHT
284  
285 Copyright 2013 Google Inc.
286  
287 Licensed under the Apache License, Version 2.0 (the "License");
288 you may not use this file except in compliance with the License.
289 You may obtain a copy of the License at
290  
291 http://www.apache.org/licenses/LICENSE-2.0
292  
293 Unless required by applicable law or agreed to in writing, software
294 distributed under the License is distributed on an "AS IS" BASIS,
295 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
296 See the License for the specific language governing permissions and
297 limitations under the License.
298  
299 =head1 REPOSITORY INFORMATION
300  
301 $Rev: $
302 $LastChangedBy: $
303 $Id: $
304  
305 =cut
« lib/Google/Ads/AdWords/Utilities/ReportQueryBuilder.pm ↑ Summary lib/Google/Ads/AdWords/Utilities/ServiceQueryBuilder.pm »