lib/Google/Ads/Common/AuthTokenHandler.pm

filter: Statementstmt 42.1% 54 / 128 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 57.6% 15 / 26 Podpod 50.0% 4 / 8 totaltotal 31.2% 73 / 234 CC 24 SCAR 38.2
CC
24
Cov
30%
issue_new_token
132.0
__get_auth_token
72.0
initialize
30.0
CRAP 45.5
« blib/lib/Google/Ads/SOAP/Generator/TypemapVisitor.pm ↑ Summary blib/lib/Google/Ads/Common/HTTPTransport.pm »
1 # Copyright 2012, 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::AuthTokenHandler;
16  
17 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use base qw(Google::Ads::Common::AuthHandlerInterface);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # The following needs to be on one line because CPAN uses a particularly hacky
22 # eval() to determine module versions.
23 9 ▶use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 9 ▶use Google::Ads::Common::AuthError;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use Google::Ads::Common::CaptchaRequiredError;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 9 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # These constants should be fairly, well, constant. But if anything does change
34 # with the ClientLogin mechanism, this should be updated.
35 9 ▶use constant ACCOUNT_TYPE => "GOOGLE";
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use constant DEFAULT_SERVER => "https://www.google.com";
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 ▶use constant AUTH_URL => "%s/accounts/ClientLogin";
Subroutine BEGIN: called CC=1 SCAR=0.0
38 9 ▶use constant CAPTCHA_URL_PATH => "/accounts/";
Subroutine BEGIN: called CC=1 SCAR=0.0
39 9 ▶use constant AUTH_TOKEN_LIFETIME => 23;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # Class::Std-style attributes. Need to be kept in the same line.
42 9 my %api_client_of : ATTR(:name<api_client> :default<>);
43 9 my %email_of : ATTR(:init_arg<email> :get<email> :default<>);
44 9 my %password_of : ATTR(:init_arg<password> :get<password> :default<>);
45 9 my %auth_server_of : ATTR(:name<auth_server> :default<>);
46 9 my %auth_token_of : ATTR(:init_arg<auth_token> :get<auth_token> :default<>);
47 9 my %issued_in_of : ATTR(:name<issued_in> :default<>);
48 9 my %__user_agent_of : ATTR(:name<__user_agent> :default<>);
49  
50 0 ▶sub START {
Subroutine START: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my ($self, $ident) = @_;
52  
53 0 ▶ $__user_agent_of{$ident} ||= LWP::UserAgent->new();
Condition0%0/3
$__user_agent_of{$ident} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$__user_agent_of{$ident} ||= "LWP::UserAgent"->new
$__user_agent_of{$ident} "LWP::UserAgent"->new
Truth table0%0/3
A ||= B
A= $__user_agent_of{$ident}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
54 0 $auth_server_of{$ident} = DEFAULT_SERVER;
55 }
56  
57 # Methods from Google::Ads::Common::AuthHandlerInterface
58 0 ▶sub initialize {
Subroutine initialize: not called CC=5 SCAR=34.0
Pod documented
59 0 my ($self, $api_client, $properties) = @_;
60 0 my $ident = ident $self;
61  
62 0 $api_client_of{$ident} = $api_client;
63 0 ▶ $email_of{$ident} = $properties->{email} || $email_of{$ident};
Condition0%0/3
$properties->{'email'} || $email_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'email'} || $email_of{$ident}
$properties->{'email'} $email_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'email'}B= $email_of{$ident}
ABresult
1X1
011
000
64 0 ▶ $password_of{$ident} = $properties->{password} || $password_of{$ident};
Condition0%0/3
$properties->{'password'} || $password_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'password'} || $password_of{$ident}
$properties->{'password'} $password_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'password'}B= $password_of{$ident}
ABresult
1X1
011
000
65 0 ▶ $auth_token_of{$ident} = $properties->{authToken} || $auth_token_of{$ident};
Condition0%0/3
$properties->{'authToken'} || $auth_token_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'authToken'} || $auth_token_of{$ident}
$properties->{'authToken'} $auth_token_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'authToken'}B= $auth_token_of{$ident}
ABresult
1X1
011
000
66 $auth_server_of{$ident} = $properties->{authServer} ||
67 0 ▶ $auth_server_of{$ident};
Condition0%0/3
$properties->{'authServer'} || $auth_server_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'authServer'} || $auth_server_of{$ident}
$properties->{'authServer'} $auth_server_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'authServer'}B= $auth_server_of{$ident}
ABresult
1X1
011
000
68 }
69  
70 0 ▶sub is_auth_enabled {
Subroutine is_auth_enabled: not called CC=1 SCAR=6.9
Pod documented
71 0 my ($self) = @_;
72  
73 0 return $self->__get_auth_token();
74 }
75  
76 # Class own methods.
77  
78 # Forces a refresh of the auth token.
79 0 ▶sub refresh_auth_token {
Subroutine refresh_auth_token: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self) = @_;
81  
82 0 return $self->issue_new_token();
83 }
84  
85 # Custom setters with logic to invalidate other dependent fields.
86 0 ▶sub set_email {
Subroutine set_email: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self, $email) = @_;
88  
89 0 $email_of{ident $self} = $email;
90 0 $auth_token_of{ident $self} = undef;
91 0 $issued_in_of{ident $self} = undef;
92 }
93  
94 0 ▶sub set_password {
Subroutine set_password: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my ($self, $password) = @_;
96  
97 0 $password_of{ident $self} = $password;
98 0 $auth_token_of{ident $self} = undef;
99 0 $issued_in_of{ident $self} = undef;
100 }
101  
102 0 ▶sub set_auth_token {
Subroutine set_auth_token: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my ($self, $token) = @_;
104  
105 0 $auth_token_of{ident $self} = $token;
106 0 $issued_in_of{ident $self} = undef;
107 }
108  
109 # Uses the ClientLogin web service to request a new auth token.
110 0 ▶sub issue_new_token {
Subroutine issue_new_token: not called CC=11 SCAR=48.8
Pod documented
111 0 my ($self, $captcha_token, $captcha_code) = @_;
112  
113 0 warnings::warnif("deprecated",
114 Google::Ads::Common::Constants::CLIENT_LOGIN_DEPRECATION_MESSAGE);
115  
116 0 my $error;
117 0 ▶ my $service = uri_escape($self->_service())
Branch: 1 branch
BranchTrueFalseTotal
my $service = uri_escape($self->_service) or $error = "Google::Ads::Common::AuthError"->new({"code", "", "content", "", "message", "Required 'service' not available, handler not properly initiliazed?."}) 0 0 0
118 or $error = Google::Ads::Common::AuthError->new({
119 code => "",
120 content => "",
121 message => "Required 'service' not available, handler " .
122 "not properly initiliazed?."
123 });
124 0 ▶ my $email = uri_escape($self->get_email())
Branch: 1 branch
BranchTrueFalseTotal
my $email = uri_escape($self->get_email) or $error = "Google::Ads::Common::AuthError"->new({"code", "", "content", "", "message", "Required 'email' not available, handler not properly initiliazed?."}) 0 0 0
125 or $error = Google::Ads::Common::AuthError->new({
126 code => "",
127 content => "",
128 message => "Required 'email' not available, handler " .
129 "not properly initiliazed?."
130 });
131 0 ▶ my $password = uri_escape($self->get_password())
Branch: 1 branch
BranchTrueFalseTotal
my $password = uri_escape($self->get_password) or $error = "Google::Ads::Common::AuthError"->new({"code", "", "content", "", "message", "Required 'password' not available, handler not properly initiliazed?."}) 0 0 0
132 or $error = Google::Ads::Common::AuthError->new({
133 code => "",
134 content => "",
135 message => "Required 'password' not available, handler " .
136 "not properly initiliazed?."
137 });
138  
139 0 ▶ if (!$error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
140 0 my $server = $self->get_auth_server();
141 0 my $user_agent = $self->get___user_agent();
142  
143 # The ClientLogin interface is documented at
144 # https://developers.google.com/accounts/docs/AuthForInstalledApps
145 0 my $data = sprintf("accountType=%s&Email=%s&Passwd=%s&service=%s",
146 ACCOUNT_TYPE, $email, $password, $service);
147  
148 0 ▶ if ($captcha_token && $captcha_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($captcha_token and $captcha_code) 0 0 0
Condition0%0/3
$captcha_token and $captcha_code
!ll&&!rl&&r
000
MC/DC0%0/2
$captcha_token and $captcha_code
$captcha_token $captcha_code
Truth table0%0/3
A and B
A= $captcha_tokenB= $captcha_code
ABresult
0X0
100
111
149 0 $data .= sprintf("&logintoken=%s&logincaptcha=%s", $captcha_token,
150 $captcha_code);
151 }
152  
153 # my $userAgent = LWP::UserAgent->new();
154 0 $user_agent->env_proxy();
155 0 $user_agent->agent(sprintf("%s: %s", __PACKAGE__, $0));
156  
157 0 my $request = HTTP::Request->new(POST => sprintf(AUTH_URL, $server));
158 0 $request->content_type("application/x-www-form-urlencoded");
159 0 $request->content($data);
160  
161 0 my $response = $user_agent->request($request);
162 0 my $content = $response->content();
163  
164 0 ▶ if ($response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 0 0
165  
166 # Sample response body:
167 # SID=DQAAAGgA...7Zg8CTN
168 # LSID=DQAAAGsA...lk8BBbG
169 # Auth=DQAAAGgA...dk3fA5N
170 # We only care about the Auth token.
171 0 ▶ if ($content =~ /Auth=(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /Auth=(.+)$/) { } 0 0 0
172 0 $self->set_auth_token($1);
173 0 $self->set_issued_in(time);
174 0 return;
175 } else {
176 0 $error = "Invalid successful response: ${content} from auth server";
177 }
178 }
179  
180 0 my %content_hash = __content_to_hash($content);
181  
182 0 ▶ if (exists $content_hash{Error} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $content_hash{'Error'} and $content_hash{'Error'} eq 'CaptchaRequired') { } 0 0 0
Condition0%0/3
exists $content_hash{"Error"} and $content_hash{"Error"} eq "CaptchaRequired"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $content_hash{"Error"} and $content_hash{"Error"} eq "CaptchaRequired"
exists $content_hash{"Error"} $content_hash{"Error"} eq "CaptchaRequired"
Truth table0%0/3
A and B
A= exists $content_hash{"Error"}B= $content_hash{"Error"} eq "CaptchaRequired"
ABresult
0X0
100
111
183 $content_hash{Error} eq "CaptchaRequired") {
184 0 my $captcha_image = $server . CAPTCHA_URL_PATH . $content_hash{CaptchaUrl};
185  
186 $error = Google::Ads::Common::CaptchaRequiredError->new({
187 token => $content_hash{CaptchaToken},
188 image => $captcha_image,
189 url => $content_hash{Url},
190 0 message => $response->message(),
191 code => $response->code(),
192 content => $response->content()
193 });
194 } else {
195 0 $error = Google::Ads::Common::AuthError->new({
196 message => $response->message(),
197 code => $response->code(),
198 content => $response->content()
199 });
200 }
201 }
202  
203 0 return $error;
204 }
205  
206 # Internal methods
207  
208 # Returns or refresh an auth token, based in its lifetime.
209 0 ▶sub __get_auth_token {
Subroutine __get_auth_token: not called CC=8 SCAR=42.8
210 0 my ($self) = @_;
211  
212 0 ▶ if ($self->get_auth_token()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_auth_token) { } 0 0 0
elsif ($self->get_email) { } 0 0 0
213 0 ▶ if ($self->get_issued_in() &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_issued_in and $self->get_issued_in + 82800000 < time and $self->get_email) 0 0 0
Condition0%0/6
$self->get_issued_in and $self->get_issued_in + 82800000 < time and $self->get_email
!ll&&!rl&&r
000
$self->get_issued_in and $self->get_issued_in + 82800000 < time
!ll&&!rl&&r
000
MC/DC0%0/3
$self->get_issued_in and $self->get_issued_in + 82800000 < time and $self->get_email
$self->get_issued_in $self->get_issued_in + 82800000 < time $self->get_email
Truth table0%0/4
A and B and C
A= $self->get_issued_inB= $self->get_issued_in + 82800000 < timeC= $self->get_email
ABCresult
0XX0
10X0
1100
1111
214 ($self->get_issued_in() + (1000 * 60 * 60 * AUTH_TOKEN_LIFETIME)) <
215 time && $self->get_email()) {
216 0 $self->issue_new_token();
217 }
218 } elsif ($self->get_email()) {
219 0 my $error = $self->issue_new_token();
220 0 ▶ if (defined($error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
221 0 ▶ if ($self->get_api_client()->get_die_on_faults()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_api_client->get_die_on_faults) 0 0 0
222 0 die "An error as occurred trying to retrieve the authorization token. " .
223 "Error: " . $error;
224 }
225 0 warn "An error as occurred trying to retrieve the authorization token. " .
226 "Error: " . $error;
227 }
228 }
229  
230 0 return $self->get_auth_token();
231 }
232  
233 0 ▶sub __content_to_hash {
Subroutine __content_to_hash: not called CC=2 SCAR=17.9
234 0 my $content = shift;
235 0 ▶ return map {$_ =~ /^(.+?)=(.+)$/; if (defined $1) { $1 => $2 } else
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
236 0 { 0 => 0 }} split(/\n/, $content);
237 }
238  
239 # Returns the ClientLogin service name to use when requesting an new Auth Token.
240 # This is specific to ClientLogin and the product.
241 0 ▶sub _service {
Subroutine _service: not called CC=1 SCAR=6.9
242 0 my ($self) = @_;
243 0 die "Needs to be implemented by subclass";
244 }
245  
246 9 return 1;
247  
248 =pod
249  
250 =head1 NAME
251  
252 Google::Ads::Common::AuthTokenHandler
253  
254 =head1 DESCRIPTION
255  
256 Google::Ads::Common::AuthTokenHandler implements most of the methods required
257 to request AuthTokens using the ClientLogin protocol, see
258 L<https://developers.google.com/accounts/docs/AuthForInstalledApps> for more
259 information about the protocol.
260  
261 =head1 ATTRIBUTES
262  
263 Each of these attributes can be set via the constructor as a hash.
264 Alternatively, there is a get_ and set_ method associated with each attribute
265 for retrieving or setting them dynamically.
266  
267 =head2 email
268  
269 The email of the account to authorization against.
270  
271 =head2 password
272  
273 The password to use for authorization.
274  
275 =head2 auth_server
276  
277 Endpoint of the authorization server.
278  
279 =head2 auth_token
280  
281 Auth token either manually set or generated.
282  
283 =head2 issued_in
284  
285 L<time> in which the token was issued if it was requested by this class.
286  
287 =head1 METHODS
288  
289 =head2 initialize
290  
291 Initializes the handler with properties such as the email and password to use
292 for generating AuthTokens.
293  
294 =head3 Parameters
295  
296 =over
297  
298 =item *
299  
300 A required I<api_client> with a reference to the API client object handling the
301 requests against the API.
302  
303 =item *
304  
305 A hash reference with the following keys:
306 {
307 # The service name to access.
308 service => "adwords",
309 # The email address of a Google Account.
310 email => "user@domain.com",
311 # The password for the Google Account.
312 password => "password",
313 }
314  
315 =head2 is_auth_enabled
316  
317 =head3 Returns
318  
319 True, if a valid AuthToken is available or can be generated to use within API
320 requests.
321  
322 =head2 refresh_auth_token
323  
324 Forces refreshing the auth token using the credentials set in the handler.
325  
326 =head2 issue_new_token
327  
328 Calls the authorization server to generate a new AuthToken. Sets the generated
329 AuthToken in the L<auth_token> property as well as the L<issued_in> property.
330 A captcha token and code can be passed to this method to handle captcha challenges
331  
332 =head3 Parameters
333  
334 =over
335  
336 =item *
337  
338 An optional captcha token, obtained from a captcha challenge error. See
339 L<Google::Ads::Common::CaptchaRequiredError>.
340  
341 =item *
342  
343 An optional captcha code, obtained from the user by looking at the captcha
344 image.
345  
346 =back
347  
348 =head3 Returns
349  
350 Returns an error of type L<Google::Ads::Common::CaptchaRequiredError> or
351 L<Google::Ads::Common::AuthError> if an error occurred while generating the
352 token.
353  
354 =head1 LICENSE AND COPYRIGHT
355  
356 Copyright 2012 Google Inc.
357  
358 Licensed under the Apache License, Version 2.0 (the "License");
359 you may not use this file except in compliance with the License.
360 You may obtain a copy of the License at
361  
362 http://www.apache.org/licenses/LICENSE-2.0
363  
364 Unless required by applicable law or agreed to in writing, software
365 distributed under the License is distributed on an "AS IS" BASIS,
366 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
367 See the License for the specific language governing permissions and
368 limitations under the License.
369  
370 =head1 AUTHOR
371  
372 David Torres E<lt>david.t at google.comE<gt>
373  
374 =head1 REPOSITORY INFORMATION
375  
376 $Rev: $
377 $LastChangedBy: $
378 $Id: $
379  
380 =cut
« blib/lib/Google/Ads/SOAP/Generator/TypemapVisitor.pm ↑ Summary blib/lib/Google/Ads/Common/HTTPTransport.pm »