« lib/Google/Ads/AdWords/AuthTokenHandler.pm
↑ Summary
blib/lib/Google/Ads/AdWords/RequestStats.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 | use strict; | ||
| 18 | use version; | ||
| 19 | use base qw(Google::Ads::Common::OAuth2BaseHandler | ||
| 20 | Google::Ads::Common::OAuthApplicationsHandlerInterface); | ||
| 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_BASE_URL => "https://accounts.google.com/o/oauth2"; | ||
| 32 | use constant OAUTH2_TOKEN_INFO_URL => | ||
| 33 | "https://www.googleapis.com/oauth2/v1/tokeninfo"; | ||
| 34 | |||
| 35 | # Class::Std-style attributes. Need to be kept in the same line. | ||
| 36 | # These need to go in the same line for older Perl interpreters to understand. | ||
| 37 | my %client_secret_of : ATTR(:name<client_secret> :default<>); | ||
| 38 | my %access_type_of : ATTR(:name<access_type> :default<offline>); | ||
| 39 | my %approval_prompt_of : ATTR(:name<approval_prompt> :default<auto>); | ||
| 40 | my %refresh_token_of : ATTR(:name<refresh_token> :default<>); | ||
| 41 | my %redirect_uri_of : ATTR(:name<redirect_uri> :default<urn:ietf:wg:oauth:2.0:oob>); | ||
| 42 | |||
| 43 | # Methods from Google::Ads::Common::AuthHandlerInterface | ||
| 44 | sub initialize :CUMULATIVE(BASE FIRST) { | ||
| 45 | my ($self, $api_client, $properties) = @_; | ||
| 46 | my $ident = ident $self; | ||
| 47 | |||
| 48 | $client_secret_of{$ident} = $properties->{oAuth2ClientSecret} || | ||
| 49 | $client_secret_of{$ident}; | ||
| 50 | $access_type_of{$ident} = $properties->{oAuth2AccessType} || | ||
| 51 | $access_type_of{$ident}; | ||
| 52 | $approval_prompt_of{$ident} = $properties->{oAuth2ApprovalPrompt} || | ||
| 53 | $approval_prompt_of{$ident}; | ||
| 54 | $refresh_token_of{$ident} = $properties->{oAuth2RefreshToken} || | ||
| 55 | $refresh_token_of{$ident}; | ||
| 56 | $redirect_uri_of{$ident} = $properties->{oAuth2RedirectUri} || | ||
| 57 | $redirect_uri_of{$ident}; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Methods from Google::Ads::Common::OAuthHandlerInterface | ||
| 61 | sub get_authorization_url { | ||
| 62 | my ($self, $state) = @_; | ||
| 63 | |||
| 64 | $state ||= ""; | ||
| 65 | my ($client_id, $redirect_uri, $access_type, $approval_prompt) = | ||
| 66 | ($self->get_client_id(), $self->get_redirect_uri(), | ||
| 67 | $self->get_access_type(), $self->get_approval_prompt()); | ||
| 68 | |||
| 69 | return OAUTH2_BASE_URL . "/auth?response_type=code" . | ||
| 70 | "&client_id=" . uri_escape($client_id) . | ||
| 71 | "&redirect_uri=" . $redirect_uri . | ||
| 72 | "&scope=" . uri_escape($self->_scope()) . | ||
| 73 | "&access_type=" . $access_type . | ||
| 74 | "&approval_prompt=" . $approval_prompt . | ||
| 75 | "&state=" . uri_escape($state); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub issue_access_token { | ||
| 79 | my ($self, $authorization_code) = @_; | ||
| 80 | |||
| 81 | my $body = "code=" . uri_escape($authorization_code) . | ||
| 82 | "&client_id=" . uri_escape($self->get_client_id()) . | ||
| 83 | "&client_secret=" . uri_escape($self->get_client_secret()) . | ||
| 84 | "&redirect_uri=" . uri_escape($self->get_redirect_uri()) . | ||
| 85 | "&grant_type=authorization_code"; | ||
| 86 | |||
| 87 | push my @headers, "Content-Type" => "application/x-www-form-urlencoded"; | ||
| 88 | my $request = HTTP::Request->new("POST", OAUTH2_BASE_URL . "/token", | ||
| 89 | \@headers, $body); | ||
| 90 | my $res = $self->get___user_agent()->request($request); | ||
| 91 | |||
| 92 | if (!$res->is_success()) { | ||
| 93 | return $res->decoded_content(); | ||
| 94 | } | ||
| 95 | |||
| 96 | my $content_hash = $self->__parse_auth_response($res->decoded_content()); | ||
| 97 | |||
| 98 | $self->set_access_token($content_hash->{access_token}); | ||
| 99 | $self->set_refresh_token($content_hash->{refresh_token}); | ||
| 100 | $self->set_access_token_expires(time + $content_hash->{expires_in}); | ||
| 101 | |||
| 102 | return undef; | ||
| 103 | } | ||
| 104 | |||
| 105 | # Internal methods | ||
| 106 | |||
| 107 | sub _refresh_access_token { | ||
| 108 | my $self = shift; | ||
| 109 | |||
| 110 | if (!($self->get_client_id() && | ||
| 111 | $self->get_client_secret() && | ||
| 112 | $self->get_refresh_token())) { | ||
| 113 | return 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | my $body = "refresh_token=" . uri_escape($self->get_refresh_token()) . | ||
| 117 | "&client_id=" . uri_escape($self->get_client_id()) . | ||
| 118 | "&client_secret=" . uri_escape($self->get_client_secret()) . | ||
| 119 | "&grant_type=refresh_token"; | ||
| 120 | |||
| 121 | push my @headers, "Content-Type" => "application/x-www-form-urlencoded"; | ||
| 122 | my $request = HTTP::Request->new("POST", OAUTH2_BASE_URL . "/token", | ||
| 123 | \@headers, $body); | ||
| 124 | my $res = $self->get___user_agent()->request($request); | ||
| 125 | |||
| 126 | if (!$res->is_success()) { | ||
| 127 | warn($res->decoded_content()); | ||
| 128 | return 0; | ||
| 129 | } | ||
| 130 | |||
| 131 | my $content_hash = $self->__parse_auth_response($res->decoded_content()); | ||
| 132 | |||
| 133 | $self->set_access_token($content_hash->{access_token}); | ||
| 134 | $self->set_access_token_expires(time + $content_hash->{expires_in}); | ||
| 135 | |||
| 136 | return 1; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | Google::Ads::Common::OAuth2ApplicationsHandler | ||
| 146 | |||
| 147 | =head1 DESCRIPTION | ||
| 148 | |||
| 149 | A generic abstract implementation of L<Google::Ads::Common::OAuth2BaseHandler> | ||
| 150 | that supports OAuth2 for Web/Install Applications semantics. | ||
| 151 | |||
| 152 | It is meant to be specialized and its L<_scope> method be properly implemented. | ||
| 153 | |||
| 154 | =head1 ATTRIBUTES | ||
| 155 | |||
| 156 | Each of these attributes can be set via | ||
| 157 | Google::Ads::Common::OAuth2ApplicationsHandler->new(). | ||
| 158 | |||
| 159 | Alternatively, there is a get_ and set_ method associated with each attribute | ||
| 160 | for retrieving or setting them dynamically. | ||
| 161 | |||
| 162 | =head2 api_client | ||
| 163 | |||
| 164 | A reference to the API client used to send requests. | ||
| 165 | |||
| 166 | =head2 client_id | ||
| 167 | |||
| 168 | OAuth2 client id obtained from the Google APIs Console. | ||
| 169 | |||
| 170 | =head2 client_secret | ||
| 171 | |||
| 172 | OAuth2 client secret obtained from the Google APIs Console. | ||
| 173 | |||
| 174 | =head2 access_type | ||
| 175 | |||
| 176 | OAuth2 access type to be requested when following the authorization flow. It | ||
| 177 | defaults to offline but it can be set to online. | ||
| 178 | |||
| 179 | =head2 approval_prompt | ||
| 180 | |||
| 181 | OAuth2 approval_prompt to be used when following the authorization flow. It | ||
| 182 | defaults to auto but it can be set to always - to force the user to always | ||
| 183 | authorize. | ||
| 184 | |||
| 185 | =head2 redirect_uri | ||
| 186 | |||
| 187 | Redirect URI as set for you in the Google APIs console, to which the | ||
| 188 | authorization flow will callback with the verification code. Defaults to | ||
| 189 | urn:ietf:wg:oauth:2.0:oob for the installed applications flow. | ||
| 190 | |||
| 191 | =head2 access_token | ||
| 192 | |||
| 193 | Stores an OAuth2 access token after the authorization flow is followed or for | ||
| 194 | you to manually set it in case you had it previously stored. | ||
| 195 | If this is manually set this handler will verify its validity before preparing | ||
| 196 | a request. | ||
| 197 | |||
| 198 | =head2 refresh_token | ||
| 199 | |||
| 200 | Stores an OAuth2 refresh token in case of an offline L<access_type> is | ||
| 201 | requested. It is automatically used by the handler to request new access tokens | ||
| 202 | i.e. when they expire or found invalid. | ||
| 203 | |||
| 204 | =head1 METHODS | ||
| 205 | |||
| 206 | =head2 initialize | ||
| 207 | |||
| 208 | Initializes the handler with properties such as the client_id and | ||
| 209 | client_secret to use for generating authorization requests. | ||
| 210 | |||
| 211 | =head3 Parameters | ||
| 212 | |||
| 213 | =over | ||
| 214 | |||
| 215 | =item * | ||
| 216 | |||
| 217 | A required I<api_client> with a reference to the API client object handling the | ||
| 218 | requests against the API. | ||
| 219 | |||
| 220 | =item * | ||
| 221 | |||
| 222 | A hash reference with the following keys: | ||
| 223 | { | ||
| 224 | # Refer to the documentation of the L<client_secret> property. | ||
| 225 | oAuth2ClientId => "client-id", | ||
| 226 | # Refer to the documentation of the L<client_secret> property. | ||
| 227 | oAuth2ClientSecret => "client-secret", | ||
| 228 | # Refer to the documentation of the L<access_type> property. | ||
| 229 | oAuth2AccessType => "access-type", | ||
| 230 | # Refer to the documentation of the L<approval_prompt> property. | ||
| 231 | oAuth2ApprovalPrompt => "approval-prompt", | ||
| 232 | # Refer to the documentation of the L<refresh_token> property. | ||
| 233 | oAuth2AccessToken => "access-token", | ||
| 234 | # Refer to the documentation of the L<refresh_token> property. | ||
| 235 | oAuth2RefreshToken => "refresh-token", | ||
| 236 | # Refer to the documentation of the L<redirect_uri> property. | ||
| 237 | oAuth2RedirectUri => "secret", | ||
| 238 | } | ||
| 239 | |||
| 240 | =head2 is_auth_enabled | ||
| 241 | |||
| 242 | Refer to L<Google::Ads::Common::AuthHandlerInterface> documentation of this | ||
| 243 | method. | ||
| 244 | |||
| 245 | =head2 prepare_request | ||
| 246 | |||
| 247 | Refer to L<Google::Ads::Common::AuthHandlerInterface> documentation of this | ||
| 248 | method. | ||
| 249 | |||
| 250 | =head2 get_authorization_url | ||
| 251 | |||
| 252 | Refer to L<Google::Ads::Common::OAuthApplicationsHandlerInterface> documentation | ||
| 253 | of this method. | ||
| 254 | |||
| 255 | =head2 issue_access_token | ||
| 256 | |||
| 257 | Refer to L<Google::Ads::Common::OAuthApplicationsHandlerInterface> documentation | ||
| 258 | of this method. | ||
| 259 | |||
| 260 | =head1 LICENSE AND COPYRIGHT | ||
| 261 | |||
| 262 | Copyright 2013 Google Inc. | ||
| 263 | |||
| 264 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 265 | you may not use this file except in compliance with the License. | ||
| 266 | You may obtain a copy of the License at | ||
| 267 | |||
| 268 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 269 | |||
| 270 | Unless required by applicable law or agreed to in writing, software | ||
| 271 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 272 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 273 | See the License for the specific language governing permissions and | ||
| 274 | limitations under the License. | ||
| 275 | |||
| 276 | =head1 AUTHOR | ||
| 277 | |||
| 278 | David Torres E<lt>david.t at google.comE<gt> | ||
| 279 | |||
| 280 | =head1 REPOSITORY INFORMATION | ||
| 281 | |||
| 282 | $Rev: $ | ||
| 283 | $LastChangedBy: $ | ||
| 284 | $Id: $ | ||
| 285 | |||
| 286 | =cut |