signet 0.11.0 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -15
  3. data/Gemfile +5 -4
  4. data/README.md +4 -6
  5. data/Rakefile +107 -37
  6. data/lib/signet.rb +17 -14
  7. data/lib/signet/errors.rb +4 -4
  8. data/lib/signet/oauth_1.rb +129 -154
  9. data/lib/signet/oauth_1/client.rb +309 -343
  10. data/lib/signet/oauth_1/credential.rb +40 -37
  11. data/lib/signet/oauth_1/server.rb +197 -203
  12. data/lib/signet/oauth_1/signature_methods/hmac_sha1.rb +11 -10
  13. data/lib/signet/oauth_1/signature_methods/plaintext.rb +8 -7
  14. data/lib/signet/oauth_1/signature_methods/rsa_sha1.rb +11 -11
  15. data/lib/signet/oauth_2.rb +41 -43
  16. data/lib/signet/oauth_2/client.rb +328 -313
  17. data/lib/signet/version.rb +2 -73
  18. data/signet.gemspec +37 -39
  19. data/spec/signet/oauth_1/client_spec.rb +313 -315
  20. data/spec/signet/oauth_1/credential_spec.rb +64 -56
  21. data/spec/signet/oauth_1/server_spec.rb +362 -362
  22. data/spec/signet/oauth_1/signature_methods/hmac_sha1_spec.rb +26 -26
  23. data/spec/signet/oauth_1/signature_methods/plaintext_spec.rb +28 -28
  24. data/spec/signet/oauth_1/signature_methods/rsa_sha1_spec.rb +34 -35
  25. data/spec/signet/oauth_1_spec.rb +553 -524
  26. data/spec/signet/oauth_2/client_spec.rb +652 -576
  27. data/spec/signet/oauth_2_spec.rb +88 -89
  28. data/spec/signet_spec.rb +41 -41
  29. data/spec/spec_helper.rb +7 -7
  30. data/spec/spec_helper_spec.rb +8 -8
  31. metadata +64 -52
  32. data/tasks/clobber.rake +0 -2
  33. data/tasks/gem.rake +0 -34
  34. data/tasks/git.rake +0 -40
  35. data/tasks/metrics.rake +0 -41
  36. data/tasks/spec.rake +0 -34
  37. data/tasks/wiki.rake +0 -38
  38. data/tasks/yard.rake +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee45b6d3e6075bfd1d1e5b2fae2830e0f981b2e70e7bb075422e51e20b12f4e8
4
- data.tar.gz: 3f019f83eda28d4e0a2b80da812c181f605eae19ecc5b138f9ceee2cfd686fbc
3
+ metadata.gz: c94f4ee9a5ea982bba69826873be998a4cbf833af8c70920fd2384558efe33e5
4
+ data.tar.gz: 211e019a051159858a4e18cbe64efe9989a13df08b2d449e67ac4057f91f4332
5
5
  SHA512:
6
- metadata.gz: 4e9f7704e31e89ecb6b8b24c6f09d1d13615cdfc7ba2d165384ab854be0f33c175bc38db7fd138070fdafff73d33676b96d8e1403b10128e811c25311cd15bf8
7
- data.tar.gz: 399c408f37ef1cd30d0e0d31dab9d80ebed8a917e659d252905f4d162511edf2f88d72e27cf8b0792794245ce8e06ae769c473aa94f4c559dcea9e222e221785
6
+ metadata.gz: d8e5ac7a7977d89976275a4988e62139622436dea9f539a934b99e94f32832e57c8c522a91647449f9b5ea199fa561ad62680803f7d1dbe5b40946087b95dae4
7
+ data.tar.gz: 2b6476b3b89d09d66c0274fa9bd1b5da6aebb68932e7e15325cf34ccca14b5d5f5a16f76a22330bbcb9f6b66882e016eeccb54977e7a87cc4ce6f32568361808
@@ -1,27 +1,63 @@
1
- ## 0.11.0 (2018-10-08)
1
+ # Release History
2
+
3
+ ## [0.14.1](https://www.github.com/googleapis/signet/compare/v0.14.0...v0.14.1) (2021-01-27)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * Fix OAuth1 signature with duplicate query param names ([9f5b81a](https://www.github.com/googleapis/signet/commit/9f5b81a60625a6e6f0e5bca24c67b90e73d7479b))
9
+
10
+ ## 0.14.0 / 2020-03-31
11
+
12
+ * Support for fetching ID tokens from google oauth2 endpoint.
13
+
14
+ ## 0.13.2 / 2020-03-25
15
+
16
+ Rerelease of 0.13.1.
17
+
18
+ ## 0.13.1 / 2020-03-24
19
+
20
+ * Update github url
21
+
22
+ ## 0.13.0 / 2020-02-24
23
+
24
+ * Support Faraday 1.x
25
+
26
+ ## 0.12.0 / 2019-10-08
27
+
28
+ * This version now requires Ruby 2.4.
29
+ * Support array values of the "aud" field.
30
+ * Normalize the version constant to match related gems.
31
+
32
+ ## 0.11.0 / 2018-10-08
33
+
2
34
  * Add constant time comparison for oauth signatures.
3
35
 
4
- ## 0.10.0 (2018-09-21)
36
+ ## 0.10.0 / 2018-09-21
37
+
5
38
  * Add UnexpectedStatusError class for http status errors that are not handled.
6
39
 
7
- ## 0.9.2 (2018-09-12)
40
+ ## 0.9.2 / 2018-09-12
41
+
8
42
  * Update issued_at correctly when it is set simultaneously with expires_in.
9
43
 
10
- ## 0.9.1 (2018-08-29)
44
+ ## 0.9.1 / 2018-08-29
45
+
11
46
  * Warn on EOL ruby versions.
12
47
  * Fix DateTime normalization.
