googleauth 1.14.0 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -0
- data/Credentials.md +110 -0
- data/Errors.md +152 -0
- data/README.md +0 -1
- data/lib/googleauth/api_key.rb +9 -0
- data/lib/googleauth/application_default.rb +3 -1
- data/lib/googleauth/base_client.rb +5 -0
- data/lib/googleauth/bearer_token.rb +16 -2
- data/lib/googleauth/client_id.rb +9 -5
- data/lib/googleauth/compute_engine.rb +64 -18
- data/lib/googleauth/credentials.rb +67 -35
- data/lib/googleauth/credentials_loader.rb +24 -4
- data/lib/googleauth/default_credentials.rb +67 -32
- data/lib/googleauth/errors.rb +117 -0
- data/lib/googleauth/external_account/aws_credentials.rb +85 -18
- data/lib/googleauth/external_account/base_credentials.rb +31 -2
- data/lib/googleauth/external_account/external_account_utils.rb +15 -4
- data/lib/googleauth/external_account/identity_pool_credentials.rb +40 -15
- data/lib/googleauth/external_account/pluggable_credentials.rb +34 -19
- data/lib/googleauth/external_account.rb +44 -6
- data/lib/googleauth/iam.rb +19 -3
- data/lib/googleauth/id_tokens/errors.rb +13 -7
- data/lib/googleauth/id_tokens/key_sources.rb +13 -7
- data/lib/googleauth/id_tokens/verifier.rb +2 -3
- data/lib/googleauth/id_tokens.rb +4 -4
- data/lib/googleauth/impersonated_service_account.rb +117 -18
- data/lib/googleauth/json_key_reader.rb +11 -2
- data/lib/googleauth/oauth2/sts_client.rb +9 -4
- data/lib/googleauth/scope_util.rb +1 -1
- data/lib/googleauth/service_account.rb +37 -10
- data/lib/googleauth/service_account_jwt_header.rb +9 -2
- data/lib/googleauth/signet.rb +24 -6
- data/lib/googleauth/user_authorizer.rb +35 -7
- data/lib/googleauth/user_refresh.rb +42 -16
- data/lib/googleauth/version.rb +1 -1
- data/lib/googleauth/web_user_authorizer.rb +46 -9
- data/lib/googleauth.rb +1 -0
- metadata +8 -5
|
@@ -63,12 +63,14 @@ module Google
|
|
|
63
63
|
# @param [String] code_verifier
|
|
64
64
|
# Random string of 43-128 chars used to verify the key exchange using
|
|
65
65
|
# PKCE.
|
|
66
|
+
# @raise [Google::Auth::InitializationError]
|
|
67
|
+
# If client_id is nil or scope is nil
|
|
66
68
|
def initialize client_id, scope, token_store,
|
|
67
69
|
legacy_callback_uri = nil,
|
|
68
70
|
callback_uri: nil,
|
|
69
71
|
code_verifier: nil
|
|
70
|
-
raise NIL_CLIENT_ID_ERROR if client_id.nil?
|
|
71
|
-
raise NIL_SCOPE_ERROR if scope.nil?
|
|
72
|
+
raise InitializationError, NIL_CLIENT_ID_ERROR if client_id.nil?
|
|
73
|
+
raise InitializationError, NIL_SCOPE_ERROR if scope.nil?
|
|
72
74
|
|
|
73
75
|
@client_id = client_id
|
|
74
76
|
@scope = Array(scope)
|
|
@@ -133,14 +135,19 @@ module Google
|
|
|
133
135
|
# the requested scopes
|
|
134
136
|
# @return [Google::Auth::UserRefreshCredentials]
|
|
135
137
|
# Stored credentials, nil if none present
|
|
138
|
+
# @raise [Google::Auth::CredentialsError]
|
|
139
|
+
# If the client ID in the stored token doesn't match the configured client ID
|
|
136
140
|
def get_credentials user_id, scope = nil
|
|
137
141
|
saved_token = stored_token user_id
|
|
138
142
|
return nil if saved_token.nil?
|
|
139
143
|
data = MultiJson.load saved_token
|
|
140
144
|
|
|
141
145
|
if data.fetch("client_id", @client_id.id) != @client_id.id
|
|
142
|
-
raise
|
|
143
|
-
|
|
146
|
+
raise CredentialsError.with_details(
|
|
147
|
+
format(MISMATCHED_CLIENT_ID_ERROR, data["client_id"], @client_id.id),
|
|
148
|
+
credential_type_name: self.class.name,
|
|
149
|
+
principal: principal
|
|
150
|
+
)
|
|
144
151
|
end
|
|
145
152
|
|
|
146
153
|
credentials = UserRefreshCredentials.new(
|
|
@@ -240,6 +247,8 @@ module Google
|
|
|
240
247
|
# Unique ID of the user for loading/storing credentials.
|
|
241
248
|
# @param [Google::Auth::UserRefreshCredentials] credentials
|
|
242
249
|
# Credentials to store.
|
|
250
|
+
# @return [Google::Auth::UserRefreshCredentials]
|
|
251
|
+
# The stored credentials
|
|
243
252
|
def store_credentials user_id, credentials
|
|
244
253
|
json = MultiJson.dump(
|
|
245
254
|
client_id: credentials.client_id,
|
|
@@ -269,6 +278,15 @@ module Google
|
|
|
269
278
|
SecureRandom.alphanumeric random_number
|
|
270
279
|
end
|
|
271
280
|
|
|
281
|
+
# Returns the principal identifier for this authorizer
|
|
282
|
+
# The client ID is used as the principal for user authorizers
|
|
283
|
+
#
|
|
284
|
+
# @private
|
|
285
|
+
# @return [String] The client ID associated with this authorizer
|
|
286
|
+
def principal
|
|
287
|
+
@client_id.id
|
|
288
|
+
end
|
|
289
|
+
|
|
272
290
|
private
|
|
273
291
|
|
|
274
292
|
# @private Fetch stored token with given user_id
|
|
@@ -276,9 +294,11 @@ module Google
|
|
|
276
294
|
# @param [String] user_id
|
|
277
295
|
# Unique ID of the user for loading/storing credentials.
|
|
278
296
|
# @return [String] The saved token from @token_store
|
|
297
|
+
# @raise [Google::Auth::InitializationError]
|
|
298
|
+
# If user_id is nil or token_store is nil
|
|
279
299
|
def stored_token user_id
|
|
280
|
-
raise NIL_USER_ID_ERROR if user_id.nil?
|
|
281
|
-
raise NIL_TOKEN_STORE_ERROR if @token_store.nil?
|
|
300
|
+
raise InitializationError, NIL_USER_ID_ERROR if user_id.nil?
|
|
301
|
+
raise InitializationError, NIL_TOKEN_STORE_ERROR if @token_store.nil?
|
|
282
302
|
|
|
283
303
|
@token_store.load user_id
|
|
284
304
|
end
|
|
@@ -303,9 +323,17 @@ module Google
|
|
|
303
323
|
# Absolute URL to resolve the callback against if necessary.
|
|
304
324
|
# @return [String]
|
|
305
325
|
# Redirect URI
|
|
326
|
+
# @raise [Google::Auth::CredentialsError]
|
|
327
|
+
# If the callback URI is relative and base_url is nil or not absolute
|
|
306
328
|
def redirect_uri_for base_url
|
|
307
329
|
return @callback_uri if uri_is_postmessage?(@callback_uri) || !URI(@callback_uri).scheme.nil?
|
|
308
|
-
|
|
330
|
+
if base_url.nil? || URI(base_url).scheme.nil?
|
|
331
|
+
raise CredentialsError.with_details(
|
|
332
|
+
format(MISSING_ABSOLUTE_URL_ERROR, @callback_uri),
|
|
333
|
+
credential_type_name: self.class.name,
|
|
334
|
+
principal: principal
|
|
335
|
+
)
|
|
336
|
+
end
|
|
309
337
|
URI.join(base_url, @callback_uri).to_s
|
|
310
338
|
end
|
|
311
339
|
|
|
@@ -12,9 +12,10 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
require "googleauth/signet"
|
|
16
15
|
require "googleauth/credentials_loader"
|
|
16
|
+
require "googleauth/errors"
|
|
17
17
|
require "googleauth/scope_util"
|
|
18
|
+
require "googleauth/signet"
|
|
18
19
|
require "multi_json"
|
|
19
20
|
|
|
20
21
|
module Google
|
|
@@ -38,21 +39,29 @@ module Google
|
|
|
38
39
|
attr_reader :project_id
|
|
39
40
|
attr_reader :quota_project_id
|
|
40
41
|
|
|
42
|
+
# @private
|
|
43
|
+
# @type [::String] The type name for this credential.
|
|
44
|
+
CREDENTIAL_TYPE_NAME = "authorized_user".freeze
|
|
45
|
+
|
|
41
46
|
# Create a UserRefreshCredentials.
|
|
42
47
|
#
|
|
43
|
-
# @param json_key_io [IO]
|
|
48
|
+
# @param json_key_io [IO] An IO object containing the JSON key
|
|
44
49
|
# @param scope [string|array|nil] the scope(s) to access
|
|
45
50
|
def self.make_creds options = {}
|
|
46
51
|
json_key_io, scope = options.values_at :json_key_io, :scope
|
|
47
|
-
user_creds =
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
user_creds = if json_key_io
|
|
53
|
+
CredentialsLoader.load_and_verify_json_key_type json_key_io, CREDENTIAL_TYPE_NAME
|
|
54
|
+
read_json_key json_key_io
|
|
55
|
+
else
|
|
56
|
+
{
|
|
57
|
+
"client_id" => ENV[CredentialsLoader::CLIENT_ID_VAR],
|
|
58
|
+
"client_secret" => ENV[CredentialsLoader::CLIENT_SECRET_VAR],
|
|
59
|
+
"refresh_token" => ENV[CredentialsLoader::REFRESH_TOKEN_VAR],
|
|
60
|
+
"project_id" => ENV[CredentialsLoader::PROJECT_ID_VAR],
|
|
61
|
+
"quota_project_id" => nil,
|
|
62
|
+
"universe_domain" => nil
|
|
63
|
+
}
|
|
64
|
+
end
|
|
56
65
|
new(token_credential_uri: TOKEN_CRED_URI,
|
|
57
66
|
client_id: user_creds["client_id"],
|
|
58
67
|
client_secret: user_creds["client_secret"],
|
|
@@ -64,13 +73,16 @@ module Google
|
|
|
64
73
|
.configure_connection(options)
|
|
65
74
|
end
|
|
66
75
|
|
|
67
|
-
# Reads
|
|
68
|
-
#
|
|
76
|
+
# Reads a JSON key from an IO object and extracts required fields.
|
|
77
|
+
#
|
|
78
|
+
# @param [IO] json_key_io An IO object containing the JSON key
|
|
79
|
+
# @return [Hash] The parsed JSON key
|
|
80
|
+
# @raise [Google::Auth::InitializationError] If the JSON is missing required fields
|
|
69
81
|
def self.read_json_key json_key_io
|
|
70
82
|
json_key = MultiJson.load json_key_io.read
|
|
71
83
|
wanted = ["client_id", "client_secret", "refresh_token"]
|
|
72
84
|
wanted.each do |key|
|
|
73
|
-
raise "the json is missing the #{key} field" unless json_key.key? key
|
|
85
|
+
raise InitializationError, "the json is missing the #{key} field" unless json_key.key? key
|
|
74
86
|
end
|
|
75
87
|
json_key
|
|
76
88
|
end
|
|
@@ -106,6 +118,10 @@ module Google
|
|
|
106
118
|
end
|
|
107
119
|
|
|
108
120
|
# Revokes the credential
|
|
121
|
+
#
|
|
122
|
+
# @param [Hash] options Options for revoking the credential
|
|
123
|
+
# @option options [Faraday::Connection] :connection The connection to use
|
|
124
|
+
# @raise [Google::Auth::AuthorizationError] If the revocation request fails
|
|
109
125
|
def revoke! options = {}
|
|
110
126
|
c = options[:connection] || Faraday.default_connection
|
|
111
127
|
|
|
@@ -117,8 +133,11 @@ module Google
|
|
|
117
133
|
self.refresh_token = nil
|
|
118
134
|
self.expires_at = 0
|
|
119
135
|
else
|
|
120
|
-
raise(
|
|
121
|
-
|
|
136
|
+
raise AuthorizationError.with_details(
|
|
137
|
+
"Unexpected error code #{resp.status}",
|
|
138
|
+
credential_type_name: self.class.name,
|
|
139
|
+
principal: principal
|
|
140
|
+
)
|
|
122
141
|
end
|
|
123
142
|
end
|
|
124
143
|
end
|
|
@@ -157,6 +176,13 @@ module Google
|
|
|
157
176
|
|
|
158
177
|
self
|
|
159
178
|
end
|
|
179
|
+
|
|
180
|
+
# Returns the client ID as the principal for user refresh credentials
|
|
181
|
+
# @private
|
|
182
|
+
# @return [String, Symbol] the client ID or :user_refresh if not available
|
|
183
|
+
def principal
|
|
184
|
+
@client_id || :user_refresh
|
|
185
|
+
end
|
|
160
186
|
end
|
|
161
187
|
end
|
|
162
188
|
end
|
data/lib/googleauth/version.rb
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
require "multi_json"
|
|
16
|
+
require "googleauth/errors"
|
|
16
17
|
require "googleauth/signet"
|
|
17
18
|
require "googleauth/user_authorizer"
|
|
18
19
|
require "googleauth/user_refresh"
|
|
@@ -79,6 +80,8 @@ module Google
|
|
|
79
80
|
#
|
|
80
81
|
# @param [Rack::Request] request
|
|
81
82
|
# Current request
|
|
83
|
+
# @return [String, nil]
|
|
84
|
+
# Redirect URI if successfully extracted, nil otherwise
|
|
82
85
|
def self.handle_auth_callback_deferred request
|
|
83
86
|
callback_state, redirect_uri = extract_callback_state request
|
|
84
87
|
request.session[CALLBACK_STATE_KEY] = MultiJson.dump callback_state
|
|
@@ -151,11 +154,13 @@ module Google
|
|
|
151
154
|
# Optional key-values to be returned to the oauth callback.
|
|
152
155
|
# @return [String]
|
|
153
156
|
# Authorization url
|
|
157
|
+
# @raise [Google::Auth::InitializationError]
|
|
158
|
+
# If request is nil or request.session is nil
|
|
154
159
|
def get_authorization_url options = {}
|
|
155
160
|
options = options.dup
|
|
156
161
|
request = options[:request]
|
|
157
|
-
raise NIL_REQUEST_ERROR if request.nil?
|
|
158
|
-
raise NIL_SESSION_ERROR if request.session.nil?
|
|
162
|
+
raise InitializationError, NIL_REQUEST_ERROR if request.nil?
|
|
163
|
+
raise InitializationError, NIL_SESSION_ERROR if request.session.nil?
|
|
159
164
|
|
|
160
165
|
state = options[:state] || {}
|
|
161
166
|
|
|
@@ -181,9 +186,9 @@ module Google
|
|
|
181
186
|
# requested scopes
|
|
182
187
|
# @return [Google::Auth::UserRefreshCredentials]
|
|
183
188
|
# Stored credentials, nil if none present
|
|
184
|
-
# @raise [
|
|
185
|
-
#
|
|
186
|
-
#
|
|
189
|
+
# @raise [Google::Auth::AuthorizationError]
|
|
190
|
+
# If the authorization code is missing, there's an error in the request,
|
|
191
|
+
# or the state token doesn't match
|
|
187
192
|
def get_credentials user_id, request = nil, scope = nil
|
|
188
193
|
if request&.session&.key? CALLBACK_STATE_KEY
|
|
189
194
|
# Note - in theory, no need to check required scope as this is
|
|
@@ -202,6 +207,12 @@ module Google
|
|
|
202
207
|
end
|
|
203
208
|
end
|
|
204
209
|
|
|
210
|
+
# Extract the callback state from the request
|
|
211
|
+
#
|
|
212
|
+
# @param [Rack::Request] request
|
|
213
|
+
# Current request
|
|
214
|
+
# @return [Array<Hash, String>]
|
|
215
|
+
# Callback state and redirect URI
|
|
205
216
|
def self.extract_callback_state request
|
|
206
217
|
state = MultiJson.load(request.params[STATE_PARAM] || "{}")
|
|
207
218
|
redirect_uri = state[CURRENT_URI_KEY]
|
|
@@ -214,6 +225,15 @@ module Google
|
|
|
214
225
|
[callback_state, redirect_uri]
|
|
215
226
|
end
|
|
216
227
|
|
|
228
|
+
# Returns the principal identifier for this web authorizer
|
|
229
|
+
# This is a class method that returns a symbol since
|
|
230
|
+
# we might not have a client_id in the static callback context
|
|
231
|
+
#
|
|
232
|
+
# @return [Symbol] The symbol for web user authorization
|
|
233
|
+
def self.principal
|
|
234
|
+
:web_user_authorization
|
|
235
|
+
end
|
|
236
|
+
|
|
217
237
|
# Verifies the results of an authorization callback
|
|
218
238
|
#
|
|
219
239
|
# @param [Hash] state
|
|
@@ -224,13 +244,30 @@ module Google
|
|
|
224
244
|
# Error message if failed
|
|
225
245
|
# @param [Rack::Request] request
|
|
226
246
|
# Current request
|
|
247
|
+
# @raise [Google::Auth::AuthorizationError]
|
|
248
|
+
# If the authorization code is missing, there's an error in the callback state,
|
|
249
|
+
# or the state token doesn't match
|
|
227
250
|
def self.validate_callback_state state, request
|
|
228
|
-
|
|
251
|
+
if state[AUTH_CODE_KEY].nil?
|
|
252
|
+
raise AuthorizationError.with_details(
|
|
253
|
+
MISSING_AUTH_CODE_ERROR,
|
|
254
|
+
credential_type_name: name,
|
|
255
|
+
principal: principal
|
|
256
|
+
)
|
|
257
|
+
end
|
|
258
|
+
|
|
229
259
|
if state[ERROR_CODE_KEY]
|
|
230
|
-
raise
|
|
231
|
-
|
|
260
|
+
raise AuthorizationError.with_details(
|
|
261
|
+
format(AUTHORIZATION_ERROR, state[ERROR_CODE_KEY]),
|
|
262
|
+
credential_type_name: name,
|
|
263
|
+
principal: principal
|
|
264
|
+
)
|
|
232
265
|
elsif request.session[XSRF_KEY] != state[SESSION_ID_KEY]
|
|
233
|
-
raise
|
|
266
|
+
raise AuthorizationError.with_details(
|
|
267
|
+
INVALID_STATE_TOKEN_ERROR,
|
|
268
|
+
credential_type_name: name,
|
|
269
|
+
principal: principal
|
|
270
|
+
)
|
|
234
271
|
end
|
|
235
272
|
end
|
|
236
273
|
|
data/lib/googleauth.rb
CHANGED
|
@@ -18,6 +18,7 @@ require "googleauth/bearer_token"
|
|
|
18
18
|
require "googleauth/client_id"
|
|
19
19
|
require "googleauth/credentials"
|
|
20
20
|
require "googleauth/default_credentials"
|
|
21
|
+
require "googleauth/errors"
|
|
21
22
|
require "googleauth/external_account"
|
|
22
23
|
require "googleauth/id_tokens"
|
|
23
24
|
require "googleauth/impersonated_service_account"
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: googleauth
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.16.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Google LLC
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: faraday
|
|
@@ -66,7 +66,7 @@ dependencies:
|
|
|
66
66
|
version: '1.4'
|
|
67
67
|
- - "<"
|
|
68
68
|
- !ruby/object:Gem::Version
|
|
69
|
-
version: '
|
|
69
|
+
version: '4.0'
|
|
70
70
|
type: :runtime
|
|
71
71
|
prerelease: false
|
|
72
72
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -76,7 +76,7 @@ dependencies:
|
|
|
76
76
|
version: '1.4'
|
|
77
77
|
- - "<"
|
|
78
78
|
- !ruby/object:Gem::Version
|
|
79
|
-
version: '
|
|
79
|
+
version: '4.0'
|
|
80
80
|
- !ruby/object:Gem::Dependency
|
|
81
81
|
name: multi_json
|
|
82
82
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -142,6 +142,8 @@ files:
|
|
|
142
142
|
- ".yardopts"
|
|
143
143
|
- CHANGELOG.md
|
|
144
144
|
- CODE_OF_CONDUCT.md
|
|
145
|
+
- Credentials.md
|
|
146
|
+
- Errors.md
|
|
145
147
|
- LICENSE
|
|
146
148
|
- README.md
|
|
147
149
|
- SECURITY.md
|
|
@@ -155,6 +157,7 @@ files:
|
|
|
155
157
|
- lib/googleauth/credentials.rb
|
|
156
158
|
- lib/googleauth/credentials_loader.rb
|
|
157
159
|
- lib/googleauth/default_credentials.rb
|
|
160
|
+
- lib/googleauth/errors.rb
|
|
158
161
|
- lib/googleauth/external_account.rb
|
|
159
162
|
- lib/googleauth/external_account/aws_credentials.rb
|
|
160
163
|
- lib/googleauth/external_account/base_credentials.rb
|
|
@@ -202,7 +205,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
202
205
|
- !ruby/object:Gem::Version
|
|
203
206
|
version: '0'
|
|
204
207
|
requirements: []
|
|
205
|
-
rubygems_version: 3.6.
|
|
208
|
+
rubygems_version: 3.6.9
|
|
206
209
|
specification_version: 4
|
|
207
210
|
summary: Google Auth Library for Ruby
|
|
208
211
|
test_files: []
|