OAuth2ApplicationsHandler.pm

filter: Statementstmt 86.3% 76 / 88 Branchbran 50.0% 6 / 12 Conditioncond 57.6% 15 / 26 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 73.7% 118 / 160 CC 17 SCAR 19.6
CC
17
Cov
77%
_refresh_access_token
22.9
initialize
7.6
get_authorization_url
3.1
CRAP 7.1
« Utils/FieldMasks.pm ↑ Summary OAuth2ServiceAccountsHandler.pm »
1 # Copyright 2022, 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::OAuth2ApplicationsHandler;
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 11 Google::Ads::GoogleAds::Common::OAuthApplicationsHandlerInterface);
22  
23 # The following needs to be on one line because CPAN uses a particularly hacky
24 # eval() to determine module versions.
25 11 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 11 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Class::Std-style attributes. Need to be kept in the same line.
32 # These need to go in the same line for older Perl interpreters to understand.
33 11 my %client_secret_of : ATTR(:name<client_secret> :default<>);
34 11 my %access_type_of : ATTR(:name<access_type> :default<offline>);
35 11 my %prompt_of : ATTR(:name<prompt> :default<consent>);
36 11 my %refresh_token_of : ATTR(:name<refresh_token> :default<>);
37 11 my %redirect_uri_of : ATTR(:name<redirect_uri> :default<http://127.0.0.1>);
38 11 my %additional_scopes_of : ATTR(:name<additional_scopes> :default<>);
39  
40 # Methods from Google::Ads::GoogleAds::Common::AuthHandlerInterface.
41 9 ▶sub initialize : CUMULATIVE(BASE FIRST) {
Subroutine initialize: called CC=7 SCAR=20.3
Pod documented
42 9 my ($self, $api_client, $properties) = @_;
43 9 my $ident = ident $self;
44  
45 $client_secret_of{$ident} = $properties->{clientSecret}
46 9 ▶ || $client_secret_of{$ident};
Condition66%2/3
$properties->{'clientSecret'} || $client_secret_of{$ident}
l!l&&r!l&&!r
702
MC/DC50%1/2
$properties->{'clientSecret'} || $client_secret_of{$ident}
$properties->{'clientSecret'} $client_secret_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'clientSecret'}B= $client_secret_of{$ident}
ABresult
1X1
011
000
47 $refresh_token_of{$ident} = $properties->{refreshToken}
48 9 ▶ || $refresh_token_of{$ident};
Condition66%2/3
$properties->{'refreshToken'} || $refresh_token_of{$ident}
l!l&&r!l&&!r
702
MC/DC50%1/2
$properties->{'refreshToken'} || $refresh_token_of{$ident}
$properties->{'refreshToken'} $refresh_token_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'refreshToken'}B= $refresh_token_of{$ident}
ABresult
1X1
011
000
49  
50 # Below attributes are not in the googleads.properties configuration.
51 $access_type_of{$ident} = $properties->{accessType}
52 9 ▶ || $access_type_of{$ident};
Condition66%2/3
$properties->{'accessType'} || $access_type_of{$ident}
l!l&&r!l&&!r
180
MC/DC0%0/2
$properties->{'accessType'} || $access_type_of{$ident}
$properties->{'accessType'} $access_type_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'accessType'}B= $access_type_of{$ident}
ABresult
1X1
011
000
53 $prompt_of{$ident} = $properties->{approvalPrompt}
54 9 ▶ || $prompt_of{$ident};
Condition66%2/3
$properties->{'approvalPrompt'} || $prompt_of{$ident}
l!l&&r!l&&!r
180
MC/DC0%0/2
$properties->{'approvalPrompt'} || $prompt_of{$ident}
$properties->{'approvalPrompt'} $prompt_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'approvalPrompt'}B= $prompt_of{$ident}
ABresult
1X1
011
000
55 $redirect_uri_of{$ident} = $properties->{redirectUri}
56 9 ▶ || $redirect_uri_of{$ident};
Condition66%2/3
$properties->{'redirectUri'} || $redirect_uri_of{$ident}
l!l&&r!l&&!r
180
MC/DC0%0/2
$properties->{'redirectUri'} || $redirect_uri_of{$ident}
$properties->{'redirectUri'} $redirect_uri_of{$ident}
Truth table66%2/3
A || B
A= $properties->{'redirectUri'}B= $redirect_uri_of{$ident}
ABresult
1X1
011
000
57 $additional_scopes_of{$ident} = $properties->{additionalScopes}
58 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
59 11 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # Methods from Google::Ads::GoogleAds::Common::OAuthApplicationsHandlerInterface.
62 1 ▶sub get_authorization_url {
Subroutine get_authorization_url: called CC=3 SCAR=11.2
Pod documented
63 1 my ($self, $state) = @_;
64  
65 1 ▶ $state ||= "";
Condition50%1/2
$state ||= ""
l!l
10
MC/DC0%0/1
$state ||= ""
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
66 1 my ($client_id, $redirect_uri, $access_type, $prompt) = (
67 $self->get_client_id(), $self->get_redirect_uri(),
68 $self->get_access_type(), $self->get_prompt());
69  
70 1 my $authorization_url =
71 Google::Ads::GoogleAds::Constants::OAUTH2_BASE_URL .
72 "/auth?response_type=code" .
73 "&client_id=" . uri_escape($client_id) . "&redirect_uri=" .
74 $redirect_uri . "&scope=" . $self->__formatted_scopes() .
75 "&access_type=" . $access_type . "&prompt=" . $prompt;
76  
77 1 ▶ $authorization_url .= ("&state=" . uri_escape($state)) if $state;
Branch: 1 branch
BranchTrueFalseTotal
if $state 1 0 1
78  
79 1 return $authorization_url;
80 }
81  
82 2 ▶sub issue_access_token {
Subroutine issue_access_token: called CC=2 SCAR=6.9
Pod documented
83 2 my ($self, $authorization_code) = @_;
84  
85 2 my $body =
86 "code=" . uri_escape($authorization_code) .
87 "&client_id=" . uri_escape($self->get_client_id()) . "&client_secret=" .
88 uri_escape($self->get_client_secret()) . "&redirect_uri=" .
89 uri_escape($self->get_redirect_uri()) . "&grant_type=authorization_code";
90  
91 2 push my @headers, "Content-Type" => "application/x-www-form-urlencoded";
92 2 my $request =
93 HTTP::Request->new("POST",
94 Google::Ads::GoogleAds::Constants::OAUTH2_BASE_URL . "/token",
95 \@headers, $body);
96 2 my $response = $self->get___lwp_agent()->request($request);
97  
98 2 ▶ if (!$response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 1 2
99 1 return $response->decoded_content();
100 }
101  
102 1 my $content_hash = $self->__parse_auth_response($response->decoded_content());
103  
104 1 $self->set_access_token($content_hash->{access_token});
105 1 $self->set_refresh_token($content_hash->{refresh_token});
106 1 $self->set_access_token_expires(time + $content_hash->{expires_in});
107  
108 1 return undef;
109 }
110  
111 # Methods from Google::Ads::GoogleAds::Common::OAuth2BaseHandler.
112 1 ▶sub _refresh_access_token {
Subroutine _refresh_access_token: called CC=6 SCAR=31.3
113 1 my $self = shift;
114  
115 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_client_id and $self->get_client_secret and $self->get_refresh_token) 1 0 1
Condition33%2/6
$self->get_client_id and $self->get_client_secret and $self->get_refresh_token
!ll&&!rl&&r
100
$self->get_client_id and $self->get_client_secret
!ll&&!rl&&r
100
MC/DC0%0/3
$self->get_client_id and $self->get_client_secret and $self->get_refresh_token
$self->get_client_id $self->get_client_secret $self->get_refresh_token
Truth table25%1/4
A and B and C
A= $self->get_client_idB= $self->get_client_secretC= $self->get_refresh_token
ABCresult
0XX0
10X0
1100
1111
116 !(
117 $self->get_client_id()
118 && $self->get_client_secret()
119 && $self->get_refresh_token()))
120 {
121 1 return 0;
122 }
123  
124 0 my $body =
125 "refresh_token=" . uri_escape($self->get_refresh_token()) .
126 "&client_id=" . uri_escape($self->get_client_id()) . "&client_secret=" .
127 uri_escape($self->get_client_secret()) . "&grant_type=refresh_token";
128  
129 0 push my @headers, "Content-Type" => "application/x-www-form-urlencoded";
130  
131 0 my $request =
132 HTTP::Request->new("POST",
133 Google::Ads::GoogleAds::Constants::OAUTH2_BASE_URL . "/token",
134 \@headers, $body);
135 0 my $response = $self->get___lwp_agent()->request($request);
136  
137 0 ▶ if (!$response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
138 0 my $err_msg = $response->decoded_content();
139 0 ▶ $self->get_api_client()->get_die_on_faults()
Branch: 1 branch
BranchTrueFalseTotal
$self->get_api_client->get_die_on_faults ? : 0 0 0
140 ? die($err_msg)
141 : warn($err_msg);
142 0 return 0;
143 }
144  
145 0 my $content_hash = $self->__parse_auth_response($response->decoded_content());
146  
147 0 $self->set_access_token($content_hash->{access_token});
148 0 $self->set_access_token_expires(time + $content_hash->{expires_in});
149  
150 0 return 1;
151 }
152  
153 6 ▶sub _scope {
Subroutine _scope: called CC=2 SCAR=6.9
154 6 my $self = shift;
155 6 my @parsed_scopes = ();
156 6 my $additional_scopes = $self->get_additional_scopes();
157 6 ▶ if ($additional_scopes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($additional_scopes) 4 2 6
158 4 @parsed_scopes = split(/\s*,\s*/, $additional_scopes);
159 }
160 6 push @parsed_scopes, Google::Ads::GoogleAds::Constants::DEFAULT_OAUTH2_SCOPE;
161 6 return @parsed_scopes;
162 }
163  
164 # Retrieves the OAuth2 scopes defined in _scope as a list of encoded URLs
165 # separated by pluses. This is the format expected when sending the OAuth2
166 # request in a URL.
167 3 ▶sub __formatted_scopes {
Subroutine __formatted_scopes: called CC=2 SCAR=6.9
168 3 my $self = shift;
169 3 my @parsed_scopes = $self->_scope();
170 # Removes spaces and replaces commas with pluses. Encode the URI.
171 # Don't encode the plus!
172 # Example:
173 # https://www.googleapis.com/auth/adwords,https://
174 # www.googleapis.com/auth/analytics
175 # changes to
176 # https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords+https%3A%2F%2F
177 # www.googleapis.com%2Fauth%2Fanalytics
178 3 foreach my $single_scope (@parsed_scopes) {
179 5 $single_scope = uri_escape($single_scope);
180 }
181 3 return join('+', @parsed_scopes);
182 }
183  
184 11 1;
185  
186 =pod
187  
188 =head1 NAME
189  
190 Google::Ads::GoogleAds::OAuth2ApplicationsHandler
191  
192 =head1 DESCRIPTION
193  
194 A concrete implementation of L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler>
195 and L<Google::Ads::GoogleAds::Common::OAuthApplicationsHandlerInterface> that
196 supports OAuth2 for Web/Desktop applications and defines the scope required to
197 access the Google Ads API server.
198  
199 See L<https://developers.google.com/identity/protocols/oauth2> for details of
200 the protocol.
201  
202 =head1 ATTRIBUTES
203  
204 Each of these attributes can be set via
205 Google::Ads::GoogleAds::OAuth2ApplicationsHandler->new().
206  
207 Alternatively, there is a get_ and set_ method associated with each attribute
208 for retrieving or setting them dynamically.
209  
210 =head2 api_client
211  
212 A reference to the API client used to handle the API requests.
213  
214 =head2 client_id
215  
216 OAuth2 client id obtained from the Google APIs console.
217  
218 =head2 client_secret
219  
220 OAuth2 client secret obtained from the Google APIs console.
221  
222 =head2 access_type
223  
224 OAuth2 access type to be requested when following the authorization flow. It
225 defaults to offline but it can be set to online.
226  
227 =head2 prompt
228  
229 OAuth2 prompt to be used when following the authorization flow. It defaults to
230 consent.
231  
232 =head2 redirect_uri
233  
234 Redirect URI to which the authorization flow will callback with the authorization
235 code. If using Web flow, the redirect URI must match exactly what’s configured in
236 GCP for the OAuth client. If using Desktop flow, the redirect must be a localhost
237 URL and is not explicitly set in GCP. The default is http://127.0.0.1.
238  
239 =head2 access_token
240  
241 Stores an OAuth2 access token after the authorization flow is followed or for
242 you to manually set it in case you had it previously stored. If this is manually
243 set this handler will verify its validity before preparing a request.
244  
245 =head2 refresh_token
246  
247 Stores an OAuth2 refresh token in case of an offline L</access_type> is
248 requested. It is automatically used by the handler to request new access tokens,
249 i.e. when they are expired or found invalid.
250  
251 =head2 additional_scopes
252  
253 Stores additional OAuth2 scopes as a comma-separated string.
254 These scopes define which services the tokens are allowed to access,
255 e.g. https://www.googleapis.com/auth/analytics.
256  
257 =head1 METHODS
258  
259 =head2 initialize
260  
261 Initializes the handler with the API client object and the properties such as
262 client_id and client_secret, used for generating authorization requests.
263  
264 =head3 Parameters
265  
266 =over
267  
268 =item *
269  
270 A required I<api_client> with a reference to the API client object handling the
271 requests against the API.
272  
273 =item *
274  
275 A hash reference with the following keys:
276  
277 {
278 clientId => "client-id",
279 clientSecret => "client-secret",
280 accessType => "access-type",
281 approvalPrompt => "approval-prompt",
282 redirectUri => "redirect-uri",
283 accessToken => "access-token",
284 refreshToken => "refresh-token",
285 additionalScopes => "additional-scopes",
286 }
287  
288 Refer to the documentation of the properties as L</client_id>, L</client_secret>,
289 L</access_type>, L</prompt>, L</redirect_uri>, L</access_token>, L</refresh_token>
290 and L</additional_scopes>.
291  
292 =back
293  
294 =head2 prepare_request
295  
296 Refer to L<Google::Ads::GoogleAds::Common::AuthHandlerInterface> documentation
297 of this method.
298  
299 =head2 is_auth_enabled
300  
301 Refer to L<Google::Ads::GoogleAds::Common::AuthHandlerInterface> documentation
302 of this method.
303  
304 =head2 get_authorization_url
305  
306 Refer to L<Google::Ads::GoogleAds::Common::OAuthApplicationsHandlerInterface>
307 documentation of this method.
308  
309 =head2 issue_access_token
310  
311 Refer to L<Google::Ads::GoogleAds::Common::OAuthApplicationsHandlerInterface>
312 documentation of this method.
313  
314 =head2 _scope
315  
316 Method defined by L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler> and
317 implemented in this class to return the required OAuth2 scopes as an array.
318  
319 =head2 _refresh_access_token
320  
321 Method defined by L<Google::Ads::GoogleAds::Common::OAuth2BaseHandler> and
322 implemented in this class to refresh the stored OAuth2 access token.
323  
324 =head2 __formatted_scopes
325  
326 Private method to return the OAuth2 scopes as a list of encoded URLs separated
327 by pluses. This is the format expected when sending the OAuth2 request in a URL.
328  
329 =head3 Returns
330  
331 The encoded URL string of OAuth2 scopes separated by pluses.
332  
333 =head1 LICENSE AND COPYRIGHT
334  
335 Copyright 2022 Google LLC
336  
337 Licensed under the Apache License, Version 2.0 (the "License");
338 you may not use this file except in compliance with the License.
339 You may obtain a copy of the License at
340  
341 http://www.apache.org/licenses/LICENSE-2.0
342  
343 Unless required by applicable law or agreed to in writing, software
344 distributed under the License is distributed on an "AS IS" BASIS,
345 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
346 See the License for the specific language governing permissions and
347 limitations under the License.
348  
349 =head1 REPOSITORY INFORMATION
350  
351 $Rev: $
352 $LastChangedBy: $
353 $Id: $
354  
355 =cut
« Utils/FieldMasks.pm ↑ Summary OAuth2ServiceAccountsHandler.pm »