13
48
 
14
- ## 0.9.0 (2018-08-20)
49
+ ## 0.9.0 / 2018-08-20
50
+
15
51
  * Add RemoteServerError class for 5xx level errors.
16
52
  * Allow to_json to be called with arguments
17
53
  * Expires_in now sets and reflects current expires_at value
18
54
  * Expires_within(0) now returns false when expires_at is nil.
19
55
 
20
- ## 0.8.1 (2017-10-13)
56
+ ## 0.8.1 / 2017-10-13
21
57
 
22
58
  * Restore support for Ruby 1.9.3
23
59
 
24
- ## 0.8.0 (2017-10-12)
60
+ ## 0.8.0 / 2017-10-12
25
61
 
26
62
  * Ensure the "expires_at" attribute is recalculated on refresh (chutzimir)
27
63
  * Fix warnings on Ruby 2.4 (koic)
@@ -29,20 +65,20 @@
29
65
  * Provide signature verification algorithm for compatibility with ruby-jwt 2.0 (jurriaan)
30
66
  * Signet::OAuth2::Client#decoded_id_token can take a keyfinder block (mvastola)
31
67
 
32
- ## 0.7.3 (2016-06-20)
68
+ ## 0.7.3 / 2016-06-20
33
69
 
34
70
  * Fix timestamp parsing on 32-bit systems
35
71
  * Fix expiration check when issue/expiry times are nil
36
72
 
37
- ## 0.7.2 (2015-12-21)
73
+ ## 0.7.2 / 2015-12-21
38
74
 
39
75
  * Don't assume Faraday form encoding middleware is present
40
76
 
41
- ## 0.7.1 (2015-12-17)
77
+ ## 0.7.1 / 2015-12-17
42
78
 
43
79
  * Fix an issue with date parsing
44
80
 
45
- ## 0.7 (2015-12-06)
81
+ ## 0.7 / 2015-12-06
46
82
 
47
83
  * No longer overwrite SSL environment variables.
48
84
  * Tighten up date & URL (de)serialization for OAuth2 client
@@ -51,7 +87,7 @@
51
87
  * Add expires_within(sec) method to oauth2 client to facilitate proactive
52
88
  refreshes
53
89
 
54
- ## 0.6.1 (2015-06-08)
90
+ ## 0.6.1 / 2015-06-08
55
91
 
56
92
  * Fix language warnings for unused & shadowed variables ((@blowmage)[])
57
93
  * Update SSL cert path for OSX ((@gambaroff)[])
@@ -59,14 +95,14 @@
59
95
  * Fix incorrect parameter name in OAuth2 client docs ((@samuelreh)[])
60
96
  * Fix symbolization of URL parameter keys ((@swifthand)[])
61
97
 
62
- ## 0.6.0 (2014-12-05)
98
+ ## 0.6.0 / 2014-12-05
63
99
 
64
100
  * Drop support for ruby versions < 1.9.3
65
101
  * Update gem dependencies and lock down versions tighter
66
102
  * Allow form encoded responses when exchanging OAuth 2 authorization codes
67
103
  * Normalize options keys for indifferent access
68
104
 
69
- ## 0.5.1 (2014-06-08)
105
+ ## 0.5.1 / 2014-06-08
70
106
 
71
107
  * Allow Hash objects to be used to initialize authorization URI
72
108
  * Added PLAINTEXT and RSA-SHA1 signature methods to OAuth 1 support
@@ -74,7 +110,7 @@
74
110
  * The `approval_prompt` option no longer defaults to `:force`
75
111
  * The `approval_prompt` and `prompt` are now mutually exclusive.
76
112
 
77
- ## 0.5.0 (2013-05-31)
113
+ ## 0.5.0 / 2013-05-31
78
114
 
79
115
  * Switched to faraday 0.9.0
80
116
  * Added `expires_at` option
data/Gemfile CHANGED
@@ -1,7 +1,8 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'jruby-openssl', :platforms => :jruby
6
- gem 'hurley'
7
- gem 'bundler', '~> 1.15'
5
+ gem "bundler", ">= 1.15"
6
+ gem "gems", "~> 1.2"
7
+ gem "hurley"
8
+ gem "jruby-openssl", platforms: :jruby
data/README.md CHANGED
@@ -1,14 +1,13 @@
1
1
  # Signet
2
2
 
3
3
  <dl>
4
- <dt>Homepage</dt><dd><a href="http://code.google.com/p/oauth-signet/">http://code.google.com/p/oauth-signet/</a></dd>
4
+ <dt>Homepage</dt><dd><a href="https://github.com/googleapis/signet/">https://github.com/googleapis/signet/</a></dd>
5
5
  <dt>Author</dt><dd><a href="mailto:bobaman@google.com">Bob Aman</a></dd>
6
6
  <dt>Copyright</dt><dd>Copyright © 2010 Google, Inc.</dd>
7
7
  <dt>License</dt><dd>Apache 2.0</dd>
8
8
  </dl>
9
9
 
