lib/Google/Ads/Common/OAuth2BaseHandler.pm

filter: Statementstmt 47.4% 47 / 99 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.3% 11 / 21 Podpod 50.0% 3 / 6 totaltotal 36.3% 61 / 168 CC 17 SCAR 27.6
CC
17
Cov
35%
_is_access_token_valid
56.0
__parse_auth_response
20.0
prepare_request
12.0
CRAP 15.7
« lib/Google/Ads/Common/HTTPTransport.pm ↑ Summary lib/Google/Ads/SOAP/Generator/Generator.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::OAuth2BaseHandler;
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use base qw(Google::Ads::Common::AuthHandlerInterface);
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 2 ▶use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 ▶use constant OAUTH2_TOKEN_INFO_URL =>
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 "https://www.googleapis.com/oauth2/v1/tokeninfo";
33  
34 # Class::Std-style attributes. Need to be kept in the same line.
35 # These need to go in the same line for older Perl interpreters to understand.
36 1 my %api_client_of : ATTR(:name<api_client> :default<>);
37 1 my %client_id_of : ATTR(:name<client_id> :default<>);
38 1 my %access_token_of : ATTR(:init_arg<access_token> :default<>);
39 1 my %access_token_expires_of : ATTR(:name<access_token_expires> :default<>);
40 1 my %__user_agent_of : ATTR(:name<__user_agent> :default<>);
41  
42 # Constructor
43 0 ▶sub START {
Subroutine START: not called CC=2 SCAR=10.5
Pod undocumented
44 1 my ($self, $ident) = @_;
45  
46 1 ▶ $__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
47 }
48  
49 # Methods from Google::Ads::Common::AuthHandlerInterface
50 0 ▶sub initialize : CUMULATIVE(BASE FIRST) {
Subroutine initialize: not called CC=3 SCAR=15.0
Pod documented
51 1 my ($self, $api_client, $properties) = @_;
52 1 my $ident = ident $self;
53  
54 1 $api_client_of{$ident} = $api_client;
55 $client_id_of{$ident} = $properties->{oAuth2ClientId}
56 1 ▶ || $client_id_of{$ident};
Condition0%0/3
$properties->{'oAuth2ClientId'} || $client_id_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2ClientId'} || $client_id_of{$ident}
$properties->{'oAuth2ClientId'} $client_id_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2ClientId'}B= $client_id_of{$ident}
ABresult
1X1
011
000
57 $access_token_of{$ident} = $properties->{oAuth2AccessToken}
58 1 ▶ || $access_token_of{$ident};
Condition0%0/3
$properties->{'oAuth2AccessToken'} || $access_token_of{$ident}
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{'oAuth2AccessToken'} || $access_token_of{$ident}
$properties->{'oAuth2AccessToken'} $access_token_of{$ident}
Truth table0%0/3
A || B
A= $properties->{'oAuth2AccessToken'}B= $access_token_of{$ident}
ABresult
1X1
011
000
59 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 0 ▶sub prepare_request {
Subroutine prepare_request: not called CC=3 SCAR=24.8
Pod documented
62 0 my ($self, $endpoint, $http_headers, $envelope) = @_;
63  
64 0 my $access_token = $self->get_access_token();
65  
66 0 ▶ if (!$access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 0 0 0
67 0 my $api_client = $self->get_api_client();
68 0 my $err_msg =
69 "Unable to prepare a request, authorization info is " .
70 "incomplete or invalid.";
71 0 ▶ $api_client->get_die_on_faults() ? die($err_msg) : warn($err_msg);
Branch: 1 branch
BranchTrueFalseTotal
$api_client->get_die_on_faults ? : 0 0 0
72 0 return;
73 }
74  
75 0 push @{$http_headers}, ("Authorization", "Bearer ${access_token}");
76  
77 0 return HTTP::Request->new('POST', $endpoint, $http_headers, $envelope);
78 }
79  
80 0 ▶sub is_auth_enabled {
Subroutine is_auth_enabled: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($self) = @_;
82  
83 0 return $self->get_access_token();
84 }
85  
86 # Custom getters and setters for the access token with logic to auto-refresh.
87 0 ▶sub get_access_token {
Subroutine get_access_token: not called CC=3 SCAR=24.8
Pod undocumented
88 0 my $self = shift;
89 0 my $ident = ident $self;
90  
91 0 ▶ if (!$self->_is_access_token_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_access_token_valid) 0 0 0
92 0 ▶ if (!$self->_refresh_access_token()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_refresh_access_token) 0 0 0
93 0 return undef;
94 }
95  
96 0 return $access_token_of{$ident};
97 }
98  
99 0 return $access_token_of{$ident};
100 }
101  
102 0 ▶sub set_access_token {
Subroutine set_access_token: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my ($self, $token) = @_;
104  
105 0 $access_token_of{ident $self} = $token;
106 0 $access_token_expires_of{ident $self} = undef;
107 }
108  
109 # Internal methods
110  
111 # Checks if:
112 # - the access token is set
113 # - if the token has no expiration set then assumes it was manually set and:
114 # - checks the token info, if it is valid then set its expiration
115 # - checks the token scopes
116 # - checks the token has not expired
117 0 ▶sub _is_access_token_valid {
Subroutine _is_access_token_valid: not called CC=7 SCAR=40.3
118 0 my $self = shift;
119 0 my $ident = ident $self;
120  
121 0 my $access_token = $access_token_of{$ident};
122 0 ▶ if (!$access_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($access_token) 0 0 0
123 0 return 0;
124 }
125  
126 0 ▶ if (!$self->get_access_token_expires()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_access_token_expires) 0 0 0
127 0 my $url =
128 OAUTH2_TOKEN_INFO_URL . "?access_token=" . uri_escape($access_token);
129 0 my $res = $self->get___user_agent()->request(GET $url);
130 0 ▶ if (!$res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
131 0 my $err_msg = $res->decoded_content();
132 0 ▶ $self->get_api_client()->get_die_on_faults()
Branch: 1 branch
BranchTrueFalseTotal
$self->get_api_client->get_die_on_faults ? : 0 0 0
133 ? die($err_msg)
134 : warn($err_msg);
135 0 return 0;
136 }
137 0 my $content_hash = $self->__parse_auth_response($res->decoded_content());
138 0 my %token_scopes = map { $_ => 1 } split(" ", $content_hash->{scope});
139  
140 0 foreach my $required_scope ($self->_scope()) {
141 0 ▶ if (!exists($token_scopes{$required_scope})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $token_scopes{$required_scope}) 0 0 0
142 0 return 0;
143 }
144 }
145 0 $self->set_access_token_expires(time + $content_hash->{expires_in});
146 }
147  
148 0 return time < ($self->get_access_token_expires() - 10);
149 }
150  
151 0 ▶sub __parse_auth_response {
Subroutine __parse_auth_response: not called CC=4 SCAR=30.0
152 0 my ($self, $response_content) = @_;
153  
154 0 my %content_hash = ();
155 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($response_content =~ /([^"]+)"\s*:\s*"([^"]+)|([^"]+)"\s*:\s*([0-9]+)/g) 0 0 0
156 $response_content =~ m/([^"]+)"\s*:\s*"([^"]+)|([^"]+)"\s*:\s*([0-9]+)/g)
157 {
158 0 ▶ if ($1 && $2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 and $2) { } 0 0 0
Condition0%0/3
$1 and $2
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $2
$1 $2
Truth table0%0/3
A and B
A= $1B= $2
ABresult
0X0
100
111
159 0 $content_hash{$1} = $2;
160 } else {
161 0 $content_hash{$3} = $4;
162 }
163 }
164  
165 0 return \%content_hash;
166 }
167  
168 # To be implemented by concrete implementations.
169 0 ▶sub _scope {
Subroutine _scope: not called CC=1 SCAR=6.9
170 0 my $self = shift;
171 0 die "Need to be implemented by subclass";
172 }
173  
174 0 ▶sub _refresh_access_token {
Subroutine _refresh_access_token: not called CC=1 SCAR=6.9
175 0 die "Need to be implemented by subclass";
176 }
177  
178 1 1;
179  
180 =pod
181  
182 =head1 NAME
183  
184 Google::Ads::Common::OAuth2BaseHandler
185  
186 =head1 DESCRIPTION
187  
188 An abstract base implementation that defines part of the logic required to use
189 OAuth2 against Google APIs.
190  
191 It is meant to be specialized and its L<_scope>, L<_refresh_access_token>
192 methods be properly implemented.
193  
194 =head1 ATTRIBUTES
195  
196 Each of these attributes can be set via
197 Google::Ads::Common::OAuth2BaseHandler->new().
198  
199 Alternatively, there is a get_ and set_ method associated with each attribute
200 for retrieving or setting them dynamically.
201  
202 my %api_client_of : ATTR(:name<api_client> :default<>);
203 my %client_id_of : ATTR(:name<client_id> :default<>);
204 my %access_token_of : ATTR(:init_arg<access_token> :default<>);
205 my %access_token_expires_of : ATTR(:name<access_token_expires> :default<>);
206  
207 =head2 api_client
208  
209 A reference to the API client used to send requests.
210  
211 =head2 client_id
212  
213 OAuth2 client id obtained from the Google APIs Console.
214  
215 =head2 access_token
216  
217 Stores an OAuth2 access token after the authorization flow is followed or for
218 you to manually set it in case you had it previously stored.
219 If this is manually set this handler will verify its validity before preparing
220 a request.
221  
222 =head1 METHODS
223  
224 =head2 initialize
225  
226 Initializes the handler with properties such as the client_id and access_token.
227  
228 =head3 Parameters
229  
230 =over
231  
232 =item *
233  
234 A required I<api_client> with a reference to the API client object handling the
235 requests against the API.
236  
237 =item *
238  
239 A hash reference with the following keys:
240 {
241 # Refer to the documentation of the L<client_id> property.
242 oAuth2ClientId => "consumer key",
243 # Refer to the documentation of the L<client_secret> property.
244 oAuth2AccessToken => "secret",
245 # Refer to the documentation of the L<refresh_token> property.
246 }
247  
248 =back
249  
250 =head1 LICENSE AND COPYRIGHT
251  
252 Copyright 2013 Google Inc.
253  
254 Licensed under the Apache License, Version 2.0 (the "License");
255 you may not use this file except in compliance with the License.
256 You may obtain a copy of the License at
257  
258 http://www.apache.org/licenses/LICENSE-2.0
259  
260 Unless required by applicable law or agreed to in writing, software
261 distributed under the License is distributed on an "AS IS" BASIS,
262 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
263 See the License for the specific language governing permissions and
264 limitations under the License.
265  
266 =head1 REPOSITORY INFORMATION
267  
268 $Rev: $
269 $LastChangedBy: $
270 $Id: $
271  
272 =cut
« lib/Google/Ads/Common/HTTPTransport.pm ↑ Summary lib/Google/Ads/SOAP/Generator/Generator.pm »