google-ads-common 0.12.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,6 @@
1
+ 0.12.1:
2
+ - Adding support for JSON keyfiles for service accounts.
3
+
1
4
  0.12.0:
2
5
  - Require Ruby version 2.0 or higher from now on. It is recommended to use
3
6
  Ruby version 2.1 or higher.
@@ -62,7 +65,7 @@
62
65
 
63
66
  0.9.0:
64
67
  - Switched to Signet OAuth implementation.
65
- - Removed deprecated OAuth1.0a scheme, please use OAuth2.0 instead.
68
+ - Removed deprecated OAuth1.0a scheme, please use OAuth2 instead.
66
69
  - Support for JWT authorization (OAuth2 service accounts).
67
70
 
68
71
  0.8.2:
@@ -78,7 +81,7 @@
78
81
  - Compatibility with Savon -> 1.0.0 (not backward-compatible).
79
82
  - Now require HTTPI ~> 1.0.0.
80
83
  - HttpClient is no longer explicitely required.
81
- - Support for OAuth2.0 authorization method.
84
+ - Support for OAuth2 authorization method.
82
85
 
83
86
  0.7.2:
84
87
  - Compatibility with savon-0.9.10 (not backward-compatible).
@@ -15,7 +15,7 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
- # This module manages OAuth2.0 authentication.
18
+ # This module manages OAuth2 authentication.
19
19
 
20
20
  require 'time'
21
21
 
@@ -28,7 +28,7 @@ require 'ads_common/errors'
28
28
  module AdsCommon
29
29
  module Auth
30
30
 
31
- # Credentials class to handle OAuth2.0 authentication.
31
+ # Credentials class to handle OAuth2 authentication.
32
32
  class OAuth2Handler < AdsCommon::Auth::BaseHandler
