googleauth 0.1.0 → 0.16.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/.github/CODEOWNERS +7 -0
  3. data/.github/CONTRIBUTING.md +74 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +36 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +21 -0
  6. data/.github/ISSUE_TEMPLATE/support_request.md +7 -0
  7. data/.github/workflows/ci.yml +55 -0
  8. data/.github/workflows/release-please.yml +39 -0
  9. data/.gitignore +3 -0
  10. data/.kokoro/populate-secrets.sh +76 -0
  11. data/.kokoro/release.cfg +52 -0
  12. data/.kokoro/release.sh +18 -0
  13. data/.kokoro/trampoline_v2.sh +489 -0
  14. data/.repo-metadata.json +5 -0
  15. data/.rubocop.yml +17 -0
  16. data/.toys/.toys.rb +45 -0
  17. data/.toys/ci.rb +43 -0
  18. data/.toys/kokoro/.toys.rb +66 -0
  19. data/.toys/kokoro/publish-docs.rb +67 -0
  20. data/.toys/kokoro/publish-gem.rb +53 -0
  21. data/.toys/linkinator.rb +43 -0
  22. data/.trampolinerc +48 -0
  23. data/CHANGELOG.md +192 -0
  24. data/CODE_OF_CONDUCT.md +43 -0
  25. data/Gemfile +22 -1
  26. data/{COPYING → LICENSE} +0 -0
  27. data/README.md +140 -17
  28. data/googleauth.gemspec +28 -28
  29. data/integration/helper.rb +31 -0
  30. data/integration/id_tokens/key_source_test.rb +74 -0
  31. data/lib/googleauth.rb +7 -37
  32. data/lib/googleauth/application_default.rb +81 -0
  33. data/lib/googleauth/client_id.rb +104 -0
  34. data/lib/googleauth/compute_engine.rb +73 -26
  35. data/lib/googleauth/credentials.rb +561 -0
  36. data/lib/googleauth/credentials_loader.rb +207 -0
  37. data/lib/googleauth/default_credentials.rb +93 -0
  38. data/lib/googleauth/iam.rb +75 -0
  39. data/lib/googleauth/id_tokens.rb +233 -0
  40. data/lib/googleauth/id_tokens/errors.rb +71 -0
  41. data/lib/googleauth/id_tokens/key_sources.rb +396 -0
  42. data/lib/googleauth/id_tokens/verifier.rb +142 -0
  43. data/lib/googleauth/json_key_reader.rb +50 -0
  44. data/lib/googleauth/scope_util.rb +61 -0
  45. data/lib/googleauth/service_account.rb +175 -67
  46. data/lib/googleauth/signet.rb +69 -8
  47. data/lib/googleauth/stores/file_token_store.rb +65 -0
  48. data/lib/googleauth/stores/redis_token_store.rb +96 -0
  49. data/lib/googleauth/token_store.rb +69 -0
  50. data/lib/googleauth/user_authorizer.rb +285 -0
  51. data/lib/googleauth/user_refresh.rb +129 -0
  52. data/lib/googleauth/version.rb +1 -1
  53. data/lib/googleauth/web_user_authorizer.rb +295 -0
  54. data/spec/googleauth/apply_auth_examples.rb +96 -94
  55. data/spec/googleauth/client_id_spec.rb +160 -0
  56. data/spec/googleauth/compute_engine_spec.rb +125 -55
  57. data/spec/googleauth/credentials_spec.rb +600 -0
  58. data/spec/googleauth/get_application_default_spec.rb +232 -80
  59. data/spec/googleauth/iam_spec.rb +80 -0
  60. data/spec/googleauth/scope_util_spec.rb +77 -0
  61. data/spec/googleauth/service_account_spec.rb +422 -68
  62. data/spec/googleauth/signet_spec.rb +101 -25
  63. data/spec/googleauth/stores/file_token_store_spec.rb +57 -0
  64. data/spec/googleauth/stores/redis_token_store_spec.rb +50 -0
  65. data/spec/googleauth/stores/store_examples.rb +58 -0
  66. data/spec/googleauth/user_authorizer_spec.rb +343 -0
  67. data/spec/googleauth/user_refresh_spec.rb +359 -0
  68. data/spec/googleauth/web_user_authorizer_spec.rb +172 -0
  69. data/spec/spec_helper.rb +51 -10
  70. data/test/helper.rb +33 -0
  71. data/test/id_tokens/key_sources_test.rb +240 -0
  72. data/test/id_tokens/verifier_test.rb +269 -0
  73. metadata +112 -75
  74. data/.travis.yml +0 -18
  75. data/CONTRIBUTING.md +0 -32
  76. data/Rakefile +0 -15
