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

filter: Statementstmt 51.7% 44 / 85 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 70.5% 12 / 17 Podpod 100% 3 / 3 totaltotal 38.0% 59 / 155 CC 14 SCAR 31.2
CC
14
Cov
37%
initialize
56.0
_refresh_access_token
42.0
get_authorization_url
6.0
CRAP 22.7
« lib/Google/Ads/AdWords/Utilities/ServiceQueryBuilder.pm ↑ Summary lib/Google/Ads/Common/HTTPTransport.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::OAuth2ApplicationsHandler;
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 1 Google::Ads::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 1 ▶use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use constant OAUTH2_BASE_URL => "https://accounts.google.com/o/oauth2";
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant OAUTH2_TOKEN_INFO_URL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 "https://www.googleapis.com/oauth2/v1/tokeninfo";
35  
36 # Class::Std-style attributes. Need to be kept in the same line.
37 # These need to go in the same line for older Perl interpreters to understand.
38 1 my %client_secret_of : ATTR(:name<client_secret> :default<>);
39 1 my %access_type_of : ATTR(:name<access_type> :default<offline>);
40 1 my %prompt_of : ATTR(:name<prompt> :default<consent>);
41 1 my %refresh_token_of : ATTR(:name<refresh_token> :default<>);
42 1 my %redirect_uri_of :
43 ATTR(:name<redirect_uri> :default<urn:ietf:wg:oauth:2.0:oob>);
44 1 my %additional_scopes_of : ATTR(:name<additional_scopes> :default<>);
45  
46 # Methods from Google::Ads::Common::AuthHandlerInterface
47 0 ▶sub initialize : CUMULATIVE(BASE FIRST) {
Subroutine initialize: not called CC=7 SCAR=40.3
Pod documented
48 0 my ($self, $api_client, $properties) = @_;
49 0 my $ident = ident $self;
50  
51 $client_secret_of{$ident} = $properties->{oAuth2ClientSecret}
52 0 ▶ || $client_secret_of{$ident};
Condition0%0/3
$properties->{'oAuth2ClientSecret'} || $client_secret_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ClientSecret'} || $client_secret_of{$ident}
$properties->{'oAuth2ClientSecret'} $client_secret_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ClientSecret'}B= $client_secret_of{$ident}
ABresult
1X1
011
000
53 $access_type_of{$ident} = $properties->{oAuth2AccessType}
54 0 ▶ || $access_type_of{$ident};
Condition0%0/3
$properties->{'oAuth2AccessType'} || $access_type_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2AccessType'} || $access_type_of{$ident}
$properties->{'oAuth2AccessType'} $access_type_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2AccessType'}B= $access_type_of{$ident}
ABresult
1X1
011
000
55 $prompt_of{$ident} = $properties->{oAuth2ApprovalPrompt}
56 0 ▶ || $prompt_of{$ident};
Condition0%0/3
$properties->{'oAuth2ApprovalPrompt'} || $prompt_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ApprovalPrompt'} || $prompt_of{$ident}
$properties->{'oAuth2ApprovalPrompt'} $prompt_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ApprovalPrompt'}B= $prompt_of{$ident}
ABresult
1X1
011
000
57 $refresh_token_of{$ident} = $properties->{oAuth2RefreshToken}
58 0 ▶ || $refresh_token_of{$ident};
Condition0%0/3
$properties->{'oAuth2RefreshToken'} || $refresh_token_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2RefreshToken'} || $refresh_token_of{$ident}
$properties->{'oAuth2RefreshToken'} $refresh_token_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2RefreshToken'}B= $refresh_token_of{$ident}
ABresult
1X1
011
000
59 $redirect_uri_of{$ident} = $properties->{oAuth2RedirectUri}
60 0 ▶ || $redirect_uri_of{$ident};
Condition0%0/3
$properties->{'oAuth2RedirectUri'} || $redirect_uri_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2RedirectUri'} || $redirect_uri_of{$ident}
$properties->{'oAuth2RedirectUri'} $redirect_uri_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2RedirectUri'}B= $redirect_uri_of{$ident}
ABresult
1X1
011
000
61 $additional_scopes_of{$ident} = $properties->{oAuth2AdditionalScopes}
62 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
63 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 # Methods from Google::Ads::Common::OAuthHandlerInterface
66 0 ▶sub get_authorization_url {
Subroutine get_authorization_url: not called CC=2 SCAR=17.9
Pod documented
67 0 my ($self, $state) = @_;
68  
69 0 ▶ $state ||= "";
Condition0%0/2
$state ||= ""
l!l
00
MC/DC0%0/1
$state ||= ""
$state
Truth table0%0/2
A
A= $state
Aresult
11
00
70 0 my ($client_id, $redirect_uri, $access_type, $prompt) = (
71 $self->get_client_id(), $self->get_redirect_uri(),
72 $self->get_access_type(), $self->get_prompt());
73  
74 0 return OAUTH2_BASE_URL . "/auth?response_type=code" . "&client_id=" .
75 uri_escape($client_id) . "&redirect_uri=" . $redirect_uri . "&scope=" .
76 $self->_formatted_scopes() . "&access_type=" . $access_type .
77 "&prompt=" . $prompt . "&state=" . uri_escape($state);
78 }
79  
80 0 ▶sub issue_access_token {
Subroutine issue_access_token: not called CC=2 SCAR=17.9
Pod documented
81 0 my ($self, $authorization_code) = @_;
82  
83 0 my $body =
84 "code=" . uri_escape($authorization_code) . "&client_id=" .
85 uri_escape($self->get_client_id()) . "&client_secret=" .
86 uri_escape($self->get_client_secret()) . "&redirect_uri=" .
87 uri_escape($self->get_redirect_uri()) . "&grant_type=authorization_code";
88  
89 0 push my @headers, "Content-Type" => "application/x-www-form-urlencoded";
90 0 my $request =
91 HTTP::Request->new("POST", OAUTH2_BASE_URL . "/token", \@headers, $body);
92 0 my $res = $self->get___user_agent()->request($request);
93  
94 0 ▶ if (!$res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
95 0 return $res->decoded_content();
96 }
97  
98 0 my $content_hash = $self->__parse_auth_response($res->decoded_content());
99  
100 0 $self->set_access_token($content_hash->{access_token});
101 0 $self->set_refresh_token($content_hash->{refresh_token});
102 0 $self->set_access_token_expires(time + $content_hash->{expires_in});
103  
104 0 return undef;
105 }
106  
107 # Internal methods
108  
109 0 ▶sub _refresh_access_token {
Subroutine _refresh_access_token: not called CC=6 SCAR=37.4
110 0 my $self = shift;
111  
112 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_client_id and $self->get_client_secret and $self->get_refresh_token) 0 0 0
Condition0%0/6
$self->get_client_id and $self->get_client_secret and $self->get_refresh_token
!ll&&!rl&&r
000
$self->get_client_id and $self->get_client_secret
!ll&&!rl&&r
000
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 table0%0/4
A and B and C
A= $self->get_client_idB= $self->get_client_secretC= $self->get_refresh_token
ABCresult
0XX0
10X0
1100
1111
113 !(
114 $self->get_client_id()
115 && $self->get_client_secret()
116 && $self->get_refresh_token()))
117 {
118 0 return 0;
119 }
120  
121 0 my $body =
122 "refresh_token=" . uri_escape($self->get_refresh_token()) .
123 "&client_id=" . uri_escape($self->get_client_id()) . "&client_secret=" .
124 uri_escape($self->get_client_secret()) . "&grant_type=refresh_token";
125  
126 0 push my @headers, "Content-Type" => "application/x-www-form-urlencoded";
127 0 my $request =
128 HTTP::Request->new("POST", OAUTH2_BASE_URL . "/token", \@headers, $body);
129 0 my $res = $self->get___user_agent()->request($request);
130  
131 0 ▶ if (!$res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
132 0 my $err_msg = $res->decoded_content();
133 0 ▶ $self->get_api_client()->get_die_on_faults()
Branch: 1 branch
BranchTrueFalseTotal
$self->get_api_client->get_die_on_faults ? : 0 0 0
134 ? die($err_msg)
135 : warn($err_msg);
136 0 return 0;
137 }
138  
139 0 my $content_hash = $self->__parse_auth_response($res->decoded_content());
140  
141 0 $self->set_access_token($content_hash->{access_token});
142 0 $self->set_access_token_expires(time + $content_hash->{expires_in});
143  
144 0 return 1;
145 }
146  
147 0 ▶sub _formatted_scopes {
Subroutine _formatted_scopes: not called CC=1 SCAR=6.9
148 0 my $self = shift;
149 0 die "Need to be implemented by subclass";
150 }
151  
152 1 1;
153  
154 =pod
155  
156 =head1 NAME
157  
158 Google::Ads::Common::OAuth2ApplicationsHandler
159  
160 =head1 DESCRIPTION
161  
162 A generic abstract implementation of L<Google::Ads::Common::OAuth2BaseHandler>
163 that supports OAuth2 for Web/Install Applications semantics.
164  
165 It is meant to be specialized and its L<_scope> and L<_formatted_scopes> methods
166 be properly implemented.
167  
168 =head1 ATTRIBUTES
169  
170 Each of these attributes can be set via
171 Google::Ads::Common::OAuth2ApplicationsHandler->new().
172  
173 Alternatively, there is a get_ and set_ method associated with each attribute
174 for retrieving or setting them dynamically.
175  
176 =head2 api_client
177  
178 A reference to the API client used to send requests.
179  
180 =head2 client_id
181  
182 OAuth2 client id obtained from the Google APIs Console.
183  
184 =head2 client_secret
185  
186 OAuth2 client secret obtained from the Google APIs Console.
187  
188 =head2 access_type
189  
190 OAuth2 access type to be requested when following the authorization flow. It
191 defaults to offline but it can be set to online.
192  
193 =head2 prompt
194  
195 OAuth2 prompt to be used when following the authorization flow. It
196 defaults to consent.
197  
198 =head2 redirect_uri
199  
200 Redirect URI as set for you in the Google APIs console, to which the
201 authorization flow will callback with the verification code. Defaults to
202 urn:ietf:wg:oauth:2.0:oob for the installed applications flow.
203  
204 =head2 access_token
205  
206 Stores an OAuth2 access token after the authorization flow is followed or for
207 you to manually set it in case you had it previously stored.
208 If this is manually set this handler will verify its validity before preparing
209 a request.
210  
211 =head2 refresh_token
212  
213 Stores an OAuth2 refresh token in case of an offline L<access_type> is
214 requested. It is automatically used by the handler to request new access tokens
215 i.e. when they expire or found invalid.
216  
217 =head2 additional_scopes
218  
219 Stores additional OAuth2 scopes as a comma-separated string.
220 The scope defines which services the tokens
221 are allowed to access e.g. https://www.googleapis.com/auth/analytics
222  
223 =head1 METHODS
224  
225 =head2 initialize
226  
227 Initializes the handler with properties such as the client_id and
228 client_secret to use for generating authorization requests.
229  
230 =head3 Parameters
231  
232 =over
233  
234 =item *
235  
236 A required I<api_client> with a reference to the API client object handling the
237 requests against the API.
238  
239 =item *
240  
241 A hash reference with the following keys:
242 {
243 # Refer to the documentation of the L<client_secret> property.
244 oAuth2ClientId => "client-id",
245 # Refer to the documentation of the L<client_secret> property.
246 oAuth2ClientSecret => "client-secret",
247 # Refer to the documentation of the L<access_type> property.
248 oAuth2AccessType => "access-type",
249 # Refer to the documentation of the L<prompt> property.
250 oAuth2ApprovalPrompt => "approval-prompt",
251 # Refer to the documentation of the L<refresh_token> property.
252 oAuth2AccessToken => "access-token",
253 # Refer to the documentation of the L<refresh_token> property.
254 oAuth2RefreshToken => "refresh-token",
255 # Refer to the documentation of the L<redirect_uri> property.
256 oAuth2RedirectUri => "secret",
257 }
258  
259 =back
260  
261 =head2 is_auth_enabled
262  
263 Refer to L<Google::Ads::Common::AuthHandlerInterface> documentation of this
264 method.
265  
266 =head2 prepare_request
267  
268 Refer to L<Google::Ads::Common::AuthHandlerInterface> documentation of this
269 method.
270  
271 =head2 get_authorization_url
272  
273 Refer to L<Google::Ads::Common::OAuthApplicationsHandlerInterface> documentation
274 of this method.
275  
276 =head2 issue_access_token
277  
278 Refer to L<Google::Ads::Common::OAuthApplicationsHandlerInterface> documentation
279 of this method.
280  
281 =head1 LICENSE AND COPYRIGHT
282  
283 Copyright 2013 Google Inc.
284  
285 Licensed under the Apache License, Version 2.0 (the "License");
286 you may not use this file except in compliance with the License.
287 You may obtain a copy of the License at
288  
289 http://www.apache.org/licenses/LICENSE-2.0
290  
291 Unless required by applicable law or agreed to in writing, software
292 distributed under the License is distributed on an "AS IS" BASIS,
293 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
294 See the License for the specific language governing permissions and
295 limitations under the License.
296  
297 =head1 REPOSITORY INFORMATION
298  
299 $Rev: $
300 $LastChangedBy: $
301 $Id: $
302  
303 =cut
« lib/Google/Ads/AdWords/Utilities/ServiceQueryBuilder.pm ↑ Summary lib/Google/Ads/Common/HTTPTransport.pm »