| 1 | # Copyright 2011, 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::AdWords::Serializer; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use utf8; | ||
| 19 | use version; | ||
| 20 | |||
| 21 | use base qw(SOAP::WSDL::Serializer::XSD); | ||
| 22 | |||
| 23 | # The following needs to be on one line because CPAN uses a particularly hacky | ||
| 24 | # eval() to determine module versions. | ||
| 25 | use Google::Ads::Common::Constants; | ||
| 26 | use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION}; | ||
| 27 | use Google::Ads::AdWords::Logging; | ||
| 28 | |||
| 29 | use Class::Std::Fast; | ||
| 30 | use SOAP::WSDL::Factory::Serializer; | ||
| 31 | |||
| 32 | # A list of headers that need to be scrubbed before logging due to sensitive | ||
| 33 | # content. | ||
| 34 | use constant SCRUBBED_HEADERS => qw(authToken password developerToken); | ||
| 35 | |||
| 36 | # Class attributes used to hook this class with the AdWords client. | ||
| 37 | my %client_of :ATTR(:name<client> :default<>); | ||
| 38 | |||
| 39 | # Invoked by SOAP::WSDL to serialize outgoing SOAP requests. | ||
| 40 | sub serialize { | ||
| 41 | my $self = shift; | ||
| 42 | my $client = $self->get_client(); | ||
| 43 | my $request = $self->SUPER::serialize(@_); | ||
| 44 | utf8::is_utf8 $request and utf8::encode $request; | ||
| 45 | |||
| 46 | my $sanitized_request = __scrub_request($request); | ||
| 47 | |||
| 48 | my $auth_handler = $client->_get_auth_handler(); | ||
| 49 | if ($auth_handler && $client->_get_auth_handler()->isa( | ||
| 50 | "Google::Ads::Common::AuthTokenHandler")) { | ||
| 51 | Google::Ads::AdWords::Logging::get_soap_logger->warn( | ||
| 52 | Google::Ads::Common::Constants::CLIENT_LOGIN_DEPRECATION_MESSAGE); | ||
| 53 | } | ||
| 54 | |||
| 55 | Google::Ads::AdWords::Logging::get_soap_logger->info("Outgoing Request:\n" . | ||
| 56 | $sanitized_request); | ||
| 57 | $client->set_last_soap_request($sanitized_request); | ||
| 58 | |||
| 59 | return $request; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Invoked by SOAP::WSDL to serialize outgoing SOAP header, AdWords header | ||
| 63 | # information is injected at this time. | ||
| 64 | sub serialize_header { | ||
| 65 | my $self = shift; | ||
| 66 | my $client = $self->get_client(); | ||
| 67 | my $client_header = $client->_get_header(); | ||
| 68 | my $adwords_header = $_[1]; | ||
| 69 | |||
| 70 | # Set request header parameters based on configured header parameters | ||
| 71 | # through the client class. | ||
| 72 | if ($adwords_header->can("set_clientEmail")) { | ||
| 73 | $adwords_header->set_clientEmail($client_header->{clientEmail}); | ||
| 74 | } elsif ($client_header->{clientEmail} && | ||
| 75 | $client_header->{clientEmail} ne "") { | ||
| 76 | if ($client->get_die_on_faults()) { | ||
| 77 | die("Version " . $client->get_version() . | ||
| 78 | " has no support for identifying clients by email."); | ||
| 79 | } else { | ||
| 80 | warn("Version " . $client->get_version() . | ||
| 81 | " has no support for identifying clients by email."); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | $adwords_header->set_clientCustomerId($client_header->{clientCustomerId}); | ||
| 85 | $adwords_header->set_developerToken($client_header->{developerToken}); | ||
| 86 | $adwords_header->set_userAgent($client_header->{userAgent}); | ||
| 87 | $adwords_header->set_validateOnly($client_header->{validateOnly}); | ||
| 88 | if ($adwords_header->can("set_partialFailure")) { | ||
| 89 | $adwords_header->set_partialFailure($client_header->{partialFailure}); | ||
| 90 | } | ||
| 91 | |||
| 92 | # Serialize the header. | ||
| 93 | my $header = $self->SUPER::serialize_header(@_); | ||
| 94 | |||
| 95 | # Hack the header inner elements to correctly include the namespaces. | ||
| 96 | my $xmlns = "https://adwords.google.com/api/adwords/cm/" . | ||
| 97 | $client->get_version; | ||
| 98 | $header =~ s/<authToken>/<authToken xmlns="$xmlns">/; | ||
| 99 | $header =~ s/<clientEmail>/<clientEmail xmlns="$xmlns">/; | ||
| 100 | $header =~ s/<developerToken>/<developerToken xmlns="$xmlns">/; | ||
| 101 | $header =~ s/<userAgent>/<userAgent xmlns="$xmlns">/; | ||
| 102 | $header =~ s/<validateOnly>/<validateOnly xmlns="$xmlns">/; | ||
| 103 | $header =~ s/<clientCustomerId>/<clientCustomerId xmlns="$xmlns">/; | ||
| 104 | $header =~ s/<partialFailure>/<partialFailure xmlns="$xmlns">/; | ||
| 105 | |||
| 106 | return $header; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Private method to redact sensitive information from requests before logging. | ||
| 110 | sub __scrub_request { | ||
| 111 | my ($request) = @_; | ||
| 112 | my $scrubbed_request = $request; | ||
| 113 | foreach my $header (SCRUBBED_HEADERS) { | ||
| 114 | $scrubbed_request =~ | ||
| 115 | s!<$header([^>]*)>.+?</$header>!<$header$1>REDACTED</$header>!; | ||
| 116 | } | ||
| 117 | return $scrubbed_request; | ||
| 118 | } | ||
| 119 | |||
| 120 | return 1; | ||
| 121 | |||
| 122 | =pod | ||
| 123 | |||
| 124 | =head1 NAME | ||
| 125 | |||
| 126 | Google::Ads::AdWords::Serializer | ||
| 127 | |||
| 128 | =head1 DESCRIPTION | ||
| 129 | |||
| 130 | Google::Ads::AdWords::Deserializer extends the | ||
| 131 | L<SOAP::WSDL::Serializer::XSD|SOAP::WSDL::Serializer::XSD> module. Above the | ||
| 132 | normal functionality of | ||
| 133 | L<SOAP::WSDL::Serializer::XSD|SOAP::WSDL::Serializer::XSD>, this module | ||
| 134 | implements hooks into | ||
| 135 | L<Google::Ads::AdWords::Client|Google::Ads::AdWords::Client> to inject AdWords | ||
| 136 | API Header parameters as well as hooks into | ||
| 137 | L<Google::Ads::AdWords::Client|Google::Ads::AdWords::Logging> to log SOAP | ||
| 138 | request XML. | ||
| 139 | |||
| 140 | =head1 METHODS | ||
| 141 | |||
| 142 | =head2 serialize | ||
| 143 | |||
| 144 | A method automatically invoked by SOAP::WSDL when an outgoing request needs to | ||
| 145 | be serialized into SOAP XML. SOAP XML request is logged by this method. | ||
| 146 | |||
| 147 | =head3 Parameters | ||
| 148 | |||
| 149 | The SOAP request. | ||
| 150 | |||
| 151 | =head3 Returns | ||
| 152 | |||
| 153 | The SOAP XML string representing the request. | ||
| 154 | |||
| 155 | =head2 serialize_header | ||
| 156 | |||
| 157 | A method automatically invoked by SOAP::WSDL when an outgoing request header | ||
| 158 | needs to be serialized into SOAP XML. At this time API headers are injected in | ||
| 159 | the message. | ||
| 160 | |||
| 161 | =head3 Parameters | ||
| 162 | |||
| 163 | The SOAP request. | ||
| 164 | |||
| 165 | =head3 Returns | ||
| 166 | |||
| 167 | The SOAP XML string representing the request. | ||
| 168 | |||
| 169 | =head2 __scrub_request (Private) | ||
| 170 | |||
| 171 | Scrubs sensitive password and auth token information from the request before | ||
| 172 | it's logged. | ||
| 173 | |||
| 174 | =head3 Parameters | ||
| 175 | |||
| 176 | The serialized SOAP request XML string. | ||
| 177 | |||
| 178 | =head3 Returns | ||
| 179 | |||
| 180 | A redacted version of the string. | ||
| 181 | |||
| 182 | =head1 LICENSE AND COPYRIGHT | ||
| 183 | |||
| 184 | Copyright 2011 Google Inc. | ||
| 185 | |||
| 186 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 187 | you may not use this file except in compliance with the License. | ||
| 188 | You may obtain a copy of the License at | ||
| 189 | |||
| 190 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 191 | |||
| 192 | Unless required by applicable law or agreed to in writing, software | ||
| 193 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 194 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 195 | See the License for the specific language governing permissions and | ||
| 196 | limitations under the License. | ||
| 197 | |||
| 198 | =head1 AUTHOR | ||
| 199 | |||
| 200 | David Torres E<lt>api.davidtorres at gmail.comE<gt> | ||
| 201 | |||
| 202 | =head1 REPOSITORY INFORMATION | ||
| 203 | |||
| 204 | $Rev: $ | ||
| 205 | $LastChangedBy: $ | ||
| 206 | $Id: $ | ||
| 207 | |||
| 208 | =cut |