googleauth 0.14.0 → 1.3.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.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +11 -0
  3. data/CHANGELOG.md +72 -10
  4. data/README.md +48 -13
  5. data/SECURITY.md +7 -0
  6. data/lib/googleauth/application_default.rb +10 -25
  7. data/lib/googleauth/client_id.rb +10 -25
  8. data/lib/googleauth/compute_engine.rb +16 -30
  9. data/lib/googleauth/credentials.rb +178 -74
  10. data/lib/googleauth/credentials_loader.rb +23 -44
  11. data/lib/googleauth/default_credentials.rb +10 -25
  12. data/lib/googleauth/iam.rb +11 -26
  13. data/lib/googleauth/id_tokens/errors.rb +9 -23
  14. data/lib/googleauth/id_tokens/key_sources.rb +26 -38
  15. data/lib/googleauth/id_tokens/verifier.rb +16 -32
  16. data/lib/googleauth/id_tokens.rb +9 -23
  17. data/lib/googleauth/json_key_reader.rb +10 -25
  18. data/lib/googleauth/scope_util.rb +11 -26
  19. data/lib/googleauth/service_account.rb +60 -59
  20. data/lib/googleauth/signet.rb +22 -28
  21. data/lib/googleauth/stores/file_token_store.rb +11 -25
  22. data/lib/googleauth/stores/redis_token_store.rb +11 -25
  23. data/lib/googleauth/token_store.rb +10 -25
  24. data/lib/googleauth/user_authorizer.rb +10 -25
  25. data/lib/googleauth/user_refresh.rb +15 -27
  26. data/lib/googleauth/version.rb +11 -26
  27. data/lib/googleauth/web_user_authorizer.rb +14 -32
  28. data/lib/googleauth.rb +10 -25
  29. metadata +26 -97
  30. data/.github/CODEOWNERS +0 -7
  31. data/.github/CONTRIBUTING.md +0 -74
  32. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -36
  33. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -21
  34. data/.github/ISSUE_TEMPLATE/support_request.md +0 -7
  35. data/.gitignore +0 -36
  36. data/.kokoro/build.bat +0 -16
  37. data/.kokoro/build.sh +0 -4
  38. data/.kokoro/continuous/common.cfg +0 -24
  39. data/.kokoro/continuous/linux.cfg +0 -25
  40. data/.kokoro/continuous/osx.cfg +0 -8
  41. data/.kokoro/continuous/post.cfg +0 -30
  42. data/.kokoro/continuous/windows.cfg +0 -29
  43. data/.kokoro/osx.sh +0 -4
  44. data/.kokoro/presubmit/common.cfg +0 -24
  45. data/.kokoro/presubmit/linux.cfg +0 -24
  46. data/.kokoro/presubmit/osx.cfg +0 -8
  47. data/.kokoro/presubmit/windows.cfg +0 -29
  48. data/.kokoro/release.cfg +0 -94
  49. data/.kokoro/trampoline.bat +0 -10
  50. data/.kokoro/trampoline.sh +0 -4
  51. data/.repo-metadata.json +0 -5
  52. data/.rspec +0 -2
  53. data/.rubocop.yml +0 -19
  54. data/Gemfile +0 -30
  55. data/Rakefile +0 -132
  56. data/googleauth.gemspec +0 -38
  57. data/integration/helper.rb +0 -31
  58. data/integration/id_tokens/key_source_test.rb +0 -74
  59. data/rakelib/devsite_builder.rb +0 -45
  60. data/rakelib/link_checker.rb +0 -64
  61. data/rakelib/repo_metadata.rb +0 -59
  62. data/spec/googleauth/apply_auth_examples.rb +0 -171
  63. data/spec/googleauth/client_id_spec.rb +0 -160
  64. data/spec/googleauth/compute_engine_spec.rb +0 -160
  65. data/spec/googleauth/credentials_spec.rb +0 -478
  66. data/spec/googleauth/get_application_default_spec.rb +0 -286
  67. data/spec/googleauth/iam_spec.rb +0 -80
  68. data/spec/googleauth/scope_util_spec.rb +0 -77
  69. data/spec/googleauth/service_account_spec.rb +0 -489
  70. data/spec/googleauth/signet_spec.rb +0 -142
  71. data/spec/googleauth/stores/file_token_store_spec.rb +0 -57
  72. data/spec/googleauth/stores/redis_token_store_spec.rb +0 -50
  73. data/spec/googleauth/stores/store_examples.rb +0 -58
  74. data/spec/googleauth/user_authorizer_spec.rb +0 -343
  75. data/spec/googleauth/user_refresh_spec.rb +0 -359
  76. data/spec/googleauth/web_user_authorizer_spec.rb +0 -172
  77. data/spec/spec_helper.rb +0 -92
  78. data/test/helper.rb +0 -33
  79. data/test/id_tokens/key_sources_test.rb +0 -240
  80. data/test/id_tokens/verifier_test.rb +0 -269