@@ -0,0 +1,43 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project,
4
+ and in the interest of fostering an open and welcoming community,
5
+ we pledge to respect all people who contribute through reporting issues,
6
+ posting feature requests, updating documentation,
7
+ submitting pull requests or patches, and other activities.
8
+
9
+ We are committed to making participation in this project
10
+ a harassment-free experience for everyone,
11
+ regardless of level of experience, gender, gender identity and expression,
12
+ sexual orientation, disability, personal appearance,
13
+ body size, race, ethnicity, age, religion, or nationality.
14
+
15
+ Examples of unacceptable behavior by participants include:
16
+
17
+ * The use of sexualized language or imagery
18
+ * Personal attacks
19
+ * Trolling or insulting/derogatory comments
20
+ * Public or private harassment
21
+ * Publishing other's private information,
22
+ such as physical or electronic
23
+ addresses, without explicit permission
24
+ * Other unethical or unprofessional conduct.
25
+
26
+ Project maintainers have the right and responsibility to remove, edit, or reject
27
+ comments, commits, code, wiki edits, issues, and other contributions
28
+ that are not aligned to this Code of Conduct.
29
+ By adopting this Code of Conduct,
30
+ project maintainers commit themselves to fairly and consistently
31
+ applying these principles to every aspect of managing this project.
32
+ Project maintainers who do not follow or enforce the Code of Conduct
33
+ may be permanently removed from the project team.
34
+
35
+ This code of conduct applies both within project spaces and in public spaces
36
+ when an individual is representing the project or its community.
37
+
38
+ Instances of abusive, harassing, or otherwise unacceptable behavior
39
+ may be reported by opening an issue
40
+ or contacting one or more of the project maintainers.
41
+
42
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0,
43
+ available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
data/Gemfile CHANGED
@@ -1,4 +1,25 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in googleauth.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem "bundler", ">= 1.9"
8
+ gem "coveralls", "~> 0.7"
9
+ gem "fakefs", "~> 0.6"
10
+ gem "fakeredis", "~> 0.5"
11
+ gem "google-style", "~> 1.25.1"
12
+ gem "logging", "~> 2.0"
13
+ gem "minitest", "~> 5.14"
14
+ gem "minitest-focus", "~> 1.1"
15
+ gem "rack-test", "~> 0.6"
16
+ gem "rake", "~> 13.0"
17
+ gem "redis", "~> 3.2"
18
+ gem "rspec", "~> 3.0"
19
+ gem "simplecov", "~> 0.9"
20
+ gem "sinatra"
21
+ gem "webmock", "~> 3.8"
22
+ end
23
+
24
+ gem "faraday", ">= 0.17.3", "< 2.0"
25
+ gem "gems", "~> 1.2"
File without changes
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
- # Google Auth Library for Ruby
1
+ # Google Auth Library for Ruby
2
2
 
3
3
  <dl>
4
- <dt>Homepage</dt><dd><a href="http://www.github.com/google/google-auth-library-ruby">http://www.github.com/google/google-auth-library-ruby</a></dd>
4
+ <dt>Homepage</dt><dd><a href="http://www.github.com/googleapis/google-auth-library-ruby">http://www.github.com/googleapis/google-auth-library-ruby</a></dd>
5
5
  <dt>Authors</dt><dd><a href="mailto:temiola@google.com">Tim Emiola</a></dd>
6
6
  <dt>Copyright</dt><dd>Copyright © 2015 Google, Inc.</dd>
7
7
  <dt>License</dt><dd>Apache 2.0</dd>
8
8
  </dl>
9
9
 
