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