10
10
  [![Gem Version](https://badge.fury.io/rb/signet.svg)](https://badge.fury.io/rb/signet)
11
- [![Build Status](https://secure.travis-ci.org/google/signet.png)](http://travis-ci.org/google/signet)
12
11
 
13
12
  ## Description
14
13
 
@@ -59,10 +58,9 @@ client.fetch_access_token!
59
58
  Be sure `https://rubygems.org` is in your gem sources.
60
59
 
61
60
  ## Supported Ruby Versions
62
- This library is currently supported on Ruby 1.9+.
63
- However, Ruby 2.4 or later is strongly recommended, as earlier releases have
64
- reached or are nearing end-of-life. After March 31, 2019, Google will provide
65
- official support only for Ruby versions that are considered current and
61
+
62
+ This library requires Ruby 2.4 or later.
63
+ In general, this library supports Ruby versions that are considered current and
66
64
  supported by Ruby Core (that is, Ruby versions that are either in normal
67
65
  maintenance or in security maintenance).
68
66
  See https://www.ruby-lang.org/en/downloads/branches/ for further details.
data/Rakefile CHANGED
@@ -1,42 +1,112 @@
1
- lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib"))
2
- $:.unshift(lib_dir)
3
- $:.uniq!
4
-
5
- require 'rubygems'
6
- require 'rake'
1
+ require "rubygems"
2
+ require "json"
3
+ require "rake"
7
4
  require "bundler/gem_tasks"
8
5
 
9
- require File.join(File.dirname(__FILE__), 'lib/signet', 'version')
10
-
11
- PKG_DISPLAY_NAME = 'Signet'
12
- PKG_NAME = PKG_DISPLAY_NAME.downcase
13
- PKG_VERSION = Signet::VERSION::STRING
14
- PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
15
-
16
- RELEASE_NAME = "REL #{PKG_VERSION}"
17
-
18
- PKG_AUTHOR = "Bob Aman"
19
- PKG_AUTHOR_EMAIL = "bobaman@google.com"
20
- PKG_HOMEPAGE = "http://code.google.com/p/oauth-signet/"
21
- PKG_DESCRIPTION = <<-TEXT
22
- Signet is an OAuth 1.0 / OAuth 2.0 implementation.
23
- TEXT
24
- PKG_SUMMARY = PKG_DESCRIPTION
25
-
26
- PKG_FILES = FileList[
27
- "lib/**/*", "spec/**/*", "vendor/**/*",
28
- "tasks/**/*", "website/**/*",
29
- "[A-Z]*", "Rakefile"
30
- ].exclude(/database\.yml/).exclude(/[_\.]git$/).exclude(/Gemfile\.lock/)
31
-
32
- RCOV_ENABLED = !!(RUBY_PLATFORM != 'java' && RUBY_VERSION =~ /^1\.8/)
33
- if RCOV_ENABLED
34
- task :default => 'spec:rcov'
35
- else
36
- task :default => 'spec:normal'
6
+ task :release_gem, :tag do |_t, args|
7
+ tag = args[:tag]
8
+ raise "You must provide a tag to release." if tag.nil?
9
+
10
+ # Verify the tag format "vVERSION"
11
+ m = tag.match /v(?<version>\S*)/
12
+ raise "Tag #{tag} does not match the expected format." if m.nil?
13
+
14
+ version = m[:version]
15
+ raise "You must provide a version." if version.nil?
16
+
17
+ api_token = ENV["RUBYGEMS_API_TOKEN"]
18
+
19
+ require "gems"
20
+ if api_token
21
+ ::Gems.configure do |config|
22
+ config.key = api_token
23
+ end
24
+ end
25
+
26
+ Bundler.with_clean_env do
27
+ sh "rm -rf pkg"
28
+ sh "bundle update"
29
+ sh "bundle exec rake build"
30
+ end
31
+
32
+ path_to_be_pushed = "pkg/signet-#{version}.gem"
33
+ gem_was_published = nil
34
+ if File.file? path_to_be_pushed
35
+ begin
36
+ response = ::Gems.push File.new(path_to_be_pushed)
37
+ puts response
38
+ raise unless response.include? "Successfully registered gem:"
39
+ gem_was_published = true
40
+ puts "Successfully built and pushed signet for version #{version}"
41
+ rescue StandardError => e
42
+ gem_was_published = false
43
+ puts "Error while releasing signet version #{version}: #{e.message}"
44
+ end
45
+ else
46
+ raise "Cannot build signet for version #{version}"
47
+ end
48
+
49
+ Rake::Task["kokoro:publish_docs"].invoke if gem_was_published
50
+ end
51
+
52
+ task :ci do
53
+ header "Using Ruby - #{RUBY_VERSION}"
54
+ sh "bundle exec rubocop"
55
+ sh "bundle exec rspec"
37
56
  end
38
57
 
39
- WINDOWS = (RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/) rescue false
40
- SUDO = WINDOWS ? '' : ('sudo' unless ENV['SUDOLESS'])
58
+ namespace :kokoro do
59
+ task :load_env_vars do
60
+ service_account = "#{ENV['KOKORO_GFILE_DIR']}/service-account.json"
61
+ ENV["GOOGLE_APPLICATION_CREDENTIALS"] = service_account
62
+ filename = "#{ENV['KOKORO_GFILE_DIR']}/env_vars.json"
63
+ env_vars = JSON.parse File.read(filename)
64
+ env_vars.each { |k, v| ENV[k] = v }
65
+ end
41
66
 
42
- Dir['tasks/**/*.rake'].each { |rake| load rake }
67
+ task :presubmit do
68
+ Rake::Task["ci"].invoke
69
+ end
70
+
71
+ task :continuous do
72
+ Rake::Task["ci"].invoke
73
+ end
74
+
75
+ task :nightly do
76
+ Rake::Task["ci"].invoke
77
+ end
78
+
79
+ task :release do
80
+ version = "0.1.0"
81
+ Bundler.with_clean_env do
82
+ version = `bundle exec gem list`
83
+ .split("\n").select { |line| line.include? "signet" }
84
+ .first.split("(").last.split(")").first || "0.1.0"
85
+ end
86
+ Rake::Task["kokoro:load_env_vars"].invoke
87
+ Rake::Task["release_gem"].invoke "v#{version}"
88
+ end
89
+
90
+ task :post do
91
+ require_relative "rakelib/link_checker.rb"
92
+
93
+ link_checker = LinkChecker.new
94
+ link_checker.run
95
+ exit link_checker.exit_status
96
+ end
97
+
98
+ task :publish_docs do
99
+ require_relative "rakelib/devsite_builder.rb"
100
+
101
+ DevsiteBuilder.new(__dir__).publish
102
+ end
103
+ end
104
+
105
+ def header str, token = "#"
106
+ line_length = str.length + 8
107
+ puts ""
108
+ puts token * line_length
109
+ puts "#{token * 3} #{str} #{token * 3}"
110
+ puts token * line_length
111
+ puts ""
112
+ end
@@ -12,13 +12,15 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'signet/version'
15
+ require "signet/version"
16
16
 
17
17
  module Signet #:nodoc:
18
- def self.parse_auth_param_list(auth_param_string)
18
+ # rubocop:disable Metrics/AbcSize
19
+ # rubocop:disable Metrics/MethodLength
20
+ def self.parse_auth_param_list auth_param_string
19
21
  # Production rules from:
20
22
  # http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-12
21
- token = /[-!#$\%&'*+.^_`|~0-9a-zA-Z]+/
23
+ token = /[-!#{$OUTPUT_RECORD_SEPARATOR}%&'*+.^_`|~0-9a-zA-Z]+/
22
24
  d_qdtext = /[\s\x21\x23-\x5B\x5D-\x7E\x80-\xFF]/n
23
25
  d_quoted_pair = /\\[\s\x21-\x7E\x80-\xFF]/n
24
26
  d_qs = /"(?:#{d_qdtext}|#{d_quoted_pair})*"/
@@ -36,10 +38,10 @@ module Signet #:nodoc:
36
38
  #
37
39
  # This would be way easier in Ruby 1.9, but we want backwards
38
40
  # compatibility.
39
- while (match = remainder.match(auth_param))
41
+ while (match = remainder.match auth_param)
40
42
  if match.pre_match && match.pre_match !~ /^[\s,]*$/
41
43
  raise ParseError,
42
- "Unexpected auth param format: '#{auth_param_string}'."
44
+ "Unexpected auth param format: '#{auth_param_string}'."
43
45
  end
44
46
  auth_param_pairs << match.captures[0] # Appending pair
45
47
  remainder = match.post_match
@@ -47,24 +49,25 @@ module Signet #:nodoc:
47
49
  end
48
50
  if last_match.post_match && last_match.post_match !~ /^[\s,]*$/
49
51
  raise ParseError,
50
- "Unexpected auth param format: '#{auth_param_string}'."
52
+ "Unexpected auth param format: '#{auth_param_string}'."
51
53
  end
52
54
  # Now parse the auth-param pair strings & turn them into key-value pairs.
53
- return (auth_param_pairs.inject([]) do |accu, pair|
54
- name, value = pair.split('=', 2)
55
+ (auth_param_pairs.each_with_object [] do |pair, accu|
56
+ name, value = pair.split "=", 2
55
57
  if value =~ /^".*"$/
56
58
  value = value.gsub(/^"(.*)"$/, '\1').gsub(/\\(.)/, '\1')
57
59
  elsif value =~ /^'.*'$/
58
60
  value = value.gsub(/^'(.*)'$/, '\1').gsub(/\\(.)/, '\1')
59
- elsif value =~ /[\(\)<>@,;:\\\"\/\[\]?={}]/
61
+ elsif value =~ %r{[\(\)<>@,;:\\\"/\[\]?={}]}
60
62
  # Certain special characters are not allowed
61
- raise ParseError, (
62
- "Unexpected characters in auth param " +
63
- "list: '#{auth_param_string}'."
64
- )
63
+ raise ParseError,
64
+ "Unexpected characters in auth param " \
65
+ "list: '#{auth_param_string}'."
66
+
65
67
  end
66
68
  accu << [name, value]
67
- accu
68
69
  end)
69
70
  end
71
+ # rubocop:enable Metrics/AbcSize
72
+ # rubocop:enable Metrics/MethodLength
70
73
  end
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'addressable/uri'
15
+ require "addressable/uri"
16
16
 
17
17
  module Signet
18
18
  ##
@@ -66,14 +66,14 @@ module Signet
66
66
  # - <code>:uri</code> -
67
67
  # A URI identifying a human-readable web page with additional
68
68
  # information about the error, indended for the resource owner.
69
- def initialize(message, options={})
70
- super(message)
69
+ def initialize message, options = {}
70
+ super message
71
71
  @options = options
72
72
  @request = options[:request]
73
73
  @response = options[:response]
74
74
  @code = options[:code]
75
75
  @description = options[:description]
76
- @uri = Addressable::URI.parse(options[:uri])
76
+ @uri = Addressable::URI.parse options[:uri]
77
77
  end
78
78
 
79
79
  ##
@@ -1,11 +1,11 @@
1
- require 'addressable/uri'
2
- require 'signet'
1
+ require "addressable/uri"
2
+ require "signet"
3
3
 
4
- require 'securerandom'
4
+ require "securerandom"
5
5
 
6
6
  module Signet #:nodoc:
7
7
  module OAuth1
8
- OUT_OF_BAND = 'oob'
8
+ OUT_OF_BAND = "oob".freeze
9
9
 
10
10
  ##
11
11
  # Converts a value to a percent-encoded <code>String</code> according to
@@ -15,9 +15,9 @@ module Signet #:nodoc:
15
15
  # @param [Symbol, #to_str] value The value to be encoded.
16
16
  #
17
17
  # @return [String] The percent-encoded value.
18
- def self.encode(value)
19
- value = value.to_s if value.kind_of?(Symbol)
20
- return Addressable::URI.encode_component(
18
+ def self.encode value
19
+ value = value.to_s if value.is_a? Symbol
20
+ Addressable::URI.encode_component(
21
21
  value,
22
22
  Addressable::URI::CharacterClasses::UNRESERVED
23
23
  )
@@ -30,8 +30,8 @@ module Signet #:nodoc:
30
30
  # The percent-encoded <code>String</code> to be unencoded.
31
31
  #
32
32
  # @return [String] The unencoded value.
33
- def self.unencode(value)
34
- return Addressable::URI.unencode_component(value)
33
+ def self.unencode value
34
+ Addressable::URI.unencode_component value
35
35
  end
36
36
 
37
37
  ##
@@ -39,8 +39,8 @@ module Signet #:nodoc:
39
39
  # value.
40
40
  #
41
41
  # @return [String] The current timestamp.
42
- def self.generate_timestamp()
43
- return Time.now.to_i.to_s
42
+ def self.generate_timestamp
43
+ Time.now.to_i.to_s
44
44
  end
45
45
 
46
46
  ##
@@ -48,9 +48,10 @@ module Signet #:nodoc:
48
48
  # value.
49
49
  #
50
50
  # @return [String] A random nonce.
51
- def self.generate_nonce()
52
- return SecureRandom.random_bytes(16).unpack('H*').join('')
51
+ def self.generate_nonce
52
+ SecureRandom.random_bytes(16).unpack("H*").join ""
53
53
  end
54
+ # rubocop:disable Metrics/MethodLength
54
55
 
55
56
  ##
56
57
  # Processes an options <code>Hash</code> to find a credential key value.
@@ -62,36 +63,36 @@ module Signet #:nodoc:
62
63
  # or <code>:access</code>.
63
64
  #
64
65
  # @return [String] The credential key value.
65
- def self.extract_credential_key_option(credential_type, options)
66
+ def self.extract_credential_key_option credential_type, options
66
67
  # Normalize key to String to allow indifferent access.
67
- options = options.inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu }
68
+ options = options.each_with_object({}) { |(k, v), accu| accu[k.to_s] = v; }
68
69
  credential_key = "#{credential_type}_credential_key"
69
70
  credential = "#{credential_type}_credential"
70
71
  if options[credential_key]
71
72
  credential_key = options[credential_key]
72
73
  elsif options[credential]
73
- require 'signet/oauth_1/credential'
74
- if !options[credential].respond_to?(:key)
74
+ require "signet/oauth_1/credential"
75
+ unless options[credential].respond_to? :key
75
76
  raise TypeError,
76
- "Expected Signet::OAuth1::Credential, " +
77
- "got #{options[credential].class}."
77
+ "Expected Signet::OAuth1::Credential, " \
78
+ "got #{options[credential].class}."
78
79
  end
79
80
  credential_key = options[credential].key
80
81
  elsif options["client"]
81
- require 'signet/oauth_1/client'
82
- if !options["client"].kind_of?(::Signet::OAuth1::Client)
82
+ require "signet/oauth_1/client"
83
+ unless options["client"].is_a? ::Signet::OAuth1::Client
83
84
  raise TypeError,
84
- "Expected Signet::OAuth1::Client, got #{options["client"].class}."
85
+ "Expected Signet::OAuth1::Client, got #{options['client'].class}."
85
86
  end
86
- credential_key = options["client"].send(credential_key)
87
+ credential_key = options["client"].send credential_key
87
88
  else
88
89
  credential_key = nil
89
90
  end
90
- if credential_key != nil && !credential_key.kind_of?(String)
91
+ if !credential_key.nil? && !credential_key.is_a?(String)
91
92
  raise TypeError,
92
- "Expected String, got #{credential_key.class}."
93
+ "Expected String, got #{credential_key.class}."
93
94
  end
94
- return credential_key
95
+ credential_key
95
96
  end
96
97
 
97
98
  ##
@@ -104,37 +105,38 @@ module Signet #:nodoc:
104
105
  # or <code>:access</code>.
105
106
  #
106
107
  # @return [String] The credential secret value.
107
- def self.extract_credential_secret_option(credential_type, options)
108
+ def self.extract_credential_secret_option credential_type, options
108
109
  # Normalize key to String to allow indifferent access.
109
- options = options.inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu }
110
+ options = options.each_with_object({}) { |(k, v), accu| accu[k.to_s] = v; }
110
111
  credential_secret = "#{credential_type}_credential_secret"
111
112
  credential = "#{credential_type}_credential"
112
113
  if options[credential_secret]
113
114
  credential_secret = options[credential_secret]
114
115
  elsif options[credential]
115
- require 'signet/oauth_1/credential'
116
- if !options[credential].respond_to?(:secret)
116
+ require "signet/oauth_1/credential"
117
+ unless options[credential].respond_to? :secret
117
118
  raise TypeError,
118
- "Expected Signet::OAuth1::Credential, " +
119
- "got #{options[credential].class}."
119
+ "Expected Signet::OAuth1::Credential, " \
120
+ "got #{options[credential].class}."
120
121
  end
121
122
  credential_secret = options[credential].secret
122
123
  elsif options["client"]
123
- require 'signet/oauth_1/client'
124
- if !options["client"].kind_of?(::Signet::OAuth1::Client)
124
+ require "signet/oauth_1/client"
125
+ unless options["client"].is_a? ::Signet::OAuth1::Client
125
126
  raise TypeError,
126
- "Expected Signet::OAuth1::Client, got #{options["client"].class}."
127
+ "Expected Signet::OAuth1::Client, got #{options['client'].class}."
127
128
  end
128
- credential_secret = options["client"].send(credential_secret)
129
+ credential_secret = options["client"].send credential_secret
129
130
  else
130
131
  credential_secret = nil
131
132
  end
132
- if credential_secret != nil && !credential_secret.kind_of?(String)
133
+ if !credential_secret.nil? && !credential_secret.is_a?(String)
133
134
  raise TypeError,
134
- "Expected String, got #{credential_secret.class}."
135
+ "Expected String, got #{credential_secret.class}."
135
136
  end
136
- return credential_secret
137
+ credential_secret
137
138
  end
139
+ # rubocop:enable Metrics/MethodLength
138
140
 
139
141
  ##
140
142
  # Normalizes a set of OAuth parameters according to the algorithm given
@@ -145,16 +147,14 @@ module Signet #:nodoc:
145
147
  # @param [Enumerable] parameters The OAuth parameter list.
146
148
  #
147
149
  # @return [String] The normalized parameter list.
148
- def self.normalize_parameters(parameters)
149
- if !parameters.kind_of?(Enumerable)
150
- raise TypeError, "Expected Enumerable, got #{parameters.class}."
151
- end
150
+ def self.normalize_parameters parameters
151
+ raise TypeError, "Expected Enumerable, got #{parameters.class}." unless parameters.is_a? Enumerable
152
152
  parameter_list = parameters.map do |k, v|
153
153
  next if k == "oauth_signature"
154
154
  # This is probably the wrong place to try to exclude the realm
155
- "#{self.encode(k)}=#{self.encode(v)}"
155
+ "#{encode k}=#{encode v}"
156
156
  end
157
- return parameter_list.compact.sort.join("&")
157
+ parameter_list.compact.sort.join "&"
158
158
  end
159
159
 
160
160
  ##
@@ -167,29 +167,27 @@ module Signet #:nodoc:
167
167
  # @param [Enumerable] parameters The OAuth parameter list.
168
168
  #
169
169
  # @return [String] The signature base string.
170
- def self.generate_base_string(method, uri, parameters)
171
- if !parameters.kind_of?(Enumerable)
172
- raise TypeError, "Expected Enumerable, got #{parameters.class}."
173
- end
170
+ def self.generate_base_string method, uri, parameters
171
+ raise TypeError, "Expected Enumerable, got #{parameters.class}." unless parameters.is_a? Enumerable
174
172
  method = method.to_s.upcase
175
- parsed_uri = Addressable::URI.parse(uri)
173
+ parsed_uri = Addressable::URI.parse uri
176
174
  uri = Addressable::URI.new(
177
- :scheme => parsed_uri.normalized_scheme,
178
- :authority => parsed_uri.normalized_authority,
179
- :path => parsed_uri.path,
180
- :query => parsed_uri.query,
181
- :fragment => parsed_uri.fragment
175
+ scheme: parsed_uri.normalized_scheme,
176
+ authority: parsed_uri.normalized_authority,
177
+ path: parsed_uri.path,
178
+ query: parsed_uri.query,
179
+ fragment: parsed_uri.fragment
182
180
  )
183
- uri_parameters = uri.query_values.to_a
181
+ uri_parameters = uri.query_values(Array) || []
184
182
  uri = uri.omit(:query, :fragment).to_s
185
183
  merged_parameters =
186
184
  uri_parameters.concat(parameters.map { |k, v| [k, v] })
187
- parameter_string = self.normalize_parameters(merged_parameters)
188
- return [
189
- self.encode(method),
190
- self.encode(uri),
191
- self.encode(parameter_string)
192
- ].join('&')
185
+ parameter_string = normalize_parameters merged_parameters
186
+ [
187
+ encode(method),
188
+ encode(uri),
189
+ encode(parameter_string)
190
+ ].join("&")
193
191
  end
194
192
 
195
193
  ##
@@ -201,48 +199,45 @@ module Signet #:nodoc:
201
199
  # The <code>Authorization</code> realm. See RFC 2617.
202
200
  #
203
201
  # @return [String] The <code>Authorization</code> header.
204
- def self.generate_authorization_header(parameters, realm=nil)
205
- if !parameters.kind_of?(Enumerable) || parameters.kind_of?(String)
202
+ def self.generate_authorization_header parameters, realm = nil
203
+ if !parameters.is_a?(Enumerable) || parameters.is_a?(String)
206
204
  raise TypeError, "Expected Enumerable, got #{parameters.class}."
207
205
  end
208
206
  parameter_list = parameters.map do |k, v|
209
- if k == 'realm'
207
+ if k == "realm"
210
208
  raise ArgumentError,
211
- 'The "realm" parameter must be specified as a separate argument.'
209
+ 'The "realm" parameter must be specified as a separate argument.'
212
210
  end
213
- "#{self.encode(k)}=\"#{self.encode(v)}\""
211
+ "#{encode k}=\"#{encode v}\""
214
212
  end
215
213
  if realm
216
- realm = realm.gsub('"', '\"')
217
- parameter_list.unshift("realm=\"#{realm}\"")
214
+ realm = realm.gsub '"', '\"'
215
+ parameter_list.unshift "realm=\"#{realm}\""
218
216
  end
219
- return 'OAuth ' + parameter_list.join(", ")
217
+ "OAuth " + parameter_list.join(", ")
220
218
  end
221
219
 
222
220
  ##
223
221
  # Parses an <code>Authorization</code> header into its component
224
222
  # parameters. Parameter keys and values are decoded according to the
225
223
  # rules given in RFC 5849.
226
- def self.parse_authorization_header(field_value)
227
- if !field_value.kind_of?(String)
228
- raise TypeError, "Expected String, got #{field_value.class}."
229
- end
224
+ def self.parse_authorization_header field_value
225
+ raise TypeError, "Expected String, got #{field_value.class}." unless field_value.is_a? String
230
226
  auth_scheme = field_value[/^([-._0-9a-zA-Z]+)/, 1]
231
227
  case auth_scheme
232
228
  when /^OAuth$/i
233
229
  # Other token types may be supported eventually
234
230
  pairs = Signet.parse_auth_param_list(field_value[/^OAuth\s+(.*)$/i, 1])
235
- return (pairs.inject([]) do |accu, (k, v)|
236
- if k != 'realm'
237
- k = self.unencode(k)
238
- v = self.unencode(v)
231
+ return (pairs.each_with_object [] do |(k, v), accu|
232
+ if k != "realm"
233
+ k = unencode k
234
+ v = unencode v
239
235
  end
240
236
  accu << [k, v]
241
- accu
242
237
  end)
243
238
  else
244
239
  raise ParseError,
245
- 'Parsing non-OAuth Authorization headers is out of scope.'
240
+ "Parsing non-OAuth Authorization headers is out of scope."
246
241
  end
247
242
  end
248
243
 
@@ -253,11 +248,9 @@ module Signet #:nodoc:
253
248
  # @param [String] body The response body.
254
249
  #
255
250
  # @return [Signet::OAuth1::Credential] The OAuth credentials.
256
- def self.parse_form_encoded_credentials(body)
257
- if !body.kind_of?(String)
258
- raise TypeError, "Expected String, got #{body.class}."
259
- end
260
- return Signet::OAuth1::Credential.new(
251
+ def self.parse_form_encoded_credentials body
252
+ raise TypeError, "Expected String, got #{body.class}." unless body.is_a? String
253
+ Signet::OAuth1::Credential.new(
261
254
  Addressable::URI.form_unencode(body)
262
255
  )
263
256
  end
@@ -275,33 +268,33 @@ module Signet #:nodoc:
275
268
  # The token credential secret. Omitted when unavailable.
276
269
  #
277
270
  # @return [String] The signature.
278
- def self.sign_parameters(method, uri, parameters,
279
- client_credential_secret, token_credential_secret=nil)
271
+ def self.sign_parameters method, uri, parameters,
272
+ client_credential_secret, token_credential_secret = nil
280
273
  # Technically, the token_credential_secret parameter here may actually
281
274
  # be a temporary credential secret when obtaining a token credential
282
275
  # for the first time
283
- base_string = self.generate_base_string(method, uri, parameters)
284
- parameters = parameters.inject({}) { |h,(k,v)| h[k.to_s]=v; h }
285
- signature_method = parameters['oauth_signature_method']
276
+ base_string = generate_base_string method, uri, parameters
277
+ parameters = parameters.each_with_object({}) { |(k, v), h| h[k.to_s] = v; }
278
+ signature_method = parameters["oauth_signature_method"]
286
279
  case signature_method
287
- when 'HMAC-SHA1'
288
- require 'signet/oauth_1/signature_methods/hmac_sha1'
280
+ when "HMAC-SHA1"
281
+ require "signet/oauth_1/signature_methods/hmac_sha1"
289
282
  return Signet::OAuth1::HMACSHA1.generate_signature(
290
283
  base_string, client_credential_secret, token_credential_secret
291
284
  )
292
- when 'RSA-SHA1'
293
- require 'signet/oauth_1/signature_methods/rsa_sha1'
285
+ when "RSA-SHA1"
286
+ require "signet/oauth_1/signature_methods/rsa_sha1"
294
287
  return Signet::OAuth1::RSASHA1.generate_signature(
295
288
  base_string, client_credential_secret, token_credential_secret
296
289
  )
297
- when 'PLAINTEXT'
298
- require 'signet/oauth_1/signature_methods/plaintext'
290
+ when "PLAINTEXT"
291
+ require "signet/oauth_1/signature_methods/plaintext"
299
292
  return Signet::OAuth1::PLAINTEXT.generate_signature(
300
293
  base_string, client_credential_secret, token_credential_secret
301
294
  )
302
295
  else
303
296
  raise NotImplementedError,
304
- "Unsupported signature method: #{signature_method}"
297
+ "Unsupported signature method: #{signature_method}"
305
298
  end
306
299
  end
307
300
 
@@ -322,22 +315,20 @@ module Signet #:nodoc:
322
315
  #
323
316
  # @return [Array]
324
317
  # The parameter list as an <code>Array</code> of key/value pairs.
325
- def self.unsigned_temporary_credential_parameters(options={})
318
+ def self.unsigned_temporary_credential_parameters options = {}
326
319
  options = {
327
- :callback => ::Signet::OAuth1::OUT_OF_BAND,
328
- :signature_method => 'HMAC-SHA1',
329
- :additional_parameters => []
320
+ callback: ::Signet::OAuth1::OUT_OF_BAND,
321
+ signature_method: "HMAC-SHA1",
322
+ additional_parameters: []
330
323
  }.merge(options)
331
324
  client_credential_key =
332
- self.extract_credential_key_option(:client, options)
333
- if client_credential_key == nil
334
- raise ArgumentError, "Missing :client_credential_key parameter."
335
- end
325
+ extract_credential_key_option :client, options
326
+ raise ArgumentError, "Missing :client_credential_key parameter." if client_credential_key.nil?
336
327
  parameters = [
337
328
  ["oauth_consumer_key", client_credential_key],
338
329
  ["oauth_signature_method", options[:signature_method]],
339
- ["oauth_timestamp", self.generate_timestamp()],
340
- ["oauth_nonce", self.generate_nonce()],
330
+ ["oauth_timestamp", generate_timestamp],
331
+ ["oauth_nonce", generate_nonce],
341
332
  ["oauth_version", "1.0"],
342
333
  ["oauth_callback", options[:callback]]
343
334
  ]
@@ -345,7 +336,7 @@ module Signet #:nodoc:
345
336
  options[:additional_parameters].each do |key, value|
346
337
  parameters << [key, value]
347
338
  end
348
- return parameters
339
+ parameters
349
340
  end
350
341
 
351
342
  ##
@@ -356,28 +347,24 @@ module Signet #:nodoc:
356
347
  # The base authorization URI.
357
348
  #
358
349
  # @return [String] The authorization URI to redirect the user to.
359
- def self.generate_authorization_uri(authorization_uri, options={})
350
+ def self.generate_authorization_uri authorization_uri, options = {}
360
351
  options = {
361
- :callback => nil,
362
- :additional_parameters => {}
352
+ callback: nil,
353
+ additional_parameters: {}
363
354
  }.merge(options)
364
355
  temporary_credential_key =
365
- self.extract_credential_key_option(:temporary, options)
356
+ extract_credential_key_option :temporary, options
366
357
  parsed_uri = Addressable::URI.parse(authorization_uri).dup
367
358
  query_values = parsed_uri.query_values || {}
368
359
  if options[:additional_parameters]
369
360
  query_values = query_values.merge(
370
- options[:additional_parameters].inject({}) { |h,(k,v)| h[k]=v; h }
361
+ options[:additional_parameters].each_with_object({}) { |(k, v), h| h[k] = v; }
371
362
  )
372
363
  end
373
- if temporary_credential_key
374
- query_values['oauth_token'] = temporary_credential_key
375
- end
376
- if options[:callback]
377
- query_values['oauth_callback'] = options[:callback]
378
- end
364
+ query_values["oauth_token"] = temporary_credential_key if temporary_credential_key
365
+ query_values["oauth_callback"] = options[:callback] if options[:callback]
379
366
  parsed_uri.query_values = query_values
380
- return parsed_uri.normalize.to_s
367
+ parsed_uri.normalize.to_s
381
368
  end
382
369
 
383
370
  ##
@@ -397,35 +384,29 @@ module Signet #:nodoc:
397
384
  #
398
385
  # @return [Array]
399
386
  # The parameter list as an <code>Array</code> of key/value pairs.
400
- def self.unsigned_token_credential_parameters(options={})
387
+ def self.unsigned_token_credential_parameters options = {}
401
388
  options = {
402
- :signature_method => 'HMAC-SHA1',
403
- :verifier => nil
389
+ signature_method: "HMAC-SHA1",
390
+ verifier: nil
404
391
  }.merge(options)
405
392
  client_credential_key =
406
- self.extract_credential_key_option(:client, options)
393
+ extract_credential_key_option :client, options
407
394
  temporary_credential_key =
408
- self.extract_credential_key_option(:temporary, options)
409
- if client_credential_key == nil
410
- raise ArgumentError, "Missing :client_credential_key parameter."
411
- end
412
- if temporary_credential_key == nil
413
- raise ArgumentError, "Missing :temporary_credential_key parameter."
414
- end
415
- if options[:verifier] == nil
416
- raise ArgumentError, "Missing :verifier parameter."
417
- end
395
+ extract_credential_key_option :temporary, options
396
+ raise ArgumentError, "Missing :client_credential_key parameter." if client_credential_key.nil?
397
+ raise ArgumentError, "Missing :temporary_credential_key parameter." if temporary_credential_key.nil?
398
+ raise ArgumentError, "Missing :verifier parameter." if options[:verifier].nil?
418
399
  parameters = [
419
400
  ["oauth_consumer_key", client_credential_key],
420
401
  ["oauth_token", temporary_credential_key],
421
402
  ["oauth_signature_method", options[:signature_method]],
422
- ["oauth_timestamp", self.generate_timestamp()],
423
- ["oauth_nonce", self.generate_nonce()],
403
+ ["oauth_timestamp", generate_timestamp],
404
+ ["oauth_nonce", generate_nonce],
424
405
  ["oauth_verifier", options[:verifier]],
425
406
  ["oauth_version", "1.0"]
426
407
  ]
427
408
  # No additional parameters allowed here
428
- return parameters
409
+ parameters
429
410
  end
430
411
 
431
412
  ##
@@ -445,35 +426,29 @@ module Signet #:nodoc:
445
426
  #
446
427
  # @return [Array]
447
428
  # The parameter list as an <code>Array</code> of key/value pairs.
448
- def self.unsigned_resource_parameters(options={})
429
+ def self.unsigned_resource_parameters options = {}
449
430
  options = {
450
- :signature_method => 'HMAC-SHA1',
451
- :two_legged => false
431
+ signature_method: "HMAC-SHA1",
432
+ two_legged: false
452
433
  }.merge(options)
453
434
  client_credential_key =
454
- self.extract_credential_key_option(:client, options)
455
- if client_credential_key == nil
456
- raise ArgumentError, "Missing :client_credential_key parameter."
457
- end
435
+ extract_credential_key_option :client, options
436
+ raise ArgumentError, "Missing :client_credential_key parameter." if client_credential_key.nil?
458
437
  unless options[:two_legged]
459
438
  token_credential_key =
460
- self.extract_credential_key_option(:token, options)
461
- if token_credential_key == nil
462
- raise ArgumentError, "Missing :token_credential_key parameter."
463
- end
439
+ extract_credential_key_option :token, options
440
+ raise ArgumentError, "Missing :token_credential_key parameter." if token_credential_key.nil?
464
441
  end
465
442
  parameters = [
466
443
  ["oauth_consumer_key", client_credential_key],
467
444
  ["oauth_signature_method", options[:signature_method]],
468
- ["oauth_timestamp", self.generate_timestamp()],
469
- ["oauth_nonce", self.generate_nonce()],
445
+ ["oauth_timestamp", generate_timestamp],
446
+ ["oauth_nonce", generate_nonce],
470
447
  ["oauth_version", "1.0"]
471
448
  ]
472
- unless options[:two_legged]
473
- parameters << ["oauth_token", token_credential_key]
474
- end
449
+ parameters << ["oauth_token", token_credential_key] unless options[:two_legged]
475
450
  # No additional parameters allowed here
476
- return parameters
451
+ parameters
477
452
  end
478
453
  end
479
454
  end