10
- [![Build Status](https://secure.travis-ci.org/google/google-auth-library-ruby.png)](http://travis-ci.org/google/google-auth-library-ruby)
11
- [![Dependency Status](https://gemnasium.com/google/google-auth-library-ruby.png)](https://gemnasium.com/google/google-auth-library-ruby)
10
+ [![Gem Version](https://badge.fury.io/rb/googleauth.svg)](http://badge.fury.io/rb/googleauth)
12
11
 
13
12
  ## Description
14
13
 
@@ -36,8 +35,9 @@ $ gem install googleauth
36
35
  require 'googleauth'
37
36
 
38
37
  # Get the environment configured authorization
39
- scope = 'https://www.googleapis.com/auth/userinfo.profile'
40
- authorization = Google::Auth.get_application_default(scope)
38
+ scopes = ['https://www.googleapis.com/auth/cloud-platform',
39
+ 'https://www.googleapis.com/auth/compute']
40
+ authorization = Google::Auth.get_application_default(scopes)
41
41
 
42
42
  # Add the the access token obtained using the authorization to a hash, e.g
43
43
  # headers.
@@ -59,18 +59,142 @@ and authorization level for the application independent of the user. This is
59
59
  the recommended approach to authorize calls to Cloud APIs, particularly when
60
60
  you're building an application that uses Google Compute Engine.
61
61
 
62
- ## What about auth in google-apis-ruby-client?
62
+ ## User Credentials
63
63
 
64
- The goal is for all auth done by
65
- [google-apis-ruby-client][google-apis-ruby-client] to be performed by this
66
- library. I.e, eventually google-apis-ruby-client will just take a dependency
67
- on this library. This update is a work in progress, but should be completed
68
- by Q2 2015.
64
+ The library also provides support for requesting and storing user
65
+ credentials (3-Legged OAuth2.) Two implementations are currently available,
66
+ a generic authorizer useful for command line apps or custom integrations as
67
+ well as a web variant tailored toward Rack-based applications.
68
+
69
+ The authorizers are intended for authorization use cases. For sign-on,
70
+ see [Google Identity Platform](https://developers.google.com/identity/)
71
+
72
+ ### Example (Web)
73
+
74
+ ```ruby
75
+ require 'googleauth'
76
+ require 'googleauth/web_user_authorizer'
77
+ require 'googleauth/stores/redis_token_store'
78
+ require 'redis'
79
+
80
+ client_id = Google::Auth::ClientId.from_file('/path/to/client_secrets.json')
81
+ scope = ['https://www.googleapis.com/auth/drive']
82
+ token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
83
+ authorizer = Google::Auth::WebUserAuthorizer.new(
84
+ client_id, scope, token_store, '/oauth2callback')
85
+
86
+
87
+ get('/authorize') do
88
+ # NOTE: Assumes the user is already authenticated to the app
89
+ user_id = request.session['user_id']
90
+ credentials = authorizer.get_credentials(user_id, request)
91
+ if credentials.nil?
92
+ redirect authorizer.get_authorization_url(login_hint: user_id, request: request)
93
+ end
94
+ # Credentials are valid, can call APIs
95
+ # ...
96
+ end
97
+
98
+ get('/oauth2callback') do
99
+ target_url = Google::Auth::WebUserAuthorizer.handle_auth_callback_deferred(
100
+ request)
101
+ redirect target_url
102
+ end
103
+ ```
104
+
105
+ ### Example (Command Line)
106
+
107
+ ```ruby
108
+ require 'googleauth'
109
+ require 'googleauth/stores/file_token_store'
110
+
111
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
112
+
113
+ scope = 'https://www.googleapis.com/auth/drive'
114
+ client_id = Google::Auth::ClientId.from_file('/path/to/client_secrets.json')
115
+ token_store = Google::Auth::Stores::FileTokenStore.new(
116
+ :file => '/path/to/tokens.yaml')
117
+ authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
118
+
119
+ credentials = authorizer.get_credentials(user_id)
120
+ if credentials.nil?
121
+ url = authorizer.get_authorization_url(base_url: OOB_URI )
122
+ puts "Open #{url} in your browser and enter the resulting code:"
123
+ code = gets
124
+ credentials = authorizer.get_and_store_credentials_from_code(
125
+ user_id: user_id, code: code, base_url: OOB_URI)
126
+ end
127
+
128
+ # OK to use credentials
129
+ ```
130
+
131
+ ### Example (Service Account)
132
+
133
+ ```ruby
134
+ scope = 'https://www.googleapis.com/auth/androidpublisher'
135
+
136
+ authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
137
+ json_key_io: File.open('/path/to/service_account_json_key.json'),
138
+ scope: scope)
139
+
140
+ authorizer.fetch_access_token!
141
+ ```
142
+
143
+ ### Example (Environment Variables)
144
+
145
+ ```bash
146
+ export GOOGLE_ACCOUNT_TYPE=service_account
147
+ export GOOGLE_CLIENT_ID=000000000000000000000
148
+ export GOOGLE_CLIENT_EMAIL=xxxx@xxxx.iam.gserviceaccount.com
149
+ export GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
150
+ ```
151
+
152
+ ```ruby
153
+ require 'googleauth'
154
+ require 'google/apis/drive_v3'
155
+
156
+ Drive = ::Google::Apis::DriveV3
157
+ drive = Drive::DriveService.new
158
+
159
+ # Auths with ENV vars:
160
+ # "GOOGLE_CLIENT_ID",
161
+ # "GOOGLE_CLIENT_EMAIL",
162
+ # "GOOGLE_ACCOUNT_TYPE",
163
+ # "GOOGLE_PRIVATE_KEY"
164
+ auth = ::Google::Auth::ServiceAccountCredentials
165
+ .make_creds(scope: 'https://www.googleapis.com/auth/drive')
166
+ drive.authorization = auth
167
+
168
+ list_files = drive.list_files()
169
+
170
+ ```
171
+
172
+ ### Storage
173
+
174
+ Authorizers require a storage instance to manage long term persistence of
175
+ access and refresh tokens. Two storage implementations are included:
176
+
177
+ * Google::Auth::Stores::FileTokenStore
178
+ * Google::Auth::Stores::RedisTokenStore
179
+
180
+ Custom storage implementations can also be used. See
181
+ [token_store.rb](https://googleapis.dev/ruby/googleauth/latest/Google/Auth/TokenStore.html) for additional details.
182
+
183
+ ## Supported Ruby Versions
184
+
185
+ This library is supported on Ruby 2.5+.
186
+
187
+ Google provides official support for Ruby versions that are actively supported
188
+ by Ruby Core—that is, Ruby versions that are either in normal maintenance or in
189
+ security maintenance, and not end of life. Currently, this means Ruby 2.5 and
190
+ later. Older versions of Ruby _may_ still work, but are unsupported and not
191
+ recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details
192
+ about the Ruby support schedule.
69
193
 
70
194
  ## License
71
195
 
72
196
  This library is licensed under Apache 2.0. Full license text is
73
- available in [COPYING][copying].
197
+ available in [LICENSE][license].
74
198
 
75
199
  ## Contributing
76
200
 
@@ -84,7 +208,6 @@ hesitate to
84
208
  [ask questions](http://stackoverflow.com/questions/tagged/google-auth-library-ruby)
85
209
  about the client or APIs on [StackOverflow](http://stackoverflow.com).
86
210
 
87
- [google-apis-ruby-client]: (https://github.com/google/google-api-ruby-client)
88
- [application default credentials]: (https://developers.google.com/accounts/docs/application-default-credentials)
89
- [contributing]: https://github.com/google/google-auth-library-ruby/tree/master/CONTRIBUTING.md
90
- [copying]: https://github.com/google/google-auth-library-ruby/tree/master/COPYING
211
+ [application default credentials]: https://developers.google.com/accounts/docs/application-default-credentials
212
+ [contributing]: https://github.com/googleapis/google-auth-library-ruby/tree/master/.github/CONTRIBUTING.md
213
+ [license]: https://github.com/googleapis/google-auth-library-ruby/tree/master/LICENSE
data/googleauth.gemspec CHANGED
@@ -1,39 +1,39 @@
1
1
  # -*- ruby -*-
2
2
  # encoding: utf-8
3
- $LOAD_PATH.push File.expand_path('../lib', __FILE__)
4
- require 'googleauth/version'
5
3
 
6
- Gem::Specification.new do |s|
7
- s.name = 'googleauth'
8
- s.version = Google::Auth::VERSION
9
- s.authors = ['Tim Emiola']
10
- s.email = 'temiola@google.com'
11
- s.homepage = 'https://github.com/google/google-auth-library-ruby'
12
- s.summary = 'Google Auth Library for Ruby'
13
- s.license = "Apache-2.0"
14
- s.description = <<-eos
4
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
5
+ require "googleauth/version"
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "googleauth"
9
+ gem.version = Google::Auth::VERSION
10
+ gem.authors = ["Tim Emiola"]
11
+ gem.email = "temiola@google.com"
12
+ gem.homepage = "https://github.com/googleapis/google-auth-library-ruby"
13
+ gem.summary = "Google Auth Library for Ruby"
14
+ gem.license = "Apache-2.0"
15
+ gem.description = <<-DESCRIPTION
15
16
  Allows simple authorization for accessing Google APIs.
16
17
  Provide support for Application Default Credentials, as described at
17
18
  https://developers.google.com/accounts/docs/application-default-credentials
18
- eos
19
+ DESCRIPTION
19
20
 
20
- s.files = `git ls-files`.split("\n")
21
- s.test_files = `git ls-files -- spec/*`.split("\n")
22
- s.executables = `git ls-files -- bin/*.rb`.split("\n").map do |f|
23
- File.basename(f)
21
+ gem.files = `git ls-files`.split "\n"
22
+ gem.test_files = `git ls-files -- spec/*`.split "\n"
23
+ gem.executables = `git ls-files -- bin/*.rb`.split("\n").map do |f|
24
+ File.basename f
24
25
  end
25
- s.require_paths = ['lib']
26
- s.platform = Gem::Platform::RUBY
26
+ gem.require_paths = ["lib"]
27
+
28
+ gem.platform = Gem::Platform::RUBY
29
+ gem.required_ruby_version = ">= 2.5"
27
30
 
28
- s.add_dependency 'faraday', '~> 0.9'
29
- s.add_dependency 'logging', '~> 1.8'
30
- s.add_dependency 'jwt', '~> 1.2.1'
31
- s.add_dependency 'memoist', '~> 0.11.0'
32
- s.add_dependency 'multi_json', '1.10.1'
33
- s.add_dependency 'signet', '~> 0.6.0'
31
+ gem.add_dependency "faraday", ">= 0.17.3", "< 2.0"
32
+ gem.add_dependency "jwt", ">= 1.4", "< 3.0"
33
+ gem.add_dependency "memoist", "~> 0.16"
34
+ gem.add_dependency "multi_json", "~> 1.11"
35
+ gem.add_dependency "os", ">= 0.9", "< 2.0"
36
+ gem.add_dependency "signet", "~> 0.14"
34
37
 
35
- s.add_development_dependency 'bundler', '~> 1.7'
36
- s.add_development_dependency 'rake', '~> 10.0'
37
- s.add_development_dependency 'rubocop', '~> 0.28.0'
38
- s.add_development_dependency 'rspec', '~> 3.0'
38
+ gem.add_development_dependency "yard", "~> 0.9"
39
39
  end
@@ -0,0 +1,31 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions are
5
+ # met:
6
+ #
7
+ # * Redistributions of source code must retain the above copyright
8
+ # notice, this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above
10
+ # copyright notice, this list of conditions and the following disclaimer
11
+ # in the documentation and/or other materials provided with the
12
+ # distribution.
13
+ # * Neither the name of Google Inc. nor the names of its
14
+ # contributors may be used to endorse or promote products derived from
15
+ # this software without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ require "minitest/autorun"
30
+ require "minitest/focus"
31
+ require "googleauth"
@@ -0,0 +1,74 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions are
5
+ # met:
6
+ #
7
+ # * Redistributions of source code must retain the above copyright
8
+ # notice, this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above
10
+ # copyright notice, this list of conditions and the following disclaimer
11
+ # in the documentation and/or other materials provided with the
12
+ # distribution.
13
+ # * Neither the name of Google Inc. nor the names of its
14
+ # contributors may be used to endorse or promote products derived from
15
+ # this software without specific prior written permission.
16
+ #
17
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ require "helper"
30
+
31
+ describe Google::Auth::IDTokens do
32
+ describe "key source" do
33
+ let(:legacy_oidc_key_source) {
34
+ Google::Auth::IDTokens::X509CertHttpKeySource.new "https://www.googleapis.com/oauth2/v1/certs"
35
+ }
36
+ let(:oidc_key_source) { Google::Auth::IDTokens.oidc_key_source }
37
+ let(:iap_key_source) { Google::Auth::IDTokens.iap_key_source }
38
+
39
+ it "Gets real keys from the OAuth2 V1 cert URL" do
40
+ keys = legacy_oidc_key_source.refresh_keys
41
+ refute_empty keys
42
+ keys.each do |key|
43
+ assert_kind_of OpenSSL::PKey::RSA, key.key
44
+ refute key.key.private?
45
+ assert_equal "RS256", key.algorithm
46
+ end
47
+ end
48
+
49
+ it "Gets real keys from the OAuth2 V3 cert URL" do
50
+ keys = oidc_key_source.refresh_keys
51
+ refute_empty keys
52
+ keys.each do |key|
53
+ assert_kind_of OpenSSL::PKey::RSA, key.key
54
+ refute key.key.private?
55
+ assert_equal "RS256", key.algorithm
56
+ end
57
+ end
58
+
59
+ it "Gets the same keys from the OAuth2 V1 and V3 cert URLs" do
60
+ keys_v1 = legacy_oidc_key_source.refresh_keys.map(&:key).map(&:export).sort
61
+ keys_v3 = oidc_key_source.refresh_keys.map(&:key).map(&:export).sort
62
+ assert_equal keys_v1, keys_v3
63
+ end
64
+
65
+ it "Gets real keys from the IAP public key URL" do
66
+ keys = iap_key_source.refresh_keys
67
+ refute_empty keys
68
+ keys.each do |key|
69
+ assert_kind_of OpenSSL::PKey::EC, key.key
70
+ assert_equal "ES256", key.algorithm
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/googleauth.rb CHANGED
@@ -27,40 +27,10 @@
27
27
  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
28
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
 
30
- require 'googleauth/service_account'
31
- require 'googleauth/compute_engine'
32
-
33
- module Google
34
- # Module Auth provides classes that provide Google-specific authorization
35
- # used to access Google APIs.
36
- module Auth
37
- NOT_FOUND_ERROR = <<END
38
- Could not load the default credentials. Browse to
39
- https://developers.google.com/accounts/docs/application-default-credentials
40
- for more information
41
- END
42
-
43
- # Obtains the default credentials implementation to use in this
44
- # environment.
45
- #
46
- # Use this to obtain the Application Default Credentials for accessing
47
- # Google APIs. Application Default Credentials are described in detail
48
- # at http://goo.gl/IUuyuX.
49
- #
50
- # If supplied, scope is used to create the credentials instance, when it
51
- # can applied. E.g, on compute engine, the scope is ignored.
52
- #
53
- # @param scope [string|array] the scope(s) to access
54
- # @param options [hash] allows override of the connection being used
55
- def get_application_default(scope, options = {})
56
- creds = ServiceAccountCredentials.from_env(scope)
57
- return creds unless creds.nil?
58
- creds = ServiceAccountCredentials.from_well_known_path(scope)
59
- return creds unless creds.nil?
60
- fail NOT_FOUND_ERROR unless GCECredentials.on_gce?(options)
61
- GCECredentials.new
62
- end
63
-
64
- module_function :get_application_default
65
- end
66
- end
30
+ require "googleauth/application_default"
31
+ require "googleauth/client_id"
32
+ require "googleauth/credentials"
33
+ require "googleauth/default_credentials"
34
+ require "googleauth/id_tokens"
35
+ require "googleauth/user_authorizer"
36
+ require "googleauth/web_user_authorizer"