googleauth 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25babf8085ee83a898e246364f5f177b31cdaebd1a7721650a4601ca9a568335
4
- data.tar.gz: a62751620c543425fc7a8d83b07bb2a77b0d5e94c7dc5a790808115c17173f07
3
+ metadata.gz: e97bb2d3af353d706c6b608e1743fb3f571e62530201ffa3bebcae2c7e6460bb
4
+ data.tar.gz: 3c06cad2ea956d09a37783516292c61f754706366a2b8bf03ef7928769762070
5
5
  SHA512:
6
- metadata.gz: 6309bd3fe40fecbb4a19e4cb0196400a4a8fe75dabe540c878eada5e454a1b1491cbbdad537523689901be4f51f6a8270440fc7f1535660c730298b764e77aa0
7
- data.tar.gz: 52c8eb2d015b1442ff2c13bd13b86c40931d50f1eeedbc6fcaa2c278bfc2ed3a29199685f57c3540f110d90f693a32cee92e3ee1305da9507c4a307cc2f15555
6
+ metadata.gz: 68ad7978f7d5abcc14fbd4ca668ba24f69e4bef2c184427b1a70a8c57e2d742b636d5c0b8da10f4d62f8762adb0075d20c1eb75a76302f9f9b6e61cd1b989685
7
+ data.tar.gz: 25333a0a26181c8f8f0464642eccfe0340ed591a4c11f9fe3ed4b85179524c37100431f686a16a75f2985ade010e3b488aeb60a6c1a06e0109db46efa9e1d620
@@ -5,4 +5,12 @@ REM * Merges run all non-acceptance tests for every library, and acceptance tes
5
5
  REM * Nightlies run all acceptance tests for every library.
6
6
  REM Currently only runs tests on 2.5.1
7
7
 
8
- "C:\Program Files\Git\bin\bash.exe" github/google-auth-library-ruby/.kokoro/windows.sh
8
+ SET url="https://raw.githubusercontent.com/googleapis/google-cloud-ruby/master/.kokoro/build.bat"
9
+
10
+ SET "download=powershell -C Invoke-WebRequest -Uri %url% -OutFile master-build.bat"
11
+
12
+ SET EXIT_STATUS=1
13
+
14
+ %download% && master-build.bat && SET EXIT_STATUS=0
15
+
16
+ EXIT %EXIT_STATUS%
@@ -1,3 +1,19 @@
1
1
  # Format: //devtools/kokoro/config/proto/build.proto
2
2
 
3
- build_file: "google-auth-library-ruby/.kokoro/build.bat"
3
+ build_file: "google-auth-library-ruby/.kokoro/trampoline.bat"
4
+
5
+ # Configure the docker image for kokoro-trampoline.
6
+ env_vars: {
7
+ key: "TRAMPOLINE_IMAGE"
8
+ value: "gcr.io/cloud-devrel-kokoro-resources/yoshi-ruby/windows"
9
+ }
10
+
11
+ env_vars: {
12
+ key: "TRAMPOLINE_BUILD_FILE"
13
+ value: "github/google-auth-library-ruby/.kokoro/build.bat"
14
+ }
15
+
16
+ env_vars: {
17
+ key: "REPO_DIR"
18
+ value: "google-auth-library-ruby"
19
+ }
@@ -1,3 +1,19 @@
1
1
  # Format: //devtools/kokoro/config/proto/build.proto
2
2
 
