OAuth2ServiceAccountsHandler.pm

filter: Statementstmt 72.4% 50 / 69 Branchbran 25.0% 3 / 12 Conditioncond 54.5% 6 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 92.8% 13 / 14 Podpod 100% 1 / 1 totaltotal 66.6% 76 / 114 CC 11 SCAR 19.1
CC
11
Cov
64%
_refresh_access_token
19.1
__read_json_key_file
12.0
initialize
4.2
CRAP 6.8
« OAuth2ApplicationsHandler.pm ↑ Summary Utils/PartialFailureUtils.pm »
1 # Copyright 2021, Google LLC
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::GoogleAds::OAuth2ServiceAccountsHandler;
16  
17 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use base qw(Google::Ads::GoogleAds::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 11 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 11 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶use JSON::WebToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Class::Std-style attributes. Need to be kept in the same line.
31 # These need to go in the same line for older Perl interpreters to understand.
32 11 my %json_key_file_path_of : ATTR(:name<json_key_file_path> :default<>);
33 11 my %impersonated_email_of : ATTR(:name<impersonated_email> :default<>);
34 11 my %additional_scopes_of : ATTR(:name<additional_scopes> :default<>);
35  
36 # Methods from Google::Ads::GoogleAds::Common::AuthHandlerInterface.
37 9 ▶sub initialize : CUMULATIVE(BASE FIRST) {
Subroutine initialize: called CC=4 SCAR=14.2
Pod documented
38 9 my ($self, $api_client, $properties) = @_;
39 9 my $ident = ident $self;
40  
41 $json_key_file_path_of{$ident} = $properties->{jsonKeyFilePath}
42 9 ▶ || $json_key_file_path_of{$ident};
Condition66%2/3
$properties->{'jsonKeyFilePath'} || $json_key_file_path_of{$ident}
l!l&&r!l&&!r
108
MC/DC50%1/2
$properties->{'jsonKeyFilePath'} || $json_key_file_path_of{$ident}
$properties->{'jsonKeyFilePath'} $json_key_file_path_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'jsonKeyFilePath'}B= $json_key_file_path_of{$ident}
ABresult
1X1
011
000
43 $impersonated_email_of{$ident} = $properties->{impersonatedEmail}
44 9 ▶ || $impersonated_email_of{$ident};
Condition66%2/3
$properties->{'impersonatedEmail'} || $impersonated_email_of{$ident}
l!l&&r!l&&!r
108
MC/DC50%1/2
$properties->{'impersonatedEmail'} || $impersonated_email_of{$ident}
$properties->{'impersonatedEmail'} $impersonated_email_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'impersonatedEmail'}B= $impersonated_email_of{$ident}
ABresult
1X1
011
000
45  
46 # Below attributes are not in the googleads.properties configuration.
47 $additional_scopes_of{$ident} = $properties->{additionalScopes}
48 9 ▶ || $additional_scopes_of{$ident};
Condition66%2/3
$properties->{'additionalScopes'} || $additional_scopes_of{$ident}
l!l&&r!l&&!r
108
MC/DC50%1/2
$properties->{'additionalScopes'} || $additional_scopes_of{$ident}
$properties->{'additionalScopes'} $additional_scopes_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'additionalScopes'}B= $additional_scopes_of{$ident}
ABresult
1X1
011
000
49 11 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # Methods from Google::Ads::GoogleAds::Common::OAuth2BaseHandler.
52 1 ▶sub _refresh_access_token {
Subroutine _refresh_access_token: called CC=5 SCAR=29.5
53 1 my $self = shift;
54  
55 1 ▶ if (!$self->get_json_key_file_path()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_json_key_file_path) 1 0 1
56 1 return 0;
57 }
58  
59 0 ▶ my $json_key = $self->__read_json_key_file() || return 0;
Condition0%0/2
$self->__read_json_key_file || (return 0)
l!l
00
MC/DC0%0/1
$self->__read_json_key_file || (return 0)
$self->__read_json_key_file
Truth table0%0/2
A
A= $self->__read_json_key_file
Aresult
11
00
60 0 my $time = time;
61  
62 my $jwt = JSON::WebToken->encode({
63 iss => $json_key->{client_email},
64 scope => $self->__formatted_scopes(),
65 aud => Google::Ads::GoogleAds::Constants::OAUTH2_BASE_URL . "/token",
66 exp => $time + 3600,
67 iat => $time,
68 sub => $self->get_impersonated_email()
69 },
70 $json_key->{private_key},
71 0 "RS256"
72 );
73  
74 0 my $response = $self->get___lwp_agent()->post(
75 Google::Ads::GoogleAds::Constants::OAUTH2_BASE_URL . "/token",
76 {
77 grant_type => "urn:ietf:params:oauth:grant-type:jwt-bearer",
78 assertion => $jwt
79 });
80  
81 0 ▶ if (!$response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
82 0 my $err_msg = $response->decoded_content();
83 0 ▶ $self->get_api_client()->get_die_on_faults()
Branch: 1 branch
BranchTrueFalseTotal
$self->get_api_client->get_die_on_faults ? : 0 0 0
84 ? die($err_msg)
85 : warn($err_msg);
86 0 return 0;
87 }
88  
89 0 my $content_hash = $self->__parse_auth_response($response->decoded_content());
90  
91 0 $self->set_access_token($content_hash->{access_token});
92 0 $self->set_access_token_expires(time + $content_hash->{expires_in});
93  
94 0 return 1;
95 }
96  
97 5 ▶sub _scope {
Subroutine _scope: called CC=2 SCAR=6.9
98 5 my $self = shift;
99 5 my @parsed_scopes = ();
100 5 my $additional_scopes = $self->get_additional_scopes();
101 5 ▶ if ($additional_scopes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($additional_scopes) 3 2 5
102 3 @parsed_scopes = split(/\s*,\s*/, $additional_scopes);
103 }
104 5 push @parsed_scopes, Google::Ads::GoogleAds::Constants::DEFAULT_OAUTH2_SCOPE;
105 5 return @parsed_scopes;
106 }
107  
108 # Retrieves the OAuth2 scopes defined in _scope as a list separated by commas.
109 # This is the format expected when sending the OAuth request.
110 2 ▶sub __formatted_scopes {
Subroutine __formatted_scopes: called CC=1 SCAR=0.0
111 2 my $self = shift;
112 2 my @parsed_scopes = $self->_scope();
113 2 return join(',', @parsed_scopes);
114 }
115  
116 # Reads the values from the specified JSON key file path as a JSON object.
117 0 ▶sub __read_json_key_file {
Subroutine __read_json_key_file: not called CC=3 SCAR=24.8
118 0 my $self = shift;
119  
120 0 my $json_text;
121 0 ▶ open(KEY_FILE, $self->get_json_key_file_path()) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
open KEY_FILE, $self->get_json_key_file_path or return 0 0 0 0
122 0 ▶ while (<KEY_FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline KEY_FILE)) 0 0 0
123 0 $json_text .= $_;
124 }
125 0 close(KEY_FILE);
126  
127 0 return decode_json($json_text);
128 }
129  
130 11 1;
131  
132 =pod
133  
134 =head1 NAME
135  
136 Google::Ads::GoogleAds::OAuth2ServiceAccountsHandler
137  
138 =head1 DESCRIPTION
139  
140 A concrete implementation of L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler>
141 that supports OAuth2 for service accounts and defines the scope required to
142 access the Google Ads API server.
143  
144 See L<https://developers.google.com/identity/protocols/oauth2/service-account>
145 for details of the protocol.
146  
147 =head1 ATTRIBUTES
148  
149 Each of these attributes can be set via
150 Google::Ads::GoogleAds::OAuth2ServiceAccountsHandler->new().
151  
152 Alternatively, there is a get_ and set_ method associated with each attribute
153 for retrieving or setting them dynamically.
154  
155 =head2 api_client
156  
157 A reference to the API client used to handle the API requests.
158  
159 =head2 json_key_file_path
160  
161 The absolute path to the local JSON key file for the OAuth2 service account.
162  
163 =head2 impersonated_email
164  
165 The email address account to impersonate, when the service account has been
166 delegated domain wide access.
167  
168 =head2 access_token
169  
170 Stores an OAuth2 access token after the authorization flow is followed or for
171 you to manually set it in case you had it previously stored. If this is manually
172 set this handler will verify its validity before preparing a request.
173  
174 =head2 additional_scopes
175  
176 Stores additional OAuth2 scopes as a comma-separated string.
177 These scopes define which services the tokens are allowed to access,
178 e.g. https://www.googleapis.com/auth/analytics.
179  
180 =head1 METHODS
181  
182 =head2 initialize
183  
184 Initializes the handler with the API client object and the properties such as
185 json_key_file_path and impersonated_email, used for generating authorization
186 requests.
187  
188 =head3 Parameters
189  
190 =over
191  
192 =item *
193  
194 A required I<api_client> with a reference to the API client object handling the
195 requests against the API.
196  
197 =item *
198  
199 A hash reference with the following keys:
200  
201 {
202 jsonKeyFilePath => "json-key-file-path",
203 impersonatedEmail => "impersonated-email",
204 accessToken => "access-token",
205 additionalScopes => "additional-scopes",
206 }
207  
208 Refer to the documentation of the properties as L</json_key_file_path>,
209 L</impersonated_email>, L</access_token> and L</additional_scopes>.
210  
211 =back
212  
213 =head2 prepare_request
214  
215 Refer to L<Google::Ads::GoogleAds::Common::AuthHandlerInterface> documentation
216 of this method.
217  
218 =head2 is_auth_enabled
219  
220 Refer to L<Google::Ads::GoogleAds::Common::AuthHandlerInterface> documentation
221 of this method.
222  
223 =head2 _scope
224  
225 Method defined by L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler> and
226 implemented in this class to return the required OAuth2 scopes as an array.
227  
228 =head2 _refresh_access_token
229  
230 Method defined by L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler> and
231 implemented in this class to refresh the stored OAuth2 access token.
232  
233 =head2 __formatted_scopes
234  
235 Private method to return the OAuth2 scopes as a list of strings separated by
236 commas. This is the format expected when sending the OAuth request.
237  
238 =head3 Returns
239  
240 The string of OAuth2 scopes separated by commas.
241  
242 =head1 LICENSE AND COPYRIGHT
243  
244 Copyright 2021 Google LLC
245  
246 Licensed under the Apache License, Version 2.0 (the "License");
247 you may not use this file except in compliance with the License.
248 You may obtain a copy of the License at
249  
250 http://www.apache.org/licenses/LICENSE-2.0
251  
252 Unless required by applicable law or agreed to in writing, software
253 distributed under the License is distributed on an "AS IS" BASIS,
254 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
255 See the License for the specific language governing permissions and
256 limitations under the License.
257  
258 =head1 REPOSITORY INFORMATION
259  
260 $Rev: $
261 $LastChangedBy: $
262 $Id: $
263  
264 =cut
« OAuth2ApplicationsHandler.pm ↑ Summary Utils/PartialFailureUtils.pm »