33
33
  OAUTH2_CONFIG = {
34
34
  :authorization_uri =>
@@ -67,7 +67,7 @@ module AdsCommon
67
67
  raise error
68
68
  end
69
69
 
70
- # Generates auth string for OAuth2.0 method of authentication.
70
+ # Generates auth string for OAuth2 method of authentication.
71
71
  #
72
72
  # Args:
73
73
  # - credentials: credentials set for authorization
@@ -142,7 +142,7 @@ module AdsCommon
142
142
  end
143
143
 
144
144
  # Auxiliary method to generate an authentication token for logging via
145
- # the OAuth2.0 API.
145
+ # the OAuth2 API.
146
146
  #
147
147
  # Args:
148
148
  # - credentials: a hash with the credentials for the account being
@@ -15,10 +15,11 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
  #
18
- # This module manages OAuth2.0 service account authentication.
18
+ # This module manages OAuth2 service account authentication.
19
19
 
20
20
  require 'faraday'
21
21
  require 'signet/oauth_2/client'
22
+ require 'json'
22
23
 
23
24
  require 'ads_common/auth/base_handler'
24
25
  require 'ads_common/errors'
@@ -26,7 +27,7 @@ require 'ads_common/errors'
26
27
  module AdsCommon
27
28
  module Auth
28
29
 
29
- # Credentials class to handle OAuth2.0 authentication.
30
+ # Credentials class to handle OAuth2 authentication.
30
31
  class OAuth2ServiceAccountHandler < AdsCommon::Auth::BaseHandler
31
32
 
32
33
  OAUTH2_CONFIG = {
@@ -62,7 +63,7 @@ module AdsCommon
62
63
  raise error
63
64
  end
64
65
 
65
- # Generates auth string for OAuth2.0 service account method of
66
+ # Generates auth string for OAuth2 service account method of
66
67
  # authentication.
67
68
  #
68
69
  # Args:
@@ -113,24 +114,40 @@ module AdsCommon
113
114
  raise AdsCommon::Errors::AuthError, 'No credentials supplied.'
114
115
  end
115
116
 
116
- if credentials[:oauth2_issuer].nil?
117
+ if credentials[:oauth2_key].nil? && credentials[:oauth2_keyfile].nil?
117
118
  raise AdsCommon::Errors::AuthError,
118
- 'Issuer is not included in the credentials.'
119
+ 'Either key or key file must be provided for OAuth2 service account.'
119
120
  end
120
121
 
121
- if credentials[:oauth2_secret].nil?
122
+ if credentials[:oauth2_key] && credentials[:oauth2_keyfile]
122
123
  raise AdsCommon::Errors::AuthError,
123
- 'Key secret is not included in the credentials.'
124
+ 'Both service account key and key file provided, only one can be used.'
124
125
  end
125
126
 
126
- if credentials[:oauth2_key].nil? && credentials[:oauth2_keyfile].nil?
127
+ p12 = true
128
+ if credentials[:oauth2_keyfile]
129
+ file_name = credentials[:oauth2_keyfile]
130
+ if File.file?(file_name)
131
+ unless file_name.end_with?('.p12') || file_name.end_with?('.json')
132
+ raise AdsCommon::Errors::AuthError,
133
+ "Key file '%s' must be either a .p12 or .json file." %
134
+ file_name
135
+ end
136
+ p12 = false if file_name.end_with?('.json')
137
+ else
138
+ raise AdsCommon::Errors::AuthError,
139
+ "Key file '%s' does not exist or not a file." % file_name
140
+ end
141
+ end
142
+
143
+ if credentials[:oauth2_issuer].nil? && p12
127
144
  raise AdsCommon::Errors::AuthError,
128
- 'Either key or key file must be provided for OAuth2 service account.'
145
+ 'Issuer is not included in the credentials.'
129
146
  end
130
147
 
131
- if credentials[:oauth2_key] && credentials[:oauth2_keyfile]
148
+ if credentials[:oauth2_secret].nil? && p12
132
149
  raise AdsCommon::Errors::AuthError,
133
- 'Both service account key and key file provided, only one can be used.'
150
+ 'Key secret is not included in the credentials.'
134
151
  end
135
152
 
136
153
  if credentials[:oauth2_key] &&
@@ -138,17 +155,10 @@ module AdsCommon
138
155
  raise AdsCommon::Errors::AuthError,
139
156
  'OAuth2 service account key provided must be of type OpenSSL::PKey::RSA.'
140
157
  end
141
-
142
- if credentials[:oauth2_keyfile] &&
143
- !File.file?(credentials[:oauth2_keyfile])
144
- raise AdsCommon::Errors::AuthError,
145
- "Key file '%s' does not exist or not a file." %
146
- credentials[:oauth2_keyfile]
147
- end
148
158
  end
149
159
 
150
160
  # Auxiliary method to generate an authentication token for logging via
151
- # the OAuth2.0 API.
161
+ # the OAuth2 API.
152
162
  #
153
163
  # Args:
154
164
  # - credentials: a hash with the credentials for the account being
@@ -185,9 +195,18 @@ module AdsCommon
185
195
  def load_oauth2_service_account_credentials(credentials)
186
196
  return credentials unless credentials.include?(:oauth2_keyfile)
187
197
  key_file = File.read(credentials[:oauth2_keyfile])
188
- key_secret = credentials[:oauth2_secret]
189
- key = OpenSSL::PKCS12.new(key_file, key_secret).key
198
+ key = nil
199
+ issuer = nil
200
+ if credentials[:oauth2_keyfile].end_with?('.p12')
201
+ key_secret = credentials[:oauth2_secret]
202
+ key = OpenSSL::PKCS12.new(key_file, key_secret).key
203
+ else
204
+ key_file_hash = JSON.parse(key_file, :symbolize_names => true)
205
+ key = OpenSSL::PKey::RSA.new(key_file_hash[:private_key])
206
+ issuer = key_file_hash[:client_email]
207
+ end
190
208
  result = credentials.merge({:oauth2_key => key})
209
+ result[:oauth2_issuer] = issuer unless issuer.nil?
191
210
  result.delete(:oauth2_keyfile)
192
211
  return result
193
212
  end
@@ -19,10 +19,10 @@
19
19
 
20
20
  require 'ads_common/errors'
21
21
 
22
- MIN_RUBY_VERSION = Gem::Version.new(2.0)
23
- MIN_RUBY_RECOMMENDED_VERSION = Gem::Version.new(2.1)
22
+ MIN_RUBY_VERSION = Gem::Version.new('2.0')
23
+ MIN_RUBY_RECOMMENDED_VERSION = Gem::Version.new('2.1')
24
24
 
25
- if Gem::Version.new(RUBY_VERSION) < MIN_RUBY_VERSION
25
+ if Gem::Version.new(RUBY_VERSION.dup()) < MIN_RUBY_VERSION
26
26
  raise AdsCommon::Errors::VersionError,
27
27
  'Unsupported Ruby version %s. Upgrade to version %s or later' %
28
28
  [RUBY_VERSION, MIN_RUBY_RECOMMENDED_VERSION]
@@ -37,7 +37,7 @@ module AdsCommon
37
37
  end
38
38
  end
39
39
 
40
- # Raised when OAuth2.0 access token is required.
40
+ # Raised when OAuth2 access token is required.
41
41
  class OAuth2VerificationRequired < AuthError
42
42
  attr_reader :oauth_url
43
43
  def initialize(oauth_url)
@@ -19,6 +19,6 @@
19
19
 
20
20
  module AdsCommon
21
21
  module ApiConfig
22
- CLIENT_LIB_VERSION = '0.12.0'
22
+ CLIENT_LIB_VERSION = '0.12.1'
23
23
  end
24
24
  end
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+ # Encoding: utf-8
3
+ #
4
+ # Copyright:: Copyright 2016, Google Inc. All Rights Reserved.
5
+ #
6
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
+ # implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ # Test OAuth2 service account authentication.
20
+
21
+ require 'tempfile'
22
+
23
+ require 'ads_common/auth/oauth2_service_account_handler'
24
+ require 'ads_common/config'
25
+
26
+ module AdsCommon
27
+ module Auth
28
+ class OAuth2ServiceAccountHandler
29
+ public :validate_credentials
30
+ end
31
+ end
32
+ end
33
+
34
+ class TestOAuthServiceAccount < Test::Unit::TestCase
35
+ def test_file_extension_check()
36
+ assert_nothing_raised do
37
+ validate_credentials(["test", ".json"])
38
+ end
39
+ assert_nothing_raised do
40
+ validate_credentials(["test", ".p12"])
41
+ end
42
+ assert_raise do
43
+ validate_credentials(["test", ".other"])
44
+ end
45
+ end
46
+
47
+ def validate_credentials(filename)
48
+ file = Tempfile.new(filename)
49
+ credentials = {
50
+ :method => 'OAUTH2_SERVICE_ACCOUNT',
51
+ :oauth2_keyfile => file.path,
52
+ :oauth2_issuer => 'issuer',
53
+ :oauth2_secret => 'secret'
54
+ }
55
+ handler = AdsCommon::Auth::OAuth2ServiceAccountHandler.new(
56
+ AdsCommon::Config.new(), 'https://www.googleapis.com/auth/adwords')
57
+ handler.validate_credentials(credentials)
58
+ file.close
59
+ file.unlink
60
+ end
61
+ end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-ads-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Sergio Gomes
@@ -10,76 +11,86 @@ authors:
10
11
  autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2016-04-07 00:00:00.000000000 Z
14
+ date: 2016-05-17 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: google-ads-savon
17
18
  requirement: !ruby/object:Gem::Requirement
19
+ none: false
18
20
  requirements:
19
- - - "~>"
21
+ - - ~>
20
22
  - !ruby/object:Gem::Version
21
23
  version: 1.0.1
22
24
  type: :runtime
23
25
  prerelease: false
24
26
  version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
25
28
  requirements:
26
- - - "~>"
29
+ - - ~>
27
30
  - !ruby/object:Gem::Version
28
31
  version: 1.0.1
29
32
  - !ruby/object:Gem::Dependency
30
33
  name: httpi
31
34
  requirement: !ruby/object:Gem::Requirement
35
+ none: false
32
36
  requirements:
33
- - - "~>"
37
+ - - ~>
34
38
  - !ruby/object:Gem::Version
35
39
  version: '2.3'
36
40
  type: :runtime
37
41
  prerelease: false
38
42
  version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
39
44
  requirements:
40
- - - "~>"
45
+ - - ~>
41
46
  - !ruby/object:Gem::Version
42
47
  version: '2.3'
43
48
  - !ruby/object:Gem::Dependency
44
49
  name: httpclient
45
50
  requirement: !ruby/object:Gem::Requirement
51
+ none: false
46
52
  requirements:
47
- - - "~>"
53
+ - - ~>
48
54
  - !ruby/object:Gem::Version
49
55
  version: '2.7'
50
56
  type: :runtime
51
57
  prerelease: false
52
58
  version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
53
60
  requirements:
54
- - - "~>"
61
+ - - ~>
55
62
  - !ruby/object:Gem::Version
56
63
  version: '2.7'
57
64
  - !ruby/object:Gem::Dependency
58
65
  name: signet
59
66
  requirement: !ruby/object:Gem::Requirement
67
+ none: false
60
68
  requirements:
61
- - - "~>"
69
+ - - ~>
62
70
  - !ruby/object:Gem::Version
63
71
  version: 0.7.0
64
72
  type: :runtime
65
73
  prerelease: false
66
74
  version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
67
76
  requirements:
68
- - - "~>"
77
+ - - ~>
69
78
  - !ruby/object:Gem::Version
70
79
  version: 0.7.0
71
80
  - !ruby/object:Gem::Dependency
72
81
  name: rake
73
82
  requirement: !ruby/object:Gem::Requirement
83
+ none: false
74
84
  requirements:
75
- - - ">="
85
+ - - ! '>='
76
86
  - !ruby/object:Gem::Version
77
87
  version: 10.4.2
78
88
  type: :development
79
89
  prerelease: false
80
90
  version_requirements: !ruby/object:Gem::Requirement
91
+ none: false
81
92
  requirements:
82
- - - ">="
93
+ - - ! '>='
83
94
  - !ruby/object:Gem::Version
84
95
  version: 10.4.2
85
96
  description: Essential utilities shared by all Ads Ruby client libraries
@@ -89,72 +100,75 @@ executables: []
89
100
  extensions: []
90
101
  extra_rdoc_files: []
91
102
  files:
92
- - COPYING
93
- - ChangeLog
94
- - README.md
95
103
  - lib/ads_common/api.rb
96
- - lib/ads_common/api_config.rb
104
+ - lib/ads_common/version.rb
105
+ - lib/ads_common/errors.rb
106
+ - lib/ads_common/env_check.rb
97
107
  - lib/ads_common/auth/base_handler.rb
98
- - lib/ads_common/auth/oauth2_handler.rb
99
108
  - lib/ads_common/auth/oauth2_service_account_handler.rb
100
- - lib/ads_common/build/savon_abstract_generator.rb
101
- - lib/ads_common/build/savon_generator.rb
109
+ - lib/ads_common/auth/oauth2_handler.rb
110
+ - lib/ads_common/credential_handler.rb
111
+ - lib/ads_common/api_config.rb
112
+ - lib/ads_common/utils.rb
113
+ - lib/ads_common/build/savon_service_generator.rb
102
114
  - lib/ads_common/build/savon_registry.rb
103
115
  - lib/ads_common/build/savon_registry_generator.rb
104
- - lib/ads_common/build/savon_service_generator.rb
116
+ - lib/ads_common/build/savon_abstract_generator.rb
117
+ - lib/ads_common/build/savon_generator.rb
105
118
  - lib/ads_common/config.rb
106
- - lib/ads_common/credential_handler.rb
107
- - lib/ads_common/env_check.rb
108
- - lib/ads_common/errors.rb
109
- - lib/ads_common/http.rb
110
119
  - lib/ads_common/parameters_validator.rb
111
- - lib/ads_common/results_extractor.rb
112
120
  - lib/ads_common/savon_headers/base_header_handler.rb
113
121
  - lib/ads_common/savon_headers/oauth_header_handler.rb
122
+ - lib/ads_common/results_extractor.rb
123
+ - lib/ads_common/http.rb
114
124
  - lib/ads_common/savon_service.rb
115
- - lib/ads_common/utils.rb
116
- - lib/ads_common/version.rb
117
- - test/coverage.rb
125
+ - test/test_env.rb
118
126
  - test/suite_unittests.rb
127
+ - test/coverage.rb
128
+ - test/test_results_extractor.rb
119
129
  - test/test_config.rb
120
- - test/test_config.yml
130
+ - test/test_utils.rb
121
131
  - test/test_credential_handler.rb
122
- - test/test_env.rb
123
132
  - test/test_oauth2_handler.rb
124
- - test/test_parameters_validator.rb
125
- - test/test_results_extractor.rb
126
133
  - test/test_savon_service.rb
127
- - test/test_utils.rb
134
+ - test/test_config.yml
135
+ - test/test_parameters_validator.rb
136
+ - test/test_oauth2_service_account_handler.rb
137
+ - COPYING
138
+ - README.md
139
+ - ChangeLog
128
140
  homepage: https://github.com/googleads/google-api-ads-ruby
129
141
  licenses:
130
142
  - Apache-2.0
131
- metadata: {}
132
143
  post_install_message:
133
144
  rdoc_options: []
134
145
  require_paths:
135
146
  - lib
136
147
  required_ruby_version: !ruby/object:Gem::Requirement
148
+ none: false
137
149
  requirements:
138
- - - ">="
150
+ - - ! '>='
139
151
  - !ruby/object:Gem::Version
140
152
  version: '0'
141
153
  required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
142
155
  requirements:
143
- - - ">="
156
+ - - ! '>='
144
157
  - !ruby/object:Gem::Version
145
158
  version: 1.3.6
146
159
  requirements: []
147
160
  rubyforge_project: google-ads-common
148
- rubygems_version: 2.4.8
161
+ rubygems_version: 1.8.23
149
162
  signing_key:
150
- specification_version: 4
163
+ specification_version: 3
151
164
  summary: Common code for Google Ads APIs
152
165
  test_files:
153
- - test/test_savon_service.rb
166
+ - test/test_env.rb
167
+ - test/test_results_extractor.rb
154
168
  - test/test_config.rb
155
- - test/test_credential_handler.rb
156
169
  - test/test_utils.rb
157
- - test/test_parameters_validator.rb
170
+ - test/test_credential_handler.rb
158
171
  - test/test_oauth2_handler.rb
159
- - test/test_env.rb
160
- - test/test_results_extractor.rb
172
+ - test/test_savon_service.rb
173
+ - test/test_parameters_validator.rb
174
+ - test/test_oauth2_service_account_handler.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 92e8a7a26a88631f6109cecd9748a25907193e88
4
- data.tar.gz: 115bedb52bd5a55003ffa3ed598ff4a72554df5f
5
- SHA512:
6
- metadata.gz: 0b78cdce163e1910a6e8152b3f7b323421d82a3e6b2c2c9200ead0cf43282cdd91ce3e9ba1b3535417399d4dde394bdcf0ce6d9aa4fae9cc453ad98ecacbba26
7
- data.tar.gz: 5b104c82f29bd4f2031103b1e8897f2e408a5f7a20b03066e677c6787a5232a9d6c82d6c320b77e5481abe0b5a41a1331175f1b0b11d0dda1031fb30707ac75b