3
- build_file: "google-auth-library-ruby/.kokoro/build.bat"
3
+ build_file: "google-auth-library-ruby/.kokoro/trampoline.bat"
4
+
5
+ # Configure the docker image for kokoro-trampoline.
6
+ env_vars: {
7
+ key: "TRAMPOLINE_IMAGE"
8
+ value: "gcr.io/cloud-devrel-kokoro-resources/yoshi-ruby/windows"
9
+ }
10
+
11
+ env_vars: {
12
+ key: "TRAMPOLINE_BUILD_FILE"
13
+ value: "github/google-auth-library-ruby/.kokoro/build.bat"
14
+ }
15
+
16
+ env_vars: {
17
+ key: "REPO_DIR"
18
+ value: "google-auth-library-ruby"
19
+ }
@@ -0,0 +1,10 @@
1
+
2
+ SET url="https://raw.githubusercontent.com/googleapis/google-cloud-ruby/master/.kokoro/trampoline.bat"
3
+
4
+ SET "download=powershell -C Invoke-WebRequest -Uri %url% -OutFile master-trampoline.bat"
5
+
6
+ SET EXIT_STATUS=1
7
+
8
+ %download% && master-trampoline.bat && SET EXIT_STATUS=0
9
+
10
+ EXIT %EXIT_STATUS%
@@ -1,11 +1,42 @@
1
- inherit_gem:
2
- google-style: google-style.yml
3
-
4
1
  AllCops:
5
2
  Exclude:
6
3
  - "spec/**/*"
7
4
  - "Rakefile"
8
- Metrics/ClassLength:
9
- Max: 110
5
+
6
+ Metrics/AbcSize:
7
+ Max: 25
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - "googleauth.gemspec"
11
+ Metrics/CyclomaticComplexity:
12
+ Max: 8
13
+ Metrics/PerceivedComplexity:
14
+ Max: 8
15
+ Metrics/LineLength:
16
+ Max: 120
17
+ Metrics/MethodLength:
18
+ Max: 21
10
19
  Metrics/ModuleLength:
11
- Max: 110
20
+ Max: 150
21
+ Metrics/ClassLength:
22
+ Enabled: false
23
+ Layout/IndentHeredoc:
24
+ Enabled: false
25
+ Style/FormatString:
26
+ Enabled: false
27
+ Style/GuardClause:
28
+ Enabled: false
29
+ Style/PercentLiteralDelimiters: # Contradicting rule
30
+ Enabled: false
31
+ Style/StringLiterals:
32
+ EnforcedStyle: double_quotes
33
+ Style/SymbolArray: # Undefined syntax in Ruby 1.9.3
34
+ Enabled: false
35
+ Style/MethodDefParentheses:
36
+ Enabled: false
37
+ Style/WordArray:
38
+ Enabled: false
39
+ Style/TrivialAccessors:
40
+ Enabled: false
41
+ Style/RescueModifier:
42
+ Enabled: false
@@ -1,3 +1,12 @@
1
+ ### 0.9.0 / 2019-08-05
2
+
3
+ * Restore compatibility with Ruby 2.0. This is the last release that will work on end-of-lifed versions of Ruby. The 0.10 release will require Ruby 2.4 or later.
4
+ * Update Credentials to use methods for values that are intended to be changed by users, replacing constants.
5
+ * Add retry on error for fetch_access_token
6
+ * Allow specifying custom state key-values
7
+ * Add verbosity none to gcloud command
8
+ * Make arity of WebUserAuthorizer#get_credentials compatible with the base class
9
+
1
10
  ### 0.8.1 / 2019-03-27
2
11
 
3
12
  * Silence unnecessary gcloud warning
data/Gemfile CHANGED
@@ -8,12 +8,12 @@ group :development do
8
8
  gem "coveralls", "~> 0.7"
9
9
  gem "fakefs", "~> 0.6"
10
10
  gem "fakeredis", "~> 0.5"
11
- gem "google-style", "~> 0.2"
12
11
  gem "logging", "~> 2.0"
13
12
  gem "rack-test", "~> 0.6"
14
13
  gem "rake", "~> 10.0"
15
14
  gem "redis", "~> 3.2"
16
15
  gem "rspec", "~> 3.0"
16
+ gem "rubocop", ">= 0.41", "< 0.50"
17
17
  gem "simplecov", "~> 0.9"
18
18
  gem "sinatra"
19
19
  gem "webmock", "~> 1.21"
data/README.md CHANGED
@@ -8,7 +8,6 @@
8
8
  </dl>
9
9
 
