« blib/lib/Google/Ads/Common/HTTPTransport.pm
↑ Summary
blib/lib/Google/Ads/AdWords/Utilities/BatchJobHandler.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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use version; | ||
| 20 | use base qw(Google::Ads::Common::AuthHandlerInterface); | ||
| 21 | |||
| 22 | # The following needs to be on one line because CPAN uses a particularly hacky | ||
| 23 | # eval() to determine module versions. | ||
| 24 | use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION}; | ||
| 25 | |||
| 26 | use Class::Std::Fast; | ||
| 27 | use HTTP::Request::Common; | ||
| 28 | use LWP::UserAgent; | ||
| 29 | use URI::Escape; | ||
| 30 | |||
| 31 | use constant OAUTH2_TOKEN_INFO_URL => | ||
| 32 | "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 | my %api_client_of : ATTR(:name<api_client> :default<>); | ||
| 37 | my %client_id_of : ATTR(:name<client_id> :default<>); | ||
| 38 | my %access_token_of : ATTR(:init_arg<access_token> :default<>); | ||
| 39 | my %access_token_expires_of : ATTR(:name<access_token_expires> :default<>); | ||
| 40 | my %__user_agent_of : ATTR(:name<__user_agent> :default<>); | ||
| 41 | |||
| 42 | # Constructor | ||
| 43 | sub START { | ||
| 44 | my ($self, $ident) = @_; | ||
| 45 | |||
| 46 | $__user_agent_of{$ident} ||= LWP::UserAgent->new(); | ||
| 47 | } | ||
| 48 | |||
| 49 | # Methods from Google::Ads::Common::AuthHandlerInterface | ||
| 50 | sub initialize : CUMULATIVE(BASE FIRST) { | ||
| 51 | my ($self, $api_client, $properties) = @_; | ||
| 52 | my $ident = ident $self; | ||
| 53 | |||
| 54 | $api_client_of{$ident} = $api_client; | ||
| 55 | $client_id_of{$ident} = $properties->{oAuth2ClientId} | ||
| 56 | || $client_id_of{$ident}; | ||
| 57 | $access_token_of{$ident} = $properties->{oAuth2AccessToken} | ||
| 58 | || $access_token_of{$ident}; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub prepare_request { | ||
| 62 | my ($self, $endpoint, $http_headers, $envelope) = @_; | ||
| 63 | |||
| 64 | my $access_token = $self->get_access_token(); | ||
| 65 | |||
| 66 | if (!$access_token) { | ||
| 67 | my $api_client = $self->get_api_client(); | ||
| 68 | my $err_msg = | ||
| 69 | "Unable to prepare a request, authorization info is " . | ||
| 70 | "incomplete or invalid."; | ||
| 71 | $api_client->get_die_on_faults() ? die($err_msg) : warn($err_msg); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | push @{$http_headers}, ("Authorization", "Bearer ${access_token}"); | ||
| 76 | |||
| 77 | return HTTP::Request->new('POST', $endpoint, $http_headers, $envelope); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub is_auth_enabled { | ||
| 81 | my ($self) = @_; | ||
| 82 | |||
| 83 | return $self->get_access_token(); | ||
| 84 | } | ||
| 85 | |||
| 86 | # Custom getters and setters for the access token with logic to auto-refresh. | ||
| 87 | sub get_access_token { | ||
| 88 | my $self = shift; | ||
| 89 | my $ident = ident $self; | ||
| 90 | |||
| 91 | if (!$self->_is_access_token_valid()) { | ||
| 92 | if (!$self->_refresh_access_token()) { | ||
| 93 | return undef; | ||
| 94 | } | ||
| 95 | |||
| 96 | return $access_token_of{$ident}; | ||
| 97 | } | ||
| 98 | |||
| 99 | return $access_token_of{$ident}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub set_access_token { | ||
| 103 | my ($self, $token) = @_; | ||
| 104 | |||
| 105 | $access_token_of{ident $self} = $token; | ||
| 106 | $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 | sub _is_access_token_valid { | ||
| 118 | my $self = shift; | ||
| 119 | my $ident = ident $self; | ||
| 120 | |||
| 121 | my $access_token = $access_token_of{$ident}; | ||
| 122 | if (!$access_token) { | ||
| 123 | return 0; | ||
| 124 | } | ||
| 125 | |||
| 126 | if (!$self->get_access_token_expires()) { | ||
| 127 | my $url = | ||
| 128 | OAUTH2_TOKEN_INFO_URL . "?access_token=" . uri_escape($access_token); | ||
| 129 | my $res = $self->get___user_agent()->request(GET $url); | ||
| 130 | if (!$res->is_success()) { | ||
| 131 | my $err_msg = $res->decoded_content(); | ||
| 132 | $self->get_api_client()->get_die_on_faults() | ||
| 133 | ? die($err_msg) | ||
| 134 | : warn($err_msg); | ||
| 135 | return 0; | ||
| 136 | } | ||
| 137 | my $content_hash = $self->__parse_auth_response($res->decoded_content()); | ||
| 138 | my %token_scopes = map { $_ => 1 } split(" ", $content_hash->{scope}); | ||
| 139 | |||
| 140 | foreach my $required_scope ($self->_scope()) { | ||
| 141 | if (!exists($token_scopes{$required_scope})) { | ||
| 142 | return 0; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | $self->set_access_token_expires(time + $content_hash->{expires_in}); | ||
| 146 | } | ||
| 147 | |||
| 148 | return time < ($self->get_access_token_expires() - 10); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub __parse_auth_response { | ||
| 152 | my ($self, $response_content) = @_; | ||
| 153 | |||
| 154 | my %content_hash = (); | ||
| 155 | while ( | ||
| 156 | $response_content =~ m/([^"]+)"\s*:\s*"([^"]+)|([^"]+)"\s*:\s*([0-9]+)/g) | ||
| 157 | { | ||
| 158 | if ($1 && $2) { | ||
| 159 | $content_hash{$1} = $2; | ||
| 160 | } else { | ||
| 161 | $content_hash{$3} = $4; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | return \%content_hash; | ||
| 166 | } | ||
| 167 | |||
| 168 | # To be implemented by concrete implementations. | ||
| 169 | sub _scope { | ||
| 170 | my $self = shift; | ||
| 171 | die "Need to be implemented by subclass"; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _refresh_access_token { | ||
| 175 | die "Need to be implemented by subclass"; | ||
| 176 | } | ||
| 177 | |||
| 178 | 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 |