data/Rakefile DELETED
@@ -1,132 +0,0 @@
1
- # -*- ruby -*-
2
- require "json"
3
- require "bundler/gem_tasks"
4
-
5
- require "rubocop/rake_task"
6
- RuboCop::RakeTask.new
7
-
8
- require "rake/testtask"
9
-
10
- desc "Run tests."
11
- Rake::TestTask.new do |t|
12
- t.libs << "test"
13
- t.test_files = FileList["test/**/*_test.rb"]
14
- t.warning = false
15
- end
16
-
17
- desc "Run integration tests."
18
- Rake::TestTask.new("integration") do |t|
19
- t.libs << "integration"
20
- t.test_files = FileList["integration/**/*_test.rb"]
21
- t.warning = false
22
- end
23
-
24
- task :ci do
25
- header "Using Ruby - #{RUBY_VERSION}"
26
- sh "bundle exec rubocop"
27
- Rake::Task["test"].invoke
28
- Rake::Task["integration"].invoke
29
- sh "bundle exec rspec"
30
- end
31
-
32
- task :release_gem, :tag do |_t, args|
33
- tag = args[:tag]
34
- raise "You must provide a tag to release." if tag.nil?
35
-
36
- # Verify the tag format "vVERSION"
37
- m = tag.match /v(?<version>\S*)/
38
- raise "Tag #{tag} does not match the expected format." if m.nil?
39
-
40
- version = m[:version]
41
- raise "You must provide a version." if version.nil?
42
-
43
- api_token = ENV["RUBYGEMS_API_TOKEN"]
44
-
45
- require "gems"
46
- if api_token
47
- ::Gems.configure do |config|
48
- config.key = api_token
49
- end
50
- end
51
-
52
- Bundler.with_clean_env do
53
- sh "rm -rf pkg"
54
- sh "bundle update"
55
- sh "bundle exec rake build"
56
- end
57
-
58
- path_to_be_pushed = "pkg/googleauth-#{version}.gem"
59
- gem_was_published = nil
60
- if File.file? path_to_be_pushed
61
- begin
62
- response = ::Gems.push File.new(path_to_be_pushed)
63
- puts response
64
- raise unless response.include? "Successfully registered gem:"
65
- gem_was_published = true
66
- puts "Successfully built and pushed googleauth for version #{version}"
67
- rescue StandardError => e
68
- gem_was_published = false
69
- puts "Error while releasing googleauth version #{version}: #{e.message}"
70
- end
71
- else
72
- raise "Cannot build googleauth for version #{version}"
73
- end
74
-
75
- Rake::Task["kokoro:publish_docs"].invoke if gem_was_published
76
- end
77
-
78
- namespace :kokoro do
79
- task :load_env_vars do
80
- service_account = "#{ENV['KOKORO_GFILE_DIR']}/service-account.json"
81
- ENV["GOOGLE_APPLICATION_CREDENTIALS"] = service_account
82
- filename = "#{ENV['KOKORO_GFILE_DIR']}/env_vars.json"
83
- env_vars = JSON.parse File.read(filename)
84
- env_vars.each { |k, v| ENV[k] = v }
85
- end
86
-
87
- task :presubmit do
88
- Rake::Task["ci"].invoke
89
- end
90
-
91
- task :continuous do
92
- Rake::Task["ci"].invoke
93
- end
94
-
95
- task :post do
96
- require_relative "rakelib/link_checker.rb"
97
-
98
- link_checker = LinkChecker.new
99
- link_checker.run
100
- exit link_checker.exit_status
101
- end
102
-
103
- task :nightly do
104
- Rake::Task["ci"].invoke
105
- end
106
-
107
- task :release do
108
- version = "0.1.0"
109
- Bundler.with_clean_env do
110
- version = `bundle exec gem list`
111
- .split("\n").select { |line| line.include? "googleauth" }
112
- .first.split("(").last.split(")").first || "0.1.0"
113
- end
114
- Rake::Task["kokoro:load_env_vars"].invoke
115
- Rake::Task["release_gem"].invoke "v#{version}"
116
- end
117
-
118
- task :publish_docs do
119
- require_relative "rakelib/devsite_builder.rb"
120
-
121
- DevsiteBuilder.new(__dir__).publish
122
- end
123
- end
124
-
125
- def header str, token = "#"
126
- line_length = str.length + 8
127
- puts ""
128
- puts token * line_length
129
- puts "#{token * 3} #{str} #{token * 3}"
130
- puts token * line_length
131
- puts ""
132
- end
data/googleauth.gemspec DELETED
@@ -1,38 +0,0 @@
1
- # -*- ruby -*-
2
- # encoding: utf-8
3
-
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
16
- Allows simple authorization for accessing Google APIs.
17
- Provide support for Application Default Credentials, as described at
18
- https://developers.google.com/accounts/docs/application-default-credentials
19
- DESCRIPTION
20
-
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
25
- end
26
- gem.require_paths = ["lib"]
27
- gem.platform = Gem::Platform::RUBY
28
- gem.required_ruby_version = ">= 2.4.0"
29
-
30
- gem.add_dependency "faraday", ">= 0.17.3", "< 2.0"
31
- gem.add_dependency "jwt", ">= 1.4", "< 3.0"
32
- gem.add_dependency "memoist", "~> 0.16"
33
- gem.add_dependency "multi_json", "~> 1.11"
34
- gem.add_dependency "os", ">= 0.9", "< 2.0"
35
- gem.add_dependency "signet", "~> 0.14"
36
-
37
- gem.add_development_dependency "yard", "~> 0.9"
38
- end
@@ -1,31 +0,0 @@
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"
@@ -1,74 +0,0 @@
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
@@ -1,45 +0,0 @@
1
- require "pathname"
2
-
3
- require_relative "repo_metadata.rb"
4
-
5
- class DevsiteBuilder
6
- def initialize master_dir = "."
7
- @master_dir = Pathname.new master_dir
8
- @output_dir = "doc"
9
- @metadata = RepoMetadata.from_source "#{master_dir}/.repo-metadata.json"
10
- end
11
-
12
- def build
13
- FileUtils.remove_dir @output_dir if Dir.exist? @output_dir
14
- markup = "--markup markdown"
15
-
16
- Dir.chdir @master_dir do
17
- cmds = ["-o #{@output_dir}", markup]
18
- cmd "yard --verbose #{cmds.join ' '}"
19
- end
20
- @metadata.build @master_dir + @output_dir
21
- end
22
-
23
- def upload
24
- Dir.chdir @output_dir do
25
- opts = [
26
- "--credentials=#{ENV['KOKORO_KEYSTORE_DIR']}/73713_docuploader_service_account",
27
- "--staging-bucket=#{ENV.fetch 'STAGING_BUCKET', 'docs-staging'}",
28
- "--metadata-file=./docs.metadata"
29
- ]
30
- cmd "python3 -m docuploader upload . #{opts.join ' '}"
31
- end
32
- end
33
-
34
- def publish
35
- build
36
- upload
37
- end
38
-
39
- def cmd line
40
- puts line
41
- output = `#{line}`
42
- puts output
43
- output
44
- end
45
- end
@@ -1,64 +0,0 @@
1
- require "open3"
2
-
3
- class LinkChecker
4
- def initialize
5
- @failed = false
6
- end
7
-
8
- def run
9
- job_info
10
- git_commit = ENV.fetch "KOKORO_GITHUB_COMMIT", "master"
11
-
12
- markdown_files = Dir.glob "**/*.md"
13
- broken_markdown_links = check_links markdown_files,
14
- "https://github.com/googleapis/google-auth-library-ruby/tree/#{git_commit}",
15
- " --skip '^(?!(\\Wruby.*google|.*google.*\\Wruby|.*cloud\\.google\\.com))'"
16
-
17
- broken_devsite_links = check_links ["googleauth"],
18
- "https://googleapis.dev/ruby",
19
- "/latest/ --recurse --skip https:.*github.*"
20
-
21
- puts_broken_links broken_markdown_links
22
- puts_broken_links broken_devsite_links
23
- end
24
-
25
- def check_links location_list, base, tail
26
- broken_links = Hash.new { |h, k| h[k] = [] }
27
- location_list.each do |location|
28
- out, err, st = Open3.capture3 "npx linkinator #{base}/#{location}#{tail}"
29
- puts out
30
- unless st.to_i.zero?
31
- @failed = true
32
- puts err
33
- end
34
- checked_links = out.split "\n"
35
- checked_links.select! { |link| link =~ /\[\d+\]/ && !link.include?("[200]") }
36
- unless checked_links.empty?
37
- @failed = true
38
- broken_links[location] += checked_links
39
- end
40
- end
41
- broken_links
42
- end
43
-
44
- def puts_broken_links link_hash
45
- link_hash.each do |location, links|
46
- puts "#{location} contains the following broken links:"
47
- links.each { |link| puts " #{link}" }
48
- puts ""
49
- end
50
- end
51
-
52
- def job_info
53
- line_length = "Using Ruby - #{RUBY_VERSION}".length + 8
54
- puts ""
55
- puts "#" * line_length
56
- puts "### Using Ruby - #{RUBY_VERSION} ###"
57
- puts "#" * line_length
58
- puts ""
59
- end
60
-
61
- def exit_status
62
- @failed ? 1 : 0
63
- end
64
- end
@@ -1,59 +0,0 @@
1
- require "json"
2
-
3
- class RepoMetadata
4
- attr_reader :data
5
-
6
- def initialize data
7
- @data = data
8
- normalize_data!
9
- end
10
-
11
- def allowed_fields
12
- [
13
- "name", "version", "language", "distribution-name",
14
- "product-page", "github-repository", "issue-tracker"
15
- ]
16
- end
17
-
18
- def build output_directory
19
- fields = @data.to_a.map { |kv| "--#{kv[0]} #{kv[1]}" }
20
- Dir.chdir output_directory do
21
- cmd "python3 -m docuploader create-metadata #{fields.join ' '}"
22
- end
23
- end
24
-
25
- def normalize_data!
26
- require_relative "../lib/googleauth/version.rb"
27
-
28
- @data.delete_if { |k, _| !allowed_fields.include?(k) }
29
- @data["version"] = "v#{Google::Auth::VERSION}"
30
- end
31
-
32
- def [] key
33
- data[key]
34
- end
35
-
36
- def []= key, value
37
- @data[key] = value
38
- end
39
-
40
- def cmd line
41
- puts line
42
- output = `#{line}`
43
- puts output
44
- output
45
- end
46
-
47
- def self.from_source source
48
- if source.is_a? RepoMetadata
49
- data = source.data
50
- elsif source.is_a? Hash
51
- data = source
52
- elsif File.file? source
53
- data = JSON.parse File.read(source)
54
- else
55
- raise "Source must be a path, hash, or RepoMetadata instance"
56
- end
57
- RepoMetadata.new data
58
- end
59
- end
@@ -1,171 +0,0 @@
1
- # Copyright 2015, Google Inc.
2
- # All rights reserved.
3
- #
4
- # Redistribution and use in source and binary forms, with or without
5
- # modification, are permitted provided that the following conditions are
6
- # met:
7
- #
8
- # * Redistributions of source code must retain the above copyright
9
- # notice, this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above
11
- # copyright notice, this list of conditions and the following disclaimer
12
- # in the documentation and/or other materials provided with the
13
- # distribution.
14
- # * Neither the name of Google Inc. nor the names of its
15
- # contributors may be used to endorse or promote products derived from
16
- # this software without specific prior written permission.
17
- #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- spec_dir = File.expand_path File.join(File.dirname(__FILE__))
31
- $LOAD_PATH.unshift spec_dir
32
- $LOAD_PATH.uniq!
33
-
34
- require "faraday"
35
- require "spec_helper"
36
-
37
- shared_examples "apply/apply! are OK" do
38
- let(:auth_key) { :authorization }
39
-
40
- # tests that use these examples need to define
41
- #
42
- # @client which should be an auth client
43
- #
44
- # @make_auth_stubs, which should stub out the expected http behaviour of the
45
- # auth client
46
- describe "#fetch_access_token" do
47
- let(:token) { "1/abcdef1234567890" }
48
- let :access_stub do
49
- make_auth_stubs access_token: token
50
- end
51
- let :id_stub do
52
- make_auth_stubs id_token: token
53
- end
54
-
55
- it "should set access_token to the fetched value" do
56
- access_stub
57
- @client.fetch_access_token!
58
- expect(@client.access_token).to eq(token)
59
- expect(access_stub).to have_been_requested
60
- end
61
-
62
- it "should set id_token to the fetched value" do
63
- skip unless @id_client
64
- id_stub
65
- @id_client.fetch_access_token!
66
- expect(@id_client.id_token).to eq(token)
67
- expect(id_stub).to have_been_requested
68
- end
69
-
70
- it "should notify refresh listeners after updating" do
71
- access_stub
72
- expect do |b|
73
- @client.on_refresh(&b)
74
- @client.fetch_access_token!
75
- end.to yield_with_args(have_attributes(
76
- access_token: "1/abcdef1234567890"
77
- ))
78
- expect(access_stub).to have_been_requested
79
- end
80
- end
81
-
82
- describe "#apply!" do
83
- it "should update the target hash with fetched access token" do
84
- token = "1/abcdef1234567890"
85
- stub = make_auth_stubs access_token: token
86
-
87
- md = { foo: "bar" }
88
- @client.apply! md
89
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
90
- expect(md).to eq(want)
91
- expect(stub).to have_been_requested
92
- end
93
-
94
- it "should update the target hash with fetched ID token" do
95
- skip unless @id_client
96
- token = "1/abcdef1234567890"
97
- stub = make_auth_stubs id_token: token
98
-
99
- md = { foo: "bar" }
100
- @id_client.apply! md
101
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
102
- expect(md).to eq(want)
103
- expect(stub).to have_been_requested
104
- end
105
- end
106
-
107
- describe "updater_proc" do
108
- it "should provide a proc that updates a hash with the access token" do
109
- token = "1/abcdef1234567890"
110
- stub = make_auth_stubs access_token: token
111
- md = { foo: "bar" }
112
- the_proc = @client.updater_proc
113
- got = the_proc.call md
114
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
115
- expect(got).to eq(want)
116
- expect(stub).to have_been_requested
117
- end
118
- end
119
-
120
- describe "#apply" do
121
- it "should not update the original hash with the access token" do
122
- token = "1/abcdef1234567890"
123
- stub = make_auth_stubs access_token: token
124
-
125
- md = { foo: "bar" }
126
- @client.apply md
127
- want = { foo: "bar" }
128
- expect(md).to eq(want)
129
- expect(stub).to have_been_requested
130
- end
131
-
132
- it "should add the token to the returned hash" do
133
- token = "1/abcdef1234567890"
134
- stub = make_auth_stubs access_token: token
135
-
136
- md = { foo: "bar" }
137
- got = @client.apply md
138
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
139
- expect(got).to eq(want)
140
- expect(stub).to have_been_requested
141
- end
142
-
143
- it "should not fetch a new token if the current is not expired" do
144
- token = "1/abcdef1234567890"
145
- stub = make_auth_stubs access_token: token
146
-
147
- n = 5 # arbitrary
148
- n.times do |_t|
149
- md = { foo: "bar" }
150
- got = @client.apply md
151
- want = { :foo => "bar", auth_key => "Bearer #{token}" }
152
- expect(got).to eq(want)
153
- end
154
- expect(stub).to have_been_requested
155
- end
156
-
157
- it "should fetch a new token if the current one is expired" do
158
- token1 = "1/abcdef1234567890"
159
- token2 = "2/abcdef1234567891"
160
-
161
- [token1, token2].each do |t|
162
- make_auth_stubs access_token: t
163
- md = { foo: "bar" }
164
- got = @client.apply md
165
- want = { :foo => "bar", auth_key => "Bearer #{t}" }
166
- expect(got).to eq(want)
167
- @client.expires_at -= 3601 # default is to expire in 1hr
168
- end
169
- end
170
- end
171
- end