10
10
  [![Gem Version](https://badge.fury.io/rb/googleauth.svg)](http://badge.fury.io/rb/googleauth)
11
- [![Build Status](https://secure.travis-ci.org/google/google-auth-library-ruby.svg)](http://travis-ci.org/google/google-auth-library-ruby)
12
11
  [![Coverage Status](https://coveralls.io/repos/google/google-auth-library-ruby/badge.svg)](https://coveralls.io/r/google/google-auth-library-ruby)
13
12
 
14
13
  ## Description
@@ -184,7 +183,7 @@ Custom storage implementations can also be used. See
184
183
 
185
184
  ## Supported Ruby Versions
186
185
 
187
- This library is currently supported on Ruby 1.9+.
186
+ This library is currently supported on Ruby 2.3+.
188
187
 
189
188
  However, Ruby 2.4 or later is strongly recommended, as earlier releases have
190
189
  reached or are nearing end-of-life. After March 31, 2019, Google will provide
@@ -1,7 +1,7 @@
1
1
  # -*- ruby -*-
2
2
  # encoding: utf-8
3
3
 
4
- $LOAD_PATH.push File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
5
5
  require "googleauth/version"
6
6
 
7
7
  Gem::Specification.new do |gem|
@@ -34,13 +34,11 @@ module Google
34
34
  # Module Auth provides classes that provide Google-specific authorization
35
35
  # used to access Google APIs.
36
36
  module Auth
37
- NOT_FOUND_ERROR = <<~ERROR_MESSAGE.freeze
38
- Could not load the default credentials. Browse to
39
- https://developers.google.com/accounts/docs/application-default-credentials
40
- for more information
41
- ERROR_MESSAGE
42
-
43
- module_function
37
+ NOT_FOUND_ERROR = <<-ERROR_MESSAGE.freeze
38
+ Could not load the default credentials. Browse to
39
+ https://developers.google.com/accounts/docs/application-default-credentials
40
+ for more information
41
+ ERROR_MESSAGE
44
42
 
45
43
  # Obtains the default credentials implementation to use in this
46
44
  # environment.
@@ -77,5 +75,7 @@ module Google
77
75
  end
78
76
  GCECredentials.new
79
77
  end
78
+
79
+ module_function :get_application_default
80
80
  end
81
81
  end
@@ -35,16 +35,16 @@ module Google
35
35
  # Module Auth provides classes that provide Google-specific authorization
36
36
  # used to access Google APIs.
37
37
  module Auth
38
- NO_METADATA_SERVER_ERROR = <<~ERROR.freeze
39
- Error code 404 trying to get security access token
40
- from Compute Engine metadata for the default service account. This
41
- may be because the virtual machine instance does not have permission
42
- scopes specified.
43
- ERROR
44
- UNEXPECTED_ERROR_SUFFIX = <<~ERROR.freeze
45
- trying to get security access token from Compute Engine metadata for
46
- the default service account
47
- ERROR
38
+ NO_METADATA_SERVER_ERROR = <<-ERROR.freeze
39
+ Error code 404 trying to get security access token
40
+ from Compute Engine metadata for the default service account. This
41
+ may be because the virtual machine instance does not have permission
42
+ scopes specified.
43
+ ERROR
44
+ UNEXPECTED_ERROR_SUFFIX = <<-ERROR.freeze
45
+ trying to get security access token from Compute Engine metadata for
46
+ the default service account
47
+ ERROR
48
48
 
49
49
  # Extends Signet::OAuth2::Client so that the auth token is obtained from
50
50
  # the GCE metadata server.
@@ -35,26 +35,206 @@ require "googleauth/credentials_loader"
35
35
 
36
36
  module Google
37
37
  module Auth
38
- # This class is intended to be inherited by API-specific classes
39
- # which overrides the SCOPE constant.
38
+ ##
39
+ # Credentials is responsible for representing the authentication when connecting to an API. This
40
+ # class is also intended to be inherited by API-specific classes.
40
41
  class Credentials
42
+ ##
43
+ # The default token credential URI to be used when none is provided during initialization.
41
44
  TOKEN_CREDENTIAL_URI = "https://oauth2.googleapis.com/token".freeze
45
+
46
+ ##
47
+ # The default target audience ID to be used when none is provided during initialization.
42
48
  AUDIENCE = "https://oauth2.googleapis.com/token".freeze
43
- SCOPE = [].freeze
44
- PATH_ENV_VARS = [].freeze
45
- JSON_ENV_VARS = [].freeze
46
- DEFAULT_PATHS = [].freeze
47
49
 
50
+ ##
51
+ # The default token credential URI to be used when none is provided during initialization.
52
+ # The URI is the authorization server's HTTP endpoint capable of issuing tokens and
53
+ # refreshing expired tokens.
54
+ #
55
+ # @return [String]
56
+ #
57
+ def self.token_credential_uri
58
+ return @token_credential_uri unless @token_credential_uri.nil?
59
+
60
+ const_get :TOKEN_CREDENTIAL_URI if const_defined? :TOKEN_CREDENTIAL_URI
61
+ end
62
+
63
+ ##
64
+ # Set the default token credential URI to be used when none is provided during initialization.
65
+ #
66
+ # @param [String] new_token_credential_uri
67
+ # @return [String]
68
+ #
69
+ def self.token_credential_uri= new_token_credential_uri
70
+ @token_credential_uri = new_token_credential_uri
71
+ end
72
+
73
+ ##
74
+ # The default target audience ID to be used when none is provided during initialization.
75
+ # Used only by the assertion grant type.
76
+ #
77
+ # @return [String]
78
+ #
79
+ def self.audience
80
+ return @audience unless @audience.nil?
81
+
82
+ const_get :AUDIENCE if const_defined? :AUDIENCE
83
+ end
84
+
85
+ ##
86
+ # Sets the default target audience ID to be used when none is provided during initialization.
87
+ #
88
+ # @param [String] new_audience
89
+ # @return [String]
90
+ #
91
+ def self.audience= new_audience
92
+ @audience = new_audience
93
+ end
94
+
95
+ ##
96
+ # The default scope to be used when none is provided during initialization.
97
+ # A scope is an access range defined by the authorization server.
98
+ # The scope can be a single value or a list of values.
99
+ #
100
+ # @return [String, Array<String>]
101
+ #
102
+ def self.scope
103
+ return @scope unless @scope.nil?
104
+
105
+ tmp_scope = []
106
+ # Pull in values is the SCOPE constant exists.
107
+ tmp_scope << const_get(:SCOPE) if const_defined? :SCOPE
108
+ tmp_scope.flatten.uniq
109
+ end
110
+
111
+ ##
112
+ # Sets the default scope to be used when none is provided during initialization.
113
+ #
114
+ # @param [String, Array<String>] new_scope
115
+ # @return [String, Array<String>]
116
+ #
117
+ def self.scope= new_scope
118
+ new_scope = Array new_scope unless new_scope.nil?
119
+ @scope = new_scope
120
+ end
121
+
122
+ ##
123
+ # The environment variables to search for credentials. Values can either be a file path to the
124
+ # credentials file, or the JSON contents of the credentials file.
125
+ #
126
+ # @return [Array<String>]
127
+ #
128
+ def self.env_vars
129
+ return @env_vars unless @env_vars.nil?
130
+
131
+ # Pull values when PATH_ENV_VARS or JSON_ENV_VARS constants exists.
132
+ tmp_env_vars = []
133
+ tmp_env_vars << const_get(:PATH_ENV_VARS) if const_defined? :PATH_ENV_VARS
134
+ tmp_env_vars << const_get(:JSON_ENV_VARS) if const_defined? :JSON_ENV_VARS
135
+ tmp_env_vars.flatten.uniq
136
+ end
137
+
138
+ ##
139
+ # Sets the environment variables to search for credentials.
140
+ #
141
+ # @param [Array<String>] new_env_vars
142
+ # @return [Array<String>]
143
+ #
144
+ def self.env_vars= new_env_vars
145
+ new_env_vars = Array new_env_vars unless new_env_vars.nil?
146
+ @env_vars = new_env_vars
147
+ end
148
+
149
+ ##
150
+ # The file paths to search for credentials files.
151
+ #
152
+ # @return [Array<String>]
153
+ #
154
+ def self.paths
155
+ return @paths unless @paths.nil?
156
+
157
+ tmp_paths = []
158
+ # Pull in values is the DEFAULT_PATHS constant exists.
159
+ tmp_paths << const_get(:DEFAULT_PATHS) if const_defined? :DEFAULT_PATHS
160
+ tmp_paths.flatten.uniq
161
+ end
162
+
163
+ ##
164
+ # Set the file paths to search for credentials files.
165
+ #
166
+ # @param [Array<String>] new_paths
167
+ # @return [Array<String>]
168
+ #
169
+ def self.paths= new_paths
170
+ new_paths = Array new_paths unless new_paths.nil?
171
+ @paths = new_paths
172
+ end
173
+
174
+ ##
175
+ # The Signet::OAuth2::Client object the Credentials instance is using.
176
+ #
177
+ # @return [Signet::OAuth2::Client]
178
+ #
48
179
  attr_accessor :client
49
- attr_reader :project_id
50
180
 
51
- # Delegate client methods to the client object.
181
+ ##
182
+ # Identifier for the project the client is authenticating with.
183
+ #
184
+ # @return [String]
185
+ #
186
+ attr_reader :project_id
187
+
188
+ # @private Delegate client methods to the client object.
52
189
  extend Forwardable
190
+
191
+ ##
192
+ # @!attribute [r] token_credential_uri
193
+ # @return [String] The token credential URI. The URI is the authorization server's HTTP
194
+ # endpoint capable of issuing tokens and refreshing expired tokens.
195
+ #
196
+ # @!attribute [r] audience
197
+ # @return [String] The target audience ID when issuing assertions. Used only by the
198
+ # assertion grant type.
199
+ #
200
+ # @!attribute [r] scope
201
+ # @return [String, Array<String>] The scope for this client. A scope is an access range
202
+ # defined by the authorization server. The scope can be a single value or a list of values.
203
+ #
204
+ # @!attribute [r] issuer
205
+ # @return [String] The issuer ID associated with this client.
206
+ #
207
+ # @!attribute [r] signing_key
208
+ # @return [String, OpenSSL::PKey] The signing key associated with this client.
209
+ #
210
+ # @!attribute [r] updater_proc
211
+ # @return [Proc] Returns a reference to the {Signet::OAuth2::Client#apply} method,
212
+ # suitable for passing as a closure.
213
+ #
53
214
  def_delegators :@client,
54
215
  :token_credential_uri, :audience,
55
216
  :scope, :issuer, :signing_key, :updater_proc
56
217
 
57
218
  # rubocop:disable Metrics/AbcSize
219
+
220
+ ##
221
+ # Creates a new Credentials instance with the provided auth credentials, and with the default
222
+ # values configured on the class.
223
+ #
224
+ # @param [String, Hash, Signet::OAuth2::Client] keyfile
225
+ # The keyfile can be provided as one of the following:
226
+ #
227
+ # * The path to a JSON keyfile (as a +String+)
228
+ # * The contents of a JSON keyfile (as a +Hash+)
229
+ # * A +Signet::OAuth2::Client+ object
230
+ # @param [Hash] options
231
+ # The options for configuring the credentials instance. The following is supported:
232
+ #
233
+ # * +:scope+ - the scope for the client
234
+ # * +"project_id"+ (and optionally +"project"+) - the project identifier for the client
235
+ # * +:connection_builder+ - the connection builder to use for the client
236
+ # * +:default_connection+ - the default connection to use for the client
237
+ #
58
238
  def initialize keyfile, options = {}
59
239
  scope = options[:scope]
60
240
  verify_keyfile_provided! keyfile
@@ -80,18 +260,32 @@ module Google
80
260
  end
81
261
  # rubocop:enable Metrics/AbcSize
82
262
 
83
- # Returns the default credentials checking, in this order, the path env
84
- # evironment variables, json environment variables, default paths. If the
85
- # previously stated locations do not contain keyfile information,
86
- # this method defaults to use the application default.
263
+ ##
264
+ # Creates a new Credentials instance with auth credentials acquired by searching the
265
+ # environment variables and paths configured on the class, and with the default values
266
+ # configured on the class.
267
+ #
268
+ # The auth credentials are searched for in the following order:
269
+ #
270
+ # 1. configured environment variables (see {Credentials.env_vars})
271
+ # 2. configured default file paths (see {Credentials.paths})
272
+ # 3. application default (see {Google::Auth.get_application_default})
273
+ #
274
+ # @param [Hash] options
275
+ # The options for configuring the credentials instance. The following is supported:
276
+ #
277
+ # * +:scope+ - the scope for the client
278
+ # * +"project_id"+ (and optionally +"project"+) - the project identifier for the client
279
+ # * +:connection_builder+ - the connection builder to use for the client
280
+ # * +:default_connection+ - the default connection to use for the client
281
+ #
282
+ # @return [Credentials]
283
+ #
87
284
  def self.default options = {}
88
- # First try to find keyfile file from environment variables.
89
- client = from_path_vars options
90
-
91
- # Second try to find keyfile json from environment variables.
92
- client ||= from_json_vars options
285
+ # First try to find keyfile file or json from environment variables.
286
+ client = from_env_vars options
93
287
 
94
- # Third try to find keyfile file from known file paths.
288
+ # Second try to find keyfile file from known file paths.
95
289
  client ||= from_default_paths options
96
290
 
97
291
  # Finally get instantiated client from Google::Auth
@@ -99,33 +293,22 @@ module Google
99
293
  client
100
294
  end
101
295
 
102
- def self.from_path_vars options
103
- self::PATH_ENV_VARS
104
- .map { |v| ENV[v] }
105
- .compact
106
- .select { |p| ::File.file? p }
107
- .each do |file|
108
- return new file, options
109
- end
110
- nil
111
- end
112
-
113
- def self.from_json_vars options
114
- json = lambda do |v|
115
- unless ENV[v].nil?
116
- begin
117
- JSON.parse ENV[v]
118
- rescue StandardError
119
- nil
120
- end
121
- end
296
+ ##
297
+ # @private Lookup Credentials from environment variables.
298
+ def self.from_env_vars options
299
+ env_vars.each do |env_var|
300
+ str = ENV[env_var]
301
+ next if str.nil?
302
+ return new str, options if ::File.file? str
303
+ return new ::JSON.parse(str), options rescue nil
122
304
  end
123
- self::JSON_ENV_VARS.map(&json).compact.each { |hash| return new hash, options }
124
305
  nil
125
306
  end
126
307
 
308
+ ##
309
+ # @private Lookup Credentials from default file paths.
127
310
  def self.from_default_paths options
128
- self::DEFAULT_PATHS
311
+ paths
129
312
  .select { |p| ::File.file? p }
130
313
  .each do |file|
131
314
  return new file, options
@@ -133,13 +316,15 @@ module Google
133
316
  nil
134
317
  end
135
318
 
319
+ ##
320
+ # @private Lookup Credentials using Google::Auth.get_application_default.
136
321
  def self.from_application_default options
137
- scope = options[:scope] || self::SCOPE
322
+ scope = options[:scope] || self.scope
138
323
  client = Google::Auth.get_application_default scope
139
324
  new client, options
140
325
  end
141
- private_class_method :from_path_vars,
142
- :from_json_vars,
326
+
327
+ private_class_method :from_env_vars,
143
328
  :from_default_paths,
144
329
  :from_application_default
145
330
 
@@ -171,9 +356,9 @@ module Google
171
356
 
172
357
  def client_options options
173
358
  # Keyfile options have higher priority over constructor defaults
174
- options["token_credential_uri"] ||= self.class::TOKEN_CREDENTIAL_URI
175
- options["audience"] ||= self.class::AUDIENCE
176
- options["scope"] ||= self.class::SCOPE
359
+ options["token_credential_uri"] ||= self.class.token_credential_uri
360
+ options["audience"] ||= self.class.audience
361
+ options["scope"] ||= self.class.scope
177
362
 
178
363
  # client options for initializing signet client
179
364
  { token_credential_uri: options["token_